Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern with multiple tables

Recently I have been reading up on using the repository pattern and DI to help create easily testable code, I think I understand it for the most part. However I'm having difficulty with one issue. I need to create a Rules object for my applications business layer. To create a rule, I need the ability to read and write to two tables. How would you go about implementing a repository that uses two tables for one object?

for example:

ICollection<type> GetAllRules();

What would I put in for type as it requires two tables?

Thanks

Steve

like image 778
Steve2056726 Avatar asked Jul 29 '13 14:07

Steve2056726


People also ask

When should we use Repository pattern?

The Repository pattern makes it easier to test your application logic. The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.

What problem does repository pattern solve?

The Repository Pattern is used to simplify the Application Layer and is defined by the Application Layer . The repositories evolve with the Business Use Cases . The Repository handles domain objects and is agnostic of the technical details. Generics can still be used to implement the repositories.

What is the benefit of repository pattern in Entity Framework?

Benefits of Repository PatternIt centralizes data logic or business logic and service logic. It gives a substitution point for the unit tests. Provides a flexible architecture. If you want to modify the data access logic or business access logic, you don't need to change the repository logic.


1 Answers

I wouldn't insist on having a repository for that.

As Fowler says

Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer.

This is probably why most implementations tend to expose pure domain objects rather than derivatives (which your Rule object seems to be).

I would have two repositories for the two tables you mention, then I would have a unit of work to expose all repositories and then I would have a business layer service responsible for the compound processing.

An advantage of such approach would be that the repository layer remains clean, there is no business processing involved here, no unclear rules introduced to the persistence layer.

like image 157
Wiktor Zychla Avatar answered Sep 18 '22 13:09

Wiktor Zychla