Currently I am working on implementation of the following concept: I want 
to write automapper that would allow me to map my DTO objects to my  Domain 
objects without loading full domain object from the database. Let’s 
consider  the following example to understand what I am talking about.

 

If in my domain I have the following classes:

         public class User

        {

            public virtual Int64 Id { get; set; }

            public virtual String Email { get; set; }

            public virtual UserProfile UserProfile { get; set; }

        }

 

        public class UserProfile

        {

            public virtual Int64 Id { get; set; }

            public virtual String Address { get; set; }

            public virtual ISet<BlogPost> Posts { get; set; }

        }

 

        public class BlogPost

        {

            public virtual Int64 Id { get; set; }

            public virtual String Title { get; set; }

            public virtual String Content { get; set; }

        }

 

This tables are mapped to the following DB tables:

<https://lh3.googleusercontent.com/-YGT9By7XQqg/UHJ5mLOccwI/AAAAAAAAAAg/Wo5WsL_Ah3Q/s1600/Untitled.png>

 

In my application layer I use the following DTO objects:

 

   public class UserDTO : IMappableDTO<User>

        {

            public String Email { get; set; }

 

            [Map("UserProfile.Address")]

            public String Address { get; set; }

 

            public IList<BlogPostDTO> Posts { get; set; }

        }

 

        public class BlogPostDTO : IMappableDTO<BlogPost>

        {

            public String Title { get; set; }

        }

 

IMappableDTO interface and Map attribute are my custom classes and they are 
used internally in my automapper to show correspondence between DTO objects 
and domain objects and also show correspondence between properties in those 
objects. (Also there are some attributes that allows to map single property 
in DTO to several properties in domain object and execute some user-defined 
mapping functions, but all this stuff is not relevant to my actual 
question) 

 

In my application façade I can get list of my UserDTO objects using the 
following syntax. 

 

var users = Mapper.Map<UserDTO>();

 

(There is also some staff to be able to filter result set, but it is also 
not relevant to the actual question)

 

For now I use Criteria API to generate request to database. When Map method 
is called something like the following request is send to database:

 

SELECT 

       Users.Email,

       UserProfiles.Address,

       UserProfiles.Id

FROM   Users 

      INNER JOIN UserProfiles ON Userprofiles.Id = Users.UserProfileId

 

Using my criteria I join all tables that I need and select only those 
properties that I need. But how can I handle assotiations? How can I 
initialize BlogPostDTO collection in the same way that I do with initial 
UserDTO collection. 

On DB level I would like my request to look something like the following:

 

SELECT 

       BlogPosts.Title

FROM   BlogPosts

WHERE BlogPosts.UserProfileId in (1, 3, ...) /*here comes the list of all 
ids that we select in the previous query*/

 

Is there any way to build such query having a list of ids? 

 

I already spent some time reading NHibernate sources and trying to solve 
this problem in a different way. I end up with a few solutions but neither 
of them satisfiers me:

 


   - .       I can load whole objects from database and don’t use 
   projections in my criteria and then use fetch.Subselect and load all 
   blogposts in a single roundtrip to database. But it is  bad solution, 
   because I don’t actually want to load whole BlogPost Content, if I only 
   need to list titles.
   - .       In my query I can join not only UserProfiles table but also 
   BlogPosts table and then filter my results and do something like 
   DistinctRootEntry transform does, but it is also bad for performance 
   reasons because if my DTO would have many collections of other DTOs it 
   would result in a query with lots of joins and with a Cartesian product of 
   entities in all of this collections.
   - .       I can force developers, that would use my automapper to put in 
   BlogPost class reference to UserProfile and specify name of this reference 
   in mapping attribute, then I can easily build query I asked you about and 
   get all BlogPosts in a single request.  But this solution is bad because it 
   add unnecessary restrictions on how you should build your domain objects, 
   also it is bad because it makes it difficult for developers to use my 
   automapper and it would require different mapping attributes for handling 
   one-to-many and many-to-many assotiations.
   - .       I can write something like the following:

var c = s.CreateCriteria<BlogPost>();

var collectionCriteria = DetachedCriteria.For<UserProfile>()

    .Add(Restrictions.In("Id", /*here would be list of all ids*/))

    .CreateAlias("Posts", "posts")

   .SetProjection(Projections.Property("posts.Id"));

 

c.Add(Subqueries.PropertyIn("Id", collectionCriteria));

 

but then I would have unnesesary join with UserProfiles table in WHERE 
clause of my query and it is also bad for performance.

 

Please, advise me, how can I solve this problem. I would be very grateful 
for any help.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/kiHO-UcKlhwJ.
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