This may seem like an odd question but bare with me.
How do you fill the same data at varying degrees with the least amount of objects?
For instance to the left of this post you see a small representation of my "User Object" that shows only the most basic information. However when you click my user name you go in to my profile which contains this information plus some. So assuming that
ASP.NET Forums uses the same "User Object" for both of these different views of the same model and only fills the appropriate information when requested. How would you go about programming this? And is there currently a pattern or model that gets me almost
there?
>So assuming that ASP.NET Forums uses the same "User Object" for both of these different views of the same model and only fills the appropriate information when requested
Somewhat unlikely - I would expect different objects for each in oder to maximise speed.
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
I agree, it would be very unlikely to have an object that loads in data as it is requested as it would inevitably be slower than just loading all the data at once. I generate all my business entities using codesmith templates and in general I will have an
object which allows updates and loads all the information for each table and a read-only object for each view. This way I could create a view named "SimpleUserInfo" for use in places like the right hand of this page, but if I wanted to update a User I would
have to use the full-featured User object.
Please mark posts as the answer if I was helpful. Thanks!
There a very cases where a lazy load feature is either require or desirable. I can only think of one project in 5 years of Dot Net that needed it.
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
>I assume that this was an object which loaded data from many different locations?
A very astute assumption, however in this particular case it was a single SQL2000 database row and one column was a large text datatype, so this was only loaded on demand as the load took 1-2 seconds instead of being sub-second for the other values. In the
general case, however you are perfectly correct. (Strictly speaking you are correct insofar as SQL 2000 will store large text values separately from the rest of the row.)
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
I have used lazy-loading a little in the past and I use deep-loading often. It's really useful if you have a large object-model with alot of relationships that aren't always necessary. NetTiers handles deep-loading fairly well (with a patch applied to stop
stack overflow exceptions) - you can pass in an object and specify the types that you wish to deepload.
I agree, it would be very unlikely to have an object that loads in data as it is requested as it would inevitably be slower than just loading all the data at once. I generate all my business entities using codesmith templates and in general I will have an
object which allows updates and loads all the information for each table and a read-only object for each view. This way I could create a view named "SimpleUserInfo" for use in places like the right hand of this page, but if I wanted to update a User I would
have to use the full-featured User object.
I think I actually gave you guys the wrong impression. My question wasn't about lazy loading each property individually. I already know the performance problems with that.
My question was essentially in creating a couples states of the object. For example:
// small fill
User user1 = new User();
user1.Id = (int)reader["Id"];
user1.UserName = (string)reader["UserName"];
// partially filled
User user2 = new User();
user2.Id = (int)reader["Id"];
user2.UserName = (string)reader["UserName"];
user2.LastPost = (DateTime)reader["LastPost"];
// fully filled
User user3 = new User();
user3.Id = (int)reader["Id"];
user3.UserName = (string)reader["UserName"];
user3.LastPost = (DateTime)reader["LastPost"];
user3.PostCount = (int)reader["PostCount"];
user3.JoinedOn = (DateTime)reader["JoinedOn"];
// etc...
So my question is this. Is there a better method or pattern that accomplishes something like this for the 3 different states of the User object above. I have already determined that I don't want to create a separate object for each state of this object.
And performance is really important so minimizing the database calls and the data returned is very important. The DAL doesn't have to be enterprise grade pattern or practice, because this is a Web 2.0 initiative where most of the objects requested are going
to be the smaller objects that only have a limited amount of data. And there are only a handful of them with very minor reliance on each other.
I'm not totally getting why you'd want to partially fill an object, leaving some properties null or default values. There's no point having an object and just not setting the properties. It won't save much in terms of performance and it'll make the object
more difficult to work with (think NullReference Exceptions and default values mingled in with your real data...).
I would use inheritance, and create SmallUser, PartialUser : SmallUser and a User:PartialUser classes. Although you mention that you don't want to create seperate classes for each. That would save some performance by excluding the stuff you don't want completely
from the class.
You could create 3 seperate interfaces - ISmallUser, IPartialUser : ISmallUser, IUser : IPartialUser with the appropriate public properties on them and have the User class implement IUser. By passing the appropriate interface around instead of the actual
User class you could avoid accessing something you're not supposed to with that particular type of 'filledness' of the class. You still wouldn't be saving much in terms of performance but it would make a partially filled User object easier to work with (by
programming against the appropriate interface and knowing exactly what you're dealing with instead of encountering a whole bunch of default value properties and NullReference Exceptions).
You could create 3 seperate interfaces - ISmallUser, IPartialUser : ISmallUser, IUser : IPartialUser with the appropriate public properties on them and have the User class implement IUser. By passing the appropriate interface around instead of the actual
User class you could avoid accessing something you're not supposed to with that particular type of 'filledness' of the class. You still wouldn't be saving much in terms of performance but it would make a partially filled User object easier to work with (by
programming against the appropriate interface and knowing exactly what you're dealing with instead of encountering a whole bunch of default value properties and NullReference Exceptions).
Honestly the above was an example. The actuality of what we are talking about is the same object one with 3 fields and the other with about 20 fields and a couple supporting collections pulled from another table. So there is a real performance hit if I
just need the three columns. However most if not all of the methods in this object require the 3 fields in the smaller of the two data results, so it would be a pain to try and dual code for two different objects. We considered an interface and coding two
objects and a supporting state class with all the methods. But that seemed clunky at best.
We actually considered the interface option and it is still on the plate of possible solutions and seems like the best idea for now given the different views we need of the object.
Really I was just looking for a sanity check in this forum.
nberardi
Star
11233 Points
2352 Posts
DAL Pattern Question
Jan 30, 2008 01:22 PM|LINK
This may seem like an odd question but bare with me.
How do you fill the same data at varying degrees with the least amount of objects?
For instance to the left of this post you see a small representation of my "User Object" that shows only the most basic information. However when you click my user name you go in to my profile which contains this information plus some. So assuming that ASP.NET Forums uses the same "User Object" for both of these different views of the same model and only fills the appropriate information when requested. How would you go about programming this? And is there currently a pattern or model that gets me almost there?
Nick
TATWORTH
All-Star
72415 Points
14017 Posts
MVP
Re: DAL Pattern Question
Jan 30, 2008 01:40 PM|LINK
>So assuming that ASP.NET Forums uses the same "User Object" for both of these different views of the same model and only fills the appropriate information when requested
Somewhat unlikely - I would expect different objects for each in oder to maximise speed.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
ParrotBoy
Contributor
2379 Points
487 Posts
Re: DAL Pattern Question
Jan 30, 2008 02:13 PM|LINK
I agree, it would be very unlikely to have an object that loads in data as it is requested as it would inevitably be slower than just loading all the data at once. I generate all my business entities using codesmith templates and in general I will have an object which allows updates and loads all the information for each table and a read-only object for each view. This way I could create a view named "SimpleUserInfo" for use in places like the right hand of this page, but if I wanted to update a User I would have to use the full-featured User object.
Visit my blog
TATWORTH
All-Star
72415 Points
14017 Posts
MVP
Re: DAL Pattern Question
Jan 30, 2008 02:33 PM|LINK
There a very cases where a lazy load feature is either require or desirable. I can only think of one project in 5 years of Dot Net that needed it.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
ParrotBoy
Contributor
2379 Points
487 Posts
Re: DAL Pattern Question
Jan 30, 2008 02:44 PM|LINK
I assume that this was an object which loaded data from many different locations?
Visit my blog
TATWORTH
All-Star
72415 Points
14017 Posts
MVP
Re: DAL Pattern Question
Jan 30, 2008 03:11 PM|LINK
>I assume that this was an object which loaded data from many different locations?
A very astute assumption, however in this particular case it was a single SQL2000 database row and one column was a large text datatype, so this was only loaded on demand as the load took 1-2 seconds instead of being sub-second for the other values. In the general case, however you are perfectly correct. (Strictly speaking you are correct insofar as SQL 2000 will store large text values separately from the rest of the row.)
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
Mercury082
Participant
980 Points
156 Posts
Re: DAL Pattern Question
Jan 30, 2008 09:49 PM|LINK
I have used lazy-loading a little in the past and I use deep-loading often. It's really useful if you have a large object-model with alot of relationships that aren't always necessary. NetTiers handles deep-loading fairly well (with a patch applied to stop stack overflow exceptions) - you can pass in an object and specify the types that you wish to deepload.
eg.
User usr = UserService.GetById(1);
UserService.DeepLoad(usr, typeof(Address));
Address userAddress = usr.AddressSource;
nberardi
Star
11233 Points
2352 Posts
Re: DAL Pattern Question
Jan 31, 2008 02:28 AM|LINK
I think I actually gave you guys the wrong impression. My question wasn't about lazy loading each property individually. I already know the performance problems with that.
My question was essentially in creating a couples states of the object. For example:
So my question is this. Is there a better method or pattern that accomplishes something like this for the 3 different states of the User object above. I have already determined that I don't want to create a separate object for each state of this object. And performance is really important so minimizing the database calls and the data returned is very important. The DAL doesn't have to be enterprise grade pattern or practice, because this is a Web 2.0 initiative where most of the objects requested are going to be the smaller objects that only have a limited amount of data. And there are only a handful of them with very minor reliance on each other.
Please let me know if you have any suggestions.
Mercury082
Participant
980 Points
156 Posts
Re: DAL Pattern Question
Jan 31, 2008 03:46 AM|LINK
I'm not totally getting why you'd want to partially fill an object, leaving some properties null or default values. There's no point having an object and just not setting the properties. It won't save much in terms of performance and it'll make the object more difficult to work with (think NullReference Exceptions and default values mingled in with your real data...).
I would use inheritance, and create SmallUser, PartialUser : SmallUser and a User:PartialUser classes. Although you mention that you don't want to create seperate classes for each. That would save some performance by excluding the stuff you don't want completely from the class.
You could create 3 seperate interfaces - ISmallUser, IPartialUser : ISmallUser, IUser : IPartialUser with the appropriate public properties on them and have the User class implement IUser. By passing the appropriate interface around instead of the actual User class you could avoid accessing something you're not supposed to with that particular type of 'filledness' of the class. You still wouldn't be saving much in terms of performance but it would make a partially filled User object easier to work with (by programming against the appropriate interface and knowing exactly what you're dealing with instead of encountering a whole bunch of default value properties and NullReference Exceptions).
nberardi
Star
11233 Points
2352 Posts
Re: DAL Pattern Question
Jan 31, 2008 09:54 PM|LINK
Honestly the above was an example. The actuality of what we are talking about is the same object one with 3 fields and the other with about 20 fields and a couple supporting collections pulled from another table. So there is a real performance hit if I just need the three columns. However most if not all of the methods in this object require the 3 fields in the smaller of the two data results, so it would be a pain to try and dual code for two different objects. We considered an interface and coding two objects and a supporting state class with all the methods. But that seemed clunky at best.
We actually considered the interface option and it is still on the plate of possible solutions and seems like the best idea for now given the different views we need of the object.
Really I was just looking for a sanity check in this forum.