How to use EF Core in a N-Layer application

Olivier Richard 21 Reputation points
2022-06-06T15:49:53.913+00:00

Hi !

I don't know if there is some official best practices for this, but I need a bit of help. We have an application split into 3 layers : Data Access Layer (DAL), Business Logic Layer (BLL) and Presentation Layer. In other applications, we use nHibernate, but we were asked to switch to EF Core and we struggle a bit.

With nHibernate, the DAL would return an IQueryable list and we would construct the query in the BLL. For exemple, I need to retrieve a list of objects by name, we would call the DAL to get an IQueryable list of all objects and with a Where clause Where(x => x.Name == name). With EF Core, we have to call the DbContext inside a using statement (to be sure it will be disposed correctly), so our DAL can't return us an IQueryable list, we get an error when we manipulate the collection in the BLL.

I am trying to find out if it is okay to just call the DbContext directly in the BLL instead, and all our DAL would contain would be the DbContext and migrations.

Thank you all for your help

Developer technologies | .NET | Entity Framework Core
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. dramacool 1 Reputation point
    2022-06-21T18:12:56.703+00:00

    Bruce idea is quite good and would be helpful. If it is possible, I would recommend going one step further to create a service layer that would interact with the BLL.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.