Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nHibernate joining multiple tables and using AliasToBean Transformer

Tags:

nhibernate

I have a very basic need to get some data from the database and return a DTO. I found that joining multiple tables using nHibernate and "projecting" so to say, to a DTO to be quite a bit of code. After looking at several examples, most which didn't work leaving me a DTO with null values, I cam up with the following and was wondering if you nHibernate ninja's out there could tell me if there is a better way.

public IOpenIdUser GetOpenIdUser(string claimedIdentifier, IOpenIdUser openIdUserDto)
    {
        User user = null;
        OpenIdUser openIdUser = null;
        Profile profile = null;
        UserType userType = null;


        return
            SessionWrapper.Session.QueryOver(() => user).JoinAlias(() => user.Profiles, () => profile).
                JoinAlias(() => user.OpenIdUsers, () => openIdUser).JoinAlias(() => user.UserType, () => userType)
                .Where(() => user.UserName == claimedIdentifier)
                .SelectList(l => l
                                     .Select(x => openIdUser.OpenIdUserId).WithAlias(() => openIdUser.OpenIdUserId)
                                     .Select(x => user.UserId).WithAlias(() => openIdUserDto.UserId)
                                     .Select(x => openIdUser.OpenIdClaimedIdentifier).WithAlias(
                                         () => openIdUserDto.ClaimedIdentifier)
                                     .Select(x => openIdUser.OpenIdFriendlyIdentifier).WithAlias(
                                         () => openIdUserDto.FriendlyIdentifier)
                                     .Select(x => openIdUser.OpenIdEndPoint).WithAlias(
                                         () => openIdUserDto.OpenIdEndPoint)
                                     .Select(x => user.UserName).WithAlias(() => openIdUserDto.UserName)
                                     .Select(x => userType.Type).WithAlias(() => openIdUserDto.UserType)
                                     .Select(x => profile.DisplayName).WithAlias(() => openIdUserDto.DisplayName)
                                     .Select(x => profile.EmailAddress).WithAlias(() => openIdUserDto.EmailAddress)
                                     .Select(x => openIdUser.DateCreated).WithAlias(() => openIdUserDto.DateCreated)
                                     .Select(x => openIdUser.LastUpdated).WithAlias(() => openIdUserDto.LastUpdated)
                                     .Select(x => openIdUser.UsageCount).WithAlias(() => openIdUserDto.UsageCount)
                ).TransformUsing(Transformers.AliasToBean<OpenIdUserDto>()).Future<OpenIdUserDto>().Single();
    }

This method sits in my UserRepository and is called by my UserService. Please not that this actually works, I just think it is overkill for such a simple task. Also please note that I am new to this so if this code is crappy I apologize in advance.

like image 525
CrazyCoderz Avatar asked Feb 10 '12 15:02

CrazyCoderz


1 Answers

If you use Queryover then this is the only way.

If you really think less lines of code are preferable, more intuitive, or sits better with you then you can do either:-

  1. Create a DB view and create mapings files for the view with mutable="false" in your class definition and use protected set; on your class properties
  2. Use the LINQ provider instead e.g. .Query (see this blog post for more info)
like image 198
Rippo Avatar answered Nov 08 '22 19:11

Rippo