thanks Jason!  Nice explanation!

2010/1/28 Sam Kimmel <[email protected]>:
> Thanks again Jason.  Very helpful information. Will post back to this
> thread once I've had a chance to test everything out.
>
>
> On Jan 28, 8:41 am, Jason Meckley <[email protected]> wrote:
>> inline responses
>>
>> On Jan 27, 6:24 pm, Sam Kimmel <[email protected]> wrote:
>>
>> > Jason, thanks for the reply.  You had me until "it's a good practice
>> > to build view specific DTOs."  Wasn't quite sure what you were talking
>> > about in this paragraph.  Could you elaborate or point me to some
>> > resources on the subject?
>>
>> you have a view (webpage, windows form) this view will display
>> specific information. the information may, or may not match up 1:1
>> with your domain model. So if you have a form that shows a list of
>> users to select from, you would want there id and display name. the
>> user domain object may have numerous other members as well. by mapping
>> the User domain object to a UserDisplayName object (specific to the
>> view) your view is only aware of the id and displayname properties.
>> This approach is very helpful when you have rich domain model. if your
>> domain model is basically properties which reflect the database
>> tables, it's not nearly as useful and may be overkill.
>>
>>
>>
>> > Also, you mentioned that there is no lazy loading for detached
>> > objects. Can I perform a simple read to populate it, then detach and
>> > have it available?  Example: Access User.Roles, then detach.
>>
>> var user = session.Get<User>(id);
>> session.Detach(user);
>> var roles = user.Roles; // will be empty because there is not lazy
>> loading at this point.
>>
>> instead you would need to either access the roles collection before
>> detaching. or eagerly load them.
>> var user = session.Get<User>(id);
>> user.Roles;
>> session.Detach(user);
>> var roles = user.Roles; // will not be empty
>> ... or ...
>> var user = session
>>       .CreateCriteria<User>()
>>       .Add(Restrictions.IdEq(id))
>>       .SetFetchMode("Roles", FetchMode.Join)
>>       .UniqueResult<User>();
>> session.Detach(user);
>> var roles = user.Roles; // will not be empty
>>
>> > Most of my objects are just POCOs with getters and setters. Would you
>> > still recommend DTOs in all instances, or only when the object has
>> > embedded domain logic?
>>
>> View specific DTOs may be overkill in this instance. For me personally
>> I still map domain objects to specific view models simply to know
>> exactly what information my view is using. Even when my domain model
>> is simple properties.  This also helps be avoid select n+1 issues
>> because all my data is mapped before sending information to the view.
>> if I do have an n+1 issue it's easy to pin point where.
>>
>>
>>
>> > Finally, if I were to go the DTO route, when I do this:
>> >    user.Name = userDTO.Name;
>> > Does my user object get marked dirty if the Name values are the same?
>> > If so, can it be avoided without doing something like this:
>> >    if (user.Name != userDTO.Name)
>> >       user.Name = userDTO.Name;
>>
>> I think NH is smart enough to realize the property is not dirty.
>>
>>
>>
>>
>>
>> > Will be doing plenty of testing tonight. Thanks again Jason. Very
>> > helpful.
>>
>> > On Jan 27, 3:59 pm, Jason Meckley <[email protected]> wrote:
>>
>> > > The problem is you are using the session bound entity outside the
>> > > scope of the session. You need to detach the entity from the current
>> > > session before sending across the wire. When you send the entity back,
>> > > you will need to attach/merge it to the current session. (each request
>> > > should have it's own session). You will need to load any data you need
>> > > on the client before detaching from the session. In other words, once
>> > > you detach there is no lazy loading.
>>
>> > > I would consider a different approach though.
>> > > reading data:
>> > > 1. get the domain object
>> > > 2. map the domain object to a DTO (AutoMapper makes this real simple)
>> > > 3. pass the DTO over the wire
>> > > writing data:
>> > > 1. send a DTO from the client to the service
>> > > 2. load the domain object from session
>> > > 3. update domain object
>> > > When utilizing this approach it's good practice to build view speific
>> > > DTOs. So for every read/write form you would have 2 DTOs. One for
>> > > displaying the data and a second for modifying the domain object. This
>> > > approach will lead to a lot of view(read)/command(modify) specific
>> > > DTOs, but it's easier to add/modify individual views because the
>> > > view's Model isn't shared by multiple views. It's also easier to see
>> > > what a view does because the view model will only contain properties
>> > > it needs.
>>
>> > > On Jan 27, 4:24 pm, Sam Kimmel <[email protected]> wrote:
>>
>> > > > What are the best practices for using NHibernate in web services (or
>> > > > in any n-tier pattern) where you pass your object out of the scope of
>> > > > the session to the client, have them modify it, then send it back for
>> > > > persistence?
>>
>> > > > Simple Example with User object:
>>
>> > > > * Client calls service to request existing User.
>> > > > * Service opens session, gets User, returns it to client, closes
>> > > > session
>> > > > * Client modifies User, then sends it to the service to persist the
>> > > > updates
>> > > > * Service opens a session, validates object, persists to database,
>> > > > then closes session
>>
>> > > > Some questions...
>>
>> > > > 1) If the client sets a property on the User object outside the scope
>> > > > of the session:
>> > > >       a) Is it marked as dirty?
>> > > >       b) If so, is it marked as dirty if they assign the same value
>> > > > that already existed?
>>
>> > > > 2) In the service, what is the best way to obtain the previous state
>> > > > for the User? If I attempt to open another copy of the object I get an
>> > > > error saying that the object already exists in the Session. If I try
>> > > > to use the method outlined here (http://nhforge.org/wikis/howtonh/
>> > > > finding-dirty-properties-in-nhibernate.aspx), I get null errors when
>> > > > attempting to unproxy.  The only thing I can think of is to user 2
>> > > > sessions: Get a copy of the original in the first, then check the
>> > > > values in the second.  I'm guessing there is a better way or that I am
>> > > > missing something.- Hide quoted text -
>>
>> > > - Show quoted text -- Hide quoted text -
>>
>> - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/nhusers?hl=en.
>
>



-- 
Paulo R. Quicoli

Editor Técnico - ClubeDelphi Magazine - DevMedia

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to