My current architecture is based around my entities being simple containers for data with little or no logic within them. Any business decisions are made by Service classes which take entities as arguments and then return data back. However, I'm finding that this approach is becoming a little unwieldy as our systems grow in s开发者_StackOverflow中文版ize.
For example, we have an ASP.NET MVC web application which displays a list of products - some of these products may be available to order by the current user and some may not. In our controller class we fetch the products for display and then as each product is displayed on the page we have to call out to one of our service classes and determine whether this product is available to order for this customer.
<% For Each p In Model.Products %>
<%= Html.Encode(p.Title) %>
<% If AuthService.CanProductBeOrderedByUser(p, Model.CurrentUser) Then %>
<a href="#">Click here to order</a>
<% End If %>
<% Next %>
The problem is that this requires logic to be built into the page and also requires a call out to a service for each iteration. If I also need this information in more than one place it requires a lot of duplication.
Does anyone have comments on the above or any guidance as to how to improve this?
Thanks
James
An example could be a ViewModel-hierarchy where a VM for a Product would have the Property IsOrderable.
How this property came to be true or false is irrelevant for the view.
Business Services are handful when you have cross-entity operations (which involve entities of different types). If your business operation is strictly related only to one entity, it is better IMHO to put this operation into the entity class.
Let your controller peform a customer dependent filtering on the Products list, let the view just show them. This can be achieved in many ways: Product's extension function, a ProductService or data repository parametrized query.
I cannot recommend enough using CSLA for smart Entities. I, like you, used to use data-only objects for holding my data, but since swapping over to CSLA things have gotten better and better.
It is a very sophisticated free framework with a HUGE user base.
It supports:
- Centralized business logic
- Transactions
- Up to field level security
- Up to field level validation
- Automatic IsDirty functionality
- Understands readonly/editable/child/parent data objects and relationships
- Multiple servers
Check it out here:
Official Web Site
and the "getting started" book here:
eBook
精彩评论