How to fill just required properties of entity (Fl

2019-07-17 04:27发布

问题:

I am using FluentNHibernate to access to my database. I would like to implement next - just required properties of my entity should be filled. By example, in one case all properties should be filled, in second case repository should return entity with ID and Name properties only. Does it make sence?

I see point when I can implement a few mappings for entity - every mapping according to case. Then I get a few ISessionFactory'ies - repository uses required ISessionFactory to cover required case. Hmm.. but I am not sure that it is correct solution.

回答1:

just create specialised DTOs Viewmodels for each scenario and select directly into them

using NHibernate.Linq;

var user = session.Query<User>()
    .Where(user => user.Name == someName)
    .Select(user => new LoginUser(user.Id, user.Name))
    .FirstOrDefault();


回答2:

What are you looking for is called 'projections', but projection object should be different object, not the entity itself. NHibernate supports projections out of the books. Have a look at docs.