How to determine business object state - .NET, NHibernate
During developement process of my ASP.NET MVC + NHibernate application I came accross tough (for me) problem.
In my projects domain I have objects, lets call them "articles" (like on BBC website or something like this). This articles are described by few properties:
-Author -Creation date -IsTemp -IsApproved
These objects can be in few states:
- Temp - IsTemp == true, everything else is not important
- Approved - IsApproved == true
- Pending - IsTemp == false && IsApproved == false && CreationDate + 10 days > DateTime.Now
- Expired - IsTemp = false && IsApproved == false && CreationDate + 10 days < DateTime.Now
(Above description is just an example- my domain is a bit more complicated but the problem is identical)
And here is the question: When should I detect articles states? I want user to be able to list for exemple all Approved articles etc.
- Should I use background proccess to update state of all articles every 10 minutes? It's kinda smelly for me.
- Should I use my Article class (method GetState or something?) - then I loose ability to query states by 开发者_如何学GoHQL and I'm forcing to get all entities from DB in order to perform paging and discovering state
- Should I prepare state-specific queries? (Repository.GetAll(State.Temp)) - this one is also smelly because I don't want to my business rules (which can change/extend) to leak into my DAL layer.
- Should I extend extend NHibernate functionality? I never did that so I don't know if this is real.
This is a big problem for me. Any ideas?
Best regards, Karczas
Take a look at the state machine pattern as implemented for example here.
I would recalculate the status every time when one of the state-source-properties have changed.
So for example the IsApproved property-setter
would call the RecalculateStatus() method
.
'RecalculateStatus()' should only change the status if there is a change in status. this way you can safely call 'RecalculateStatus()' any time you like
精彩评论