tags:

views:

409

answers:

3

I have a repository (CustomerRepository) that retrieves data from the data layer. Most of the business logic is in the entity class (Customer) that the repository either accepts or returns. However, where do you put the global entity business logic (that applies to all customers). For instance, I may not want to return all customers to certain users. I don't want to put that logic in the repository.

Thanks

Dan

+3  A: 

Wrap it behind a service.

Robert Munteanu
What does that even mean in this case?
C. Ross
I sort of think of services as a place for everything else in the domain model.
Min
+1  A: 

Put it in another repository (BusinessRuleRepository) and have CustomerRepository use it.

OR

If the business logic is only limiting the results a user can see you might want to use a Facade pattern with a factory. In this case you would have an ICustomerRepository that handles your CustomerRepository and LimitedCustomerRepository (which might encapsulate CustomerRepository), and a CustomerRepositoryFactory that returns the appropriate ICustomerRepository for the user.

C. Ross
A: 

I agree with Robert Munteanu.

Basically you roll up your business logic that isn't inherent in your model into a middle tier. The middle tier is the business tier / business objects / business logic layer / etc, but is just referred to as a service tier. It doesn't have to be a webservice, its a broad use of the term service in that it aggregates functionality of a specific application area.

You would basically have a CustomerService class that contains the repository reference. Your presentation layer would reference the service layer class.

There is an additional distinction that can be made as a guess from your name you are using .net, and probably using LINQ to SQL as your repository as outlined in NerdDinner.

The Repository typically returns IQueryable to the service layer, allowing the service layer chain together multiple queries to build different result sets. The Service then evaluates the expression using ToList or another similar method and returns that to the presentation layer.

blu
The MVC Storefront by Rob Conery is what turned me on to the repository pattern but unfortunately he has refactored the service layer out without really explaining that decision in the videos. So I was concerned that perhaps that wasn't the best way.
Dan dot net
I was faced with a similar issue, is this the best architecture? I stopped worrying about interfaces with contracts on top of layer with factories and just focused on making it work. I find too often people are way over-architecting solutions. Is the purpose to build this moment of a codebase, a tribute to its own mightiness and perfectness of layers and tiers? I think as long as you keep your data stuff in your repository, your "business logic" in your business services, and your UI stuff in your Views you are good to go. Accomplish what the software is supposed to do, refactor as you go
blu
and accomplish the goal of the software.
blu