Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 Code First - What pattern should I use?

I am learning EF Code First and I am struggling a bit with what patterns to use in my application. I have read many conflicting sugestions and arugments some stating you should use the Repository pattern while others say doing so is redundant, which I tend to agree.

Here is my delima:

Suppose I am building a REST Web Service that is going to allow me to manage customers. This service will allow me to add customers, delete customes, and edit customers, and find customers.

Should I:

A.) My question comes down to where should my business logic go. Should I have a CustomerManager class that provides Add, Edit, Delete, and Find methods that take in a Customer entity? Should my validation logic go in those methods?

B.) Should I use an Active Record style of development when my Customer entity would have Save(), Delete(), and Find() methods on it withall validations login being done inside of the Customer class?

C.) Should I do some type of hybrid, where simple validation logic is on the entity itself. This could be done through code first attributing. I could also have a simple save method on the entity. Then, I could do complex business validation logic, deletes(), finds(), and multi-entity saves in a CustomerManager class?

I kind of lean toward option C. In the past I have typically used Manager/Service classes keeping my entities pretty simple. However, since code first does entity property validation on the entity level, it seems like maybe all simple entity validation should go there.

I realize this could be somewhat of a religious topic, but I would like to get some other options on what would be the best way to put together a solid application.

like image 659
Justin Avatar asked Nov 13 '22 19:11

Justin


1 Answers

EF 4.1 Code first combine unit of work with data mapper pattern.

So, I would not recommend use active record pattern.

Repository pattern with entity framework is common solution. If you want some simple validation logic, you can use DataAnnotations which works with entity framework well.

Here is simple example of implement repository pattern with EF :

http://www.efekaptan.com/repository-pattern-with-entity-framework-code-first-4.1

like image 105
Mennion Avatar answered Jan 10 '23 15:01

Mennion