I'd like to ask some questions about Fluent NHibernate as I'm developing in it almost a year but still there are some things which I don't get.
First questions is about entities as the title says. Why do I make them virtual?
A senior developer told me, that when I have a list of other objects in another one, I should initialize it 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 is that I have a warning "Virtual member called in constructor" - I googled that but didn't really understood the problem with this. And the second part of question is: do I MUST initialize this list? Maybe I don't MUST but it's a good approach: if so, why?
Third question is about mappings, this is my way for above class:
public class CategoryMap: ClassMap<Category> { public CategoryMap() { Id(x => x.Id); Map(x => x.Name); HasMany(x => x.Equipments); } }
And in some examples I have seen a one line of code:
Table("Category");
When do I need to specify the table?
----EDIT Thanks for the answer, but now I have to clear something more, about the lazy loading. I read this: http://www.codeproject.com/Articles/652556/Can-you-explain-Lazy-Loading
And you can see a class Customer with private field _Orders and a public getter of that object (which also initialize the _Orders list). Can you please tell me if this is a better approach than mine? If yes, how should I change my code? (fields are virtual because of Fluent NH.)