parent-child relationship

Data Access Application Blocks Forum

parent-child relationship

Postby shantha05 on Mon Dec 19, 2005 10:13 pm

How can I create business classes interlinking two tables (parent-child relationship)
shantha05
Staff Sergent
 
Posts: 32
Joined: Mon Dec 19, 2005 8:20 pm

Postby mgnoonan on Mon Dec 19, 2005 10:29 pm

The easiest way is to create a View with the two tables linked. You can then set the QuerySource of one of the related objects to the name of the view and do a Query.Load().

See:
http://www.easyobjects.net/Tutorials/BuildingQueries/tabid/118/Default.aspx#QuerySource

Or you can generate a new business object for the view using the Business View template.
Last edited by mgnoonan on Mon Dec 19, 2005 10:52 pm, edited 1 time in total.
Matt Noonan
EasyObjects.NET - The O/RM for the Enterprise Library
http://www.easyobjects.net
User avatar
mgnoonan
Expert
 
Posts: 1019
Joined: Tue Sep 14, 2004 3:17 am
Location: Springboro, OH

Postby shantha05 on Mon Dec 19, 2005 10:34 pm

Thanks
shantha05
Staff Sergent
 
Posts: 32
Joined: Mon Dec 19, 2005 8:20 pm

Postby TimGSXR on Wed Jan 04, 2006 3:34 pm

So say I have the table User->User_Role->Role I want in my business entity to be able to do User.Roles so if I understand this correctly in my User class i should be able to set up the Roles property for user like so.

public Role Roles
{
get
{
Role roles = new Role;
role.QuerySource = \"vUserRoles\";
role.Where.UserId.Value = this.Id;
role.Query.Load();
return role;
}
}

The problem is the schema for Role does not include the UserId column, the UserId column is grabbed in the view, how will this work?
TimGSXR
Sergeant
 
Posts: 26
Joined: Wed Dec 14, 2005 10:47 am
Location: Newcastle, UK

Postby sbc on Wed Jan 04, 2006 4:07 pm

You could use Filter:

Code: Select all
Role roles = new Role;
role.QuerySource = \"vUserRoles\";
role.Query.Load();
role.Filter = 'UserId=' + this.Id;
return role;


Although you would load all the data in the view first (if there is not a lot of data, then that is not really a problem). Of course including the Id in the view (with the same column name as in the table) would be a better way.
User avatar
sbc
Expert
 
Posts: 446
Joined: Sat Apr 03, 2004 6:10 am
Location: North East Lincolnshire, England

Postby mike.griffin on Wed Jan 04, 2006 4:44 pm

Actually, I think Matt meant for you to create a View then generate an EasyObject off of that?
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mgnoonan on Wed Jan 04, 2006 8:28 pm

Mike is correct. I was suggesting you generate a business object from the view, that way you have typed properties and WhereParameters for all the columns in the view.
Matt Noonan
EasyObjects.NET - The O/RM for the Enterprise Library
http://www.easyobjects.net
User avatar
mgnoonan
Expert
 
Posts: 1019
Joined: Tue Sep 14, 2004 3:17 am
Location: Springboro, OH

Postby TimGSXR on Thu Jan 05, 2006 10:19 am

but you can't do AddNew with a view can you?

What I really need is a way to get the object representing the table from the superset object i.e.

get the business view object(superset of Role + UserId) using where clause from view

UserRoles userroles = new UserRoles;
userroles.Where.UserId.Value = this.Id;
userroles.Query.Load();

from the superset business view object get a standard Business object based on the Role table
Role role = userrole;

Then i'd have the best of both worlds, I thnk.

Cheers

Tim
TimGSXR
Sergeant
 
Posts: 26
Joined: Wed Dec 14, 2005 10:47 am
Location: Newcastle, UK

Postby mike.griffin on Thu Jan 05, 2006 1:49 pm

I think then SBC's recommendation is the way to go, in dOOdads I know you can trick your table based object to load itself from a view that way you can still save it.
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mgnoonan on Thu Jan 05, 2006 2:41 pm

And EO gives you the same capability. Just set the QuerySource as sbc suggested. You don't have to use the Filter property if you don't want, you can add a Where clause to your query that will only retrieve the UserID you are after.
Matt Noonan
EasyObjects.NET - The O/RM for the Enterprise Library
http://www.easyobjects.net
User avatar
mgnoonan
Expert
 
Posts: 1019
Joined: Tue Sep 14, 2004 3:17 am
Location: Springboro, OH

Postby TimGSXR on Thu Jan 05, 2006 2:47 pm

ok thats great giving it a go now.

thanks for the help, EasyObjects rock! :lol:
TimGSXR
Sergeant
 
Posts: 26
Joined: Wed Dec 14, 2005 10:47 am
Location: Newcastle, UK

Postby mgnoonan on Thu Jan 05, 2006 2:49 pm

Thanks! Keep me posted on how your project is going!
Matt Noonan
EasyObjects.NET - The O/RM for the Enterprise Library
http://www.easyobjects.net
User avatar
mgnoonan
Expert
 
Posts: 1019
Joined: Tue Sep 14, 2004 3:17 am
Location: Springboro, OH


Return to EasyObjects.NET (Microsoft Enterprise Library)

Who is online

Users browsing this forum: No registered users and 1 guest