views:

36

answers:

1

I am new to the MVC way of programming so please bear with my basic question !

I have a Status class with a default constructor (in an ASP.NET MVC application).

public Status()
{
    this.DatePosted = DateTime.Now;
}

I noticed Fluent NHibernate calls this constructor each time it fetched a list of existing Status objects from the database. Hence, the constructor does not seem like the right place to initialize the date.

Where should I move this initialization ? Moving it to the Controller (Add action of Status controller) also seems to violate the principle that the Controller should not make any business decisions. Should I move it to the Status DAO then ? (In traditional ASP.NET Web Form applications I worked with, a DAO simply accepted a business object and saved it to the database and did not contain any logic)

I would like to know the right way to accomplish this. Is there another layer I am missing here where this initialization should take place?

+3  A: 

I noticed Fluent NHibernate calls this constructor each time it fetched a list of existing Status objects from the database. This does not seem right

This is exactly what is supposed to be happening. Why wouldn't an ORM call the default constructor for an object? I think every hand rolled DAL and ORM in the world would trigger DatePosted to be reset because thats just how constructors work.

Your DatePosted property should probably set via ModelBinding or manually in the controller and not be part of a constructor.

jfar
@jfar, I'm sorry. My sentence is messed up. I mean't it is not right that the DatePosted is initialized for existing status objects. I understand Hibernate needs to do it. Sorry for the confusion :-o Is it acceptable to set values in the controller though ? I'm going to read up on ModelBinding.. thanks for the pointer..
Preets