How to fill just required properties of entity (FluentNHibernate)

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:29:12

问题


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.



来源:https://stackoverflow.com/questions/17881410/how-to-fill-just-required-properties-of-entity-fluentnhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!