Where to put business rules for querying when using NHibernate?
Still new to NHibernate. I'm using NHibernate 2.1.2 and the Linq provider.
I am wondering where I should put my business rule logic. For example, I have an entity called Service with DateTime property. In my web app, I o开发者_Python百科nly ever want to display/work with services whose DataTime is less than 4 weeks away. Where should I put that rule?
I started out building a ServiceRepository with a IEnumerable GetServices() method that encapsulated that logic, thinking "Oh! Everytime I need a service, I'll go to that method!"
Of course, problems show up when I need to load child objects of my services. I don't want a lot of permutations of the same method in my repository when querying my database. In fact I really don't want to use repositories at all, because I'm trying very hard to heed Ayende's Advice, since I'm still new to NHibernate.
I like the idea of having objects that specify what I need from the database, and I could encapsulate the business rule within it. The linq part of it is what is throwing me off, I think.
You don't need additional methods to "load child objects". You can rely on relationships and lazy loading for that.
With that, your original design is valid.
Have a look at Fabio Maulo's Enhanced Query Object. This demonstrates a nice way to encapsulate NHibernate queries while allowing for a great deal of flexibility in how they are implemented.
It will good if you mention your business logic in BLL and your project structure must be like :
BLL(Business Logic Layer). it contains interface, services, models
DLL(Dynmic link libery). it contains Repsitory, Nhibernate mapping class
3.Website. (Add BLL,DLL refernce )
精彩评论