question

OlivierRichard-0675 avatar image
0 Votes"
OlivierRichard-0675 asked dramacool-7884 answered

How to use EF Core in a N-Layer application

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

dotnet-csharpdotnet-entity-framework-core
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You might consider the following post and consider a different approach.


1 Vote 1 ·

Thank you for the link ! It was really informative. If we forget about the use of IQueryable, is it good practice, in your opinion, to call the DbContext directly from the Business Layer ?

0 Votes 0 ·
Bruce-SqlWork avatar image Bruce-SqlWork OlivierRichard-0675 ·

as DbContext is already a repository pattern, you can replace the DAL with a custom DbContext. I agree you should not expose IQueryable. Only the BLL can call the DbContext, and it should not expose any entity models. The BLL should have adapters that use business logic to call the data layer.

Typically you would host the BLL as a webapi, and call from the UI layer.


2 Votes 2 ·
Show more comments

1 Answer

dramacool-7884 avatar image
0 Votes"
dramacool-7884 answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.