c# - Fluent NHibernate creating entities - why virtual -
i'd ask questions fluent nhibernate i'm developing in year still there things don't get.
first questions entities title says. why make them virtual?
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?
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.)
- properties need virtual
nhibernatecan substituted proxies (required lazy-loading) you don't have that.however, practice initialize collections in constructor don't
nullin code. example , consider following codevar 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()this specify entity
categoryshould mapped table namedcategory. can used if entity name doesn't match default name or make clear want store entities (see fluent-wiki details on other attributes)
Comments
Post a Comment