【问题标题】:Can I use an interface as a DTO in NHibernate?我可以在 NHibernate 中使用接口作为 DTO 吗?
【发布时间】:2013-11-22 21:03:35
【问题描述】:

我正在减少通过查询通过线路传输的字节数,在我写作时...我意识到我应该能够将接口传递给 QueryOver 对象并获取该接口属性的指定列。

是否可以将接口传递给 QueryOver 对象的选择或类似命令?它会只返回“映射”到界面的列吗?

例子:

Repository
    .QueryOver<MyTable>()
    .Select(table => table as IJustWantTheseColumnsInterface)
    .Execute(Parameters);
//or

Repository
    .QueryOver<MyTable>()
    .Select<IJustWantTheseColumnsInterface>()
    .Execute(Parameters);

//...

public class Table : IJustWantTheseColumnsInterface
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
    public Phone Phone { get; set; }
    public DateTime BirthDate { get; set; }
    public Occupation Occupation { get; set; }
    public Employer Employer { get; set; }
    //etc...
}

public interface IJustWantTheseColumnsInterface
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Phone Phone { get; set; }
}

【问题讨论】:

    标签: c# nhibernate dto modularity


    【解决方案1】:

    您为什么不能只创建一个仅映射到这些列的第二个实现?

    public class Table : IJustWantTheseColumnsInterface
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string MiddleName { get; set; }
        public virtual string LastName { get; set; }
        public virtual Address Address { get; set; }
        public virtual Phone Phone { get; set; }
        public virtual DateTime BirthDate { get; set; }
        public virtual Occupation Occupation { get; set; }
        public virtual Employer Employer { get; set; }
    }
    
    public class SameTable : IJustWantTheseColumnsInterface
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual Phone Phone { get; set; }
    }
    
    public interface IJustWantTheseColumnsInterface
    {
        int Id { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        Phone Phone { get; set; }
    }
    
    public class SameTableMap : ClassMap<SameTable>
    {
        public SameTableMap()
        {
            Table("Table");
            Id(x => x.Id, "ID");
    
            Map(x => x.FirstName, "FIRST_NAME");
            Map(x => x.LastName, "LAST_NAME");
    
            Reference(x => x.Phone, "PHONE_ID");
        }
    }
    

    您也可以将接口用作 ClassMap 的 T。

    如果Phone 属性是Table 类中的IPhone 接口类型。您可以像这样指定要返回的实现。

    public interface IJustWantTheseColumnsInterface
    {
        ...
        IPhone Phone { get; set; }
        ...
    }
    
    public class SameTableMap : ClassMap<SameTable>
    {
        public SameTableMap()
        {
            ...
            Reference(x => x.Phone, "PHONE_ID").Class(typeof(Phone));
            ...
        }
    }
    

    现在如果你想得到你的部分实体

    IJustWantTheseColumnsInterface someVariable = session.Get<SameTable>();
    

    【讨论】:

    • 这是一种解决方法,不是答案,请评论问题。这一行包含构建查询所需的所有信息,而且写得非常简洁... Repository.QueryOver().Select().Execute(Parameters);
    • @JPowell 我不知道这一点,所以我回答认为我的回答是唯一的方法。很感谢您找到了如此简洁的答案,因为我将来一定会自己使用它。
    • @furrier 我为错误的沟通道歉,我的评论并不像假设的那么清楚。所写的行确实包含所需的所有信息。我正在研究应该如何编写这一行(NHibernate 是否内置了它)以及如果没有使用扩展方法的可能性。
    • 我从未见过 NHibernate 的这种语法至少。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2011-05-14
    • 2015-02-09
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2015-11-21
    相关资源
    最近更新 更多