简体   繁体   中英

Can I use an interface as a DTO in NHibernate?

I was reducing the bytes transferred over the wire via my query and as I was writing... I realized that I should be able to pass an interface to the QueryOver object and get the specified columns for that interface's properties.

Is it possible to pass an interface to a select or similar command for the QueryOver object? Would it return just the columns that are "mapped" to the interface?

Example:

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; }
}

Why can't you just create a second implementation which you map to only those columns?

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");
    }
}

You can also use an Interface as T for ClassMap.

Also in case the Phone property was of type interface IPhone in your Table class. You could specify which implementation to return like this.

public interface IJustWantTheseColumnsInterface
{
    ...
    IPhone Phone { get; set; }
    ...
}

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

Now if you want to get your partial entity

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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