Best Approach for Sharing an Entity Framework Model Between Application Tiers

I want to create an asp.net application that has several layers (classic layer design): business layer and presentation layer. The data layer seems outdated as EF does all the work.

Therefore, when I create an EF model in Business Layer, I cannot use entities at the presentation level because I cannot add data annotations for display and validation, etc. (especially the display attribute is usually part of the presentation layer). And it seems to me not very good to create copies of all the data in similar classes "viewmodel" in the view layer.

So, is there a good approach for creating an object context at the business level and having a common β€œcontract” - an assembly for objects? Most of the samples I found put everything in one single assembly, which, in my opinion, is not the best approach for more complex applications.

+4
source share
4 answers

You must abstract EF from your business layer, never use such structures directly. I usually create a common repository interface and a common EF repository (this is your data layer) that implements the interface. The IoC framework will take care of implementing the correct implementation at runtime.

, ViewModels . , , , . , , .

- , ViewModels. AutoMapper .

- /.

+4

.

  • EF
  • -, EF.
  • , -, EF-,

EF - . , - -.

+2

(DTO), , , . automapper DTO.

+1

.

**SM.MVCApp**  :-  This is for MVC application 
**SM.Service** :- This layer is for holding the business logic (Interface & Class)
**SM.Data**    :- This layer is to interact with database using Entity framework (Repository & UOW)  
**SM.Entity**  :- This layer is responsible for having property in class that will be mapping with the table of database.

SM.Service example

public interface IStudent
{
    IQueryable<Entity.Student> GetAllStudent();

    Student GetStudentByID(Int32 StudentID);

    void CreateStudent(Student objStudent);

    void UpdateStudent(Student objStudent);

    void DeleteStudentvoid(Student objStudent);

    void SaveChanges();

    List<Entity.Student> SearchStudent(string Name, int Age, string EmailAddress, string CountryName);

    void MakeRelation(Entity.StudentCourceMap objMap);
}

Now it shows How to create an SM.Data layer p>

  • First, create a DBContext class to create the database and tables.
  • Then create an IRepository * Repository class for crud operations
  • Then create a UOW (Unit of Work)
0
source

All Articles