简体   繁体   English

nHibernate连接多个表并使用AliasToBean Transformer

[英]nHibernate joining multiple tables and using AliasToBean Transformer

I have a very basic need to get some data from the database and return a DTO. 我非常需要从数据库中获取一些数据并返回DTO。 I found that joining multiple tables using nHibernate and "projecting" so to say, to a DTO to be quite a bit of code. 我发现使用nHibernate连接多个表并“投射”,所以说,DTO是相当多的代码。 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. 看了几个例子,大多数没有用的东西给我留下了一个带有空值的DTO,我想到了以下内容并且想知道你是否nHibernate ninja可以告诉我是否有更好的方法。

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. 此方法位于我的UserRepository中,由我的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. 另请注意我是新手,所以如果这段代码很糟糕,我会提前道歉。

If you use Queryover then this is the only way. 如果您使用Queryover,那么这是唯一的方法。

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; 在类定义中使用mutable="false"创建数据库视图并为视图创建映射文件,并使用protected set; on your class properties 在你的类属性上
  2. Use the LINQ provider instead eg .Query (see this blog post for more info) 使用LINQ提供程序代替例如.Query (有关详细信息,请参阅此博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM