1

I am using FluentNHibernate with AutoMapping and conventions to create my database. I have 3 tables - Person, Address and Job as shown below.

    //Database table
  public class Person()
  {
    public virtual int Id { get; set; }
    public virtual string FirestName { get; set; }
    public virtual string SecondName { get; set; }

    public virtual PersonAddress Address { get; set; }
    public virtual PersonJob Job { get; set; }
  }

  //Database table
  public class PersonAddress()
  {
    public virtual int Id { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string AddressLine3 { get; set; }
  }

  //Database table
  public class PersonJob()
  {
    public virtual int Id { get; set; }
    public virtual int Description { get; set; }
  }

I want to display a list of Person details in a Grid using a flattened view model below. Do I have to create a Mapping for ViewModelPerson even though its not a database table (will Automapping affect this) or should I do this using a nHibernate query to create get the list of ViewModelPerson entities.

  //NOT database table - only used as ViewModel
  public class ViewModelPerson()
  {
    public virtual int PersonId { get; set; }
    public virtual string FirestName { get; set; }
    public virtual string SecondName { get; set; }

    //AddressLine1 + AddressLine2 + AddressLine3
    public virtual int AddressId { get; set; }
    public virtual string Address { get; set; }

    public virtual int JobId { get; set; }
    public virtual string Job { get; set; }
  }
gisWeeper
  • 501
  • 6
  • 18

1 Answers1

1

Let me give you my view, the approach I do believe in...

Data Layer - The mapping Entity vs Table is a Data layer related. It is just a way, how to later profit from ORM tool like NHibernate. All the CRUD operations are generated for us - reflecting the mapping.

Presentation Layer - The ViewModel, should help us to "map" the Entity(-ies) to Client View. So while in many simple scenarios will: Entity, Table, ViewModel .. (e.g. Country or Currency) match 1-1, we are ready to handle it differently in more complex scenarios.

The above scenario Displaying Person, Address, Jobs at one View - is a complex one. Can we profit from separation, indirect filling of the ViewModel?

Suggestion: Fill the ViewModel on a Service layer using queries to underlying Business model

The first benefit I see, is that the population of the ViewModelPerson is in our hands. It is not fixed inside of the mapping. We can reduce amount of data (even use Projections to load less data) and we can even extend it (load City from some CodeList)

The second benefit is that if we will need to change something, e.g. introduce async loading of the Addresses, we have to change it only from the Service layer UP (in MVC Controller, Model and View) while the data layer (mapping is untouched)

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thanks for the response - I intended to fill the ViewModel on the Service layer I just wasnt sure about what the best query or method. I've done it using a normal nHibernate query and then using AutoMapper to populate my ViewModel from the query results. Ideally I'd have liked to use Projections but I'm not experienced in using them and the query was getting a little to complex for me. For example the concatenating of Address in ViewModel was problematic. – gisWeeper Feb 06 '14 at 10:20
  • I see. But with NHibernate there is no other option then try it, learn it ;) These are the best links I can give you to start to experiment *(see below)*. And once you start to play with queries, projections, transformers - do **not hesitate to ask here** , and I am sure you will get help. So: *15. Criteria Queries* - http://nhforge.org/doc/nh/en/index.html#querycriteria and even more type safe *16. QueryOver Queries* http://nhforge.org/doc/nh/en/index.html#queryqueryover. Also check this *19.1.5. Using batch fetching* http://nhforge.org/doc/nh/en/index.html#performance-fetching-batch – Radim Köhler Feb 06 '14 at 10:22
  • I mean, you'll for sure get help here, if you have a query and do not know how to move on... A general tutorial is most likely out of scope here. I guess you know what I mean. You can check http://stackoverflow.com/a/20970816/1679310 or http://stackoverflow.com/a/19287008/1679310 and also this Q&A to see the power of query over http://stackoverflow.com/questions/20528760/ – Radim Köhler Feb 06 '14 at 10:30