0

Project Architecture is like this:

  • MyProject.DataAccessLayer: will communicate with database using Entity Framework

  • MyProject.BusinessLayer: application business logic will be here

  • MyProject.API : WebAPI ApiControllers will return the data from business layer methods

My question is that, if I have created an Entity Framework as separate project in the DataAccessLayer, how can I use the model classes from the DataAccessLayer?

Should I add a reference to MyProject.API to access the model classes? If this is the case, why would I need the MyProject.BusinessLayer?

Edited

Sample code for business layer:

  public async Task<List<TigerNest.Dao.Area>> GetArea()
          => await yatraDao.GetArea();

ApiController:

YatraManager manager = new YatraManager();
var result = await manager.GetArea();
return Ok(result);

In the ApiController, the BusinessLayer returns a collection of areas. It forces me to add reference to the Dao project.

4
  • 1
    The business layer has a reference to the DataAccesslayer, and the API has a reference to that business layer project. If you then need to access your DAL models from your API, you're not correctly using the business layer. Commented Mar 6, 2018 at 17:41
  • I have updated question with sample code Commented Mar 6, 2018 at 18:03
  • You don't have to update the code. It's exactly like @nbokmans said. API controller has a reference to the business layer, the business layer has a reference to the DA layer. API (uses the )-> Business (uses the)-> DA Layer. Commented Mar 6, 2018 at 18:45
  • But in api, I return as list of areas, this area class available in da layer and it's entityframework generated class Commented Mar 6, 2018 at 19:30

1 Answer 1

1

If I have created Entity Framework as separate project in DataAccessLayer, how can I use the model classes from DataAccessLayer ?

You can put the model classes into an independent project infrastructure

Should I add a reference to MyProject.API to access the model classes?

Yes, because you need to return data from API

If this is the case why do I need MyProject.BusinessLayer ?

I suggest you might create a Myproject.BusinessLayer, because the business layer can place repetitive logic for later reuse.

We separate UI,BusinessLayer,DataAccessLayer those can reference with infrastructure project,infrastructure will use the model, container ..

enter image description here


Edit

In brief, the UI layer is referenced to the BLL and the BLL is referenced to the DAO,and they all refer to the Infrastrure (DTO, ViewModel) common layer

In your question you might mapping EF model be DTO model on BusinessLayer,ApiController can use the DTO model.

So ApiController and BusinessLayer refer a new project Infrastrure,that put DTO or ViewModel,This way you can isolate the EF Model.

DTO (data transfer object) is like ViewModel but he exists in data conversion. EF Model corresponds to DB schema,DTO corresponds to EF Model.

DTO model should contain only data and not business logic.

I recommend a third party kit AutoMapper,that helps us to map objects more easily.


There are some article talk about AutoMapper and 3-Tier-Architecture

Easily Generate Data Transfer Objects from ADO.NET Entity Framework or LINQ to SQL Data Classes

Mapping Entity Framework Entities to DTOs with AutoMapper

Sign up to request clarification or add additional context in comments.

5 Comments

How can put the Model classes as separate project, since Entityframework generate the autocode from Db ?
You can create a DTO object on separate project. Entityframework Db can mapping with it.
DTO (data transfer object) is like Model but he exists in data conversion. EF Model corresponds to DB schema,DTO corresponds to EF Model
Can you refer any article which you are talking about?
@King_Fisher I put some article on answer, you could search key work "3-Tier EF"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.