c# - Fluent NHibernate creating entities - why virtual -


i'd ask questions fluent nhibernate i'm developing in year still there things don't get.

  1. first questions entities title says. why make them virtual?

  2. a senior developer told me, when have list of other objects in one, should initialize in constructor, example:

    public class category {  public virtual int id { get; set; }  public virtual string name { get; set; }   public virtual ienumerable<equipment> equipments { get; set; }   public category()  {      equipments = new list<equipment>();  } } 

    the first thing here have warning "virtual member called in constructor" - googled didn't understood problem this. , second part of question is: must initialize list? maybe don't must it's approach: if so, why?

  3. third question mappings, way above class:

    public  class categorymap: classmap<category> {   public categorymap()   {     id(x => x.id);     map(x => x.name);     hasmany(x => x.equipments);   } } 

    and in examples have seen 1 line of code:

    table("category"); 

    when need specify table?

----edit answer, have clear more, lazy loading. read this: http://www.codeproject.com/articles/652556/can-you-explain-lazy-loading

and can see class customer private field _orders , public getter of object (which initialize _orders list). can please tell me if better approach mine? if yes, how should change code? (fields virtual because of fluent nh.)

  1. properties need virtual nhibernate can substituted proxies (required lazy-loading)
  2. you don't have that.however, practice initialize collections in constructor don't null in code. example , consider following code

    var category = session.get<category>(1); //without c-tor line throw exception collection null var totalequipments = category.equipments.count(); 

    so either initialize in constructor or keep writing this

    var totalequipments = (category.equipments == null) ? 0 : category.equipments.count() 
  3. this specify entity category should mapped table named category. can used if entity name doesn't match default name or make clear want store entities (see fluent-wiki details on other attributes)


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -