How to design Model class in Repository pattern?

420 Views Asked by At

Is it a must in repository pattern to create models for each database tables? or can I create a model that can be placeholder of two or more database tables?

1

There are 1 best solutions below

0
Amit Joshi On

In repository, there are multiple approaches to design the model depending on the need. So, better focus on business need. I will try to explain a bit.

Aggregate Root:

If you are doing Domain Driven Design, one entity per multiple tables is the approach generally taken. This is called Aggregate Root. Aggregate Root may contain multiple and nested Aggregates. So, this looks like some complex class hierarchy.

Benefit is that, your model become more friendly with your domain than your database. This truly helps persistence agnostic development. Primary focus of developer is shifted from persistence needs to domain instead.

Not part of your question though, this also helps taking most benefit from the full ORM if your using any.

This approach is generally over kill for small projects.

Model per Table:

This is very simple approach generally taken for small applications; specially, if you are not doing DDD. If your application is more a database oriented (like CRUD) than domain, then this approach is preferred. You may not be able to use some of the good features of full ORM with this like extensive mapping capabilities, lazy loading etc.