Search code examples
c#3-tier

3-Tier - Models reusing?


I am creating an application that has those three different projects:

ApiService (Web API 2)
BusinessLogic (Class Library)
DataAccess (Class Library)
  • ApiService has a reference to BusinessLogic
  • BusinessLogic has a reference to DataAccess

DataAccess uses Entity Framework with code first approach so it contains the models for the database tables.

My question is, what is the best approach or best practice regarding the models for Business and Service Projects?

I have read that Service project should not be using the models of the DataAccess project, so where should I create that models, in Service or in Business?

Thanks in advance.


Solution

  • Separate BL(Business logic)/Presentation layer models from DAL(Data access layer) models always.

    Add one more layer between them which will do the mapping, use Automapper or somethogn custom. So when you are passing data to DAL models will be mapped to entity models, and when BL is getting the data from DALsame thing, map entity models to BL models,

    Why?

    The way how you are persisting your data in your database may be rather different from how you are going to present it to the user. The data may have to be obtained from several entities, joined by relationships, constructed at run time again by joining from other tables, etc. How you are going to present it to a user may be simplified and different than it is persisted, so you can hide that complexity which is needed for the database.