2

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.

Maxim Polishchuk
  • 334
  • 6
  • 17
  • until I know, with fluent nhibernate you just configure the mapping with database and not validation. You map a not nullable field like this: `Map(x => x.Name).Not.Nullable();`. You could post some code to we understand more about. – Felipe Oriani Jul 26 '13 at 12:38

2 Answers2

2

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();
Firo
  • 30,626
  • 4
  • 55
  • 94
  • I.e. in your case User repository fills Id and Name properties only, other properties will be empty/by default. Right? – Maxim Polishchuk Jul 26 '13 at 13:35
  • I never implemented a repository on top of NH. i use ISession as my repository. LoginUser is a special class for one use case which only has the properties it needs, ie Id and Name. The query shown is used as is where the LoginScreen is built. – Firo Jul 26 '13 at 14:14
  • Ok. Just one question. Sorry, but I want to understand. Your example above create query to User database table in order to get Id and Name values, and create new LiginUser dto. This query (to database table) will contain Id, Name columns only or not? I.e. it's like - Select u.Id, u.Name From [dbo].[User] u ??? – Maxim Polishchuk Jul 26 '13 at 14:52
  • yes it will only select the values need in the projection ( Select() method ). You can also select on referenced entities `user.Foo.SomeProperty` – Firo Jul 26 '13 at 15:16
  • FYI: `.Query(...)` is an extension method. You need to import the namespace `using NHibernate.Linq;` to be able to access it. – Krisztián Balla Aug 01 '15 at 13:53
0

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.

Jury Soldatenkov
  • 427
  • 1
  • 6
  • 12