Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In separate data access & business logic layer, can I use Entity framework classes in business layer?

In separate data access & business logic layer, can I use Entity framework classes in business layer?

EDIT: I don't think I will need to swap out the data access layer from my business logic in the future (i.e. will be SQL Server), however I will for the UI layer. Therefore the question is more meant to be are there any major issues with using EF classes for me in the business layer? Seems like there would be less plumbing code.

like image 258
Greg Avatar asked May 16 '10 00:05

Greg


People also ask

What are the 2 types of databases in Access?

There are two kinds of databases: flat file databases and relational database. After you have a database with one or more tables, you can search for information with queries (questions) and can generate reports, all of which can be saved separately. In Access, a database is comprised of one or more tables.


2 Answers

Typically, the "best practice" approach would be something like this:

  • in your Data layer, you have EF entities that get loaded from and stored back to the database

  • in your Business layer, you have your own domain objects (just plain C# classes) that represent the data that your app needs to work on. Those can be more or less identical to a data layer entity, or they can contain several "atomic" entities to make up a business object, or they can be vastly different. To alleviate the need for a lot of left-hand-right-hand-assignment statements (to move property values back and forth between data layer entities and business layer objects), you should check out tools like AutoMapper that make it really easy to set up "mappings" between similar object types and allow you to easily assign those types back and forth

  • your UI layer(s) will then visualize and represent Business layer objects to the user for information and/or manipulation

The benefit is that your business layer domain model represents the actual business objects you're working with, and becomes more or less independent of how those are really stored in the data layer. Also, this avoids "glueing" your UI layer to a particular data access technology.

Of course - that's the recommended best practice for an enterprise-scale app, where you might need to swap out the data access layer etc. For a simpler app, you might skip those practices, knowing what you're doing, and that you're "locking" yourself into EF if you use EF entities all the way up through the business layer into the UI. If that's ok with you and your app scenario - there's no particular reason not to do it. EF entities are perfectly valid .NET object classes - they simply derive from a common base class (EntityObject instead of object) and they have a certain amount of "baggage" that comes along. But there's nothing stopping you from using those EF entities all throughout your app.

like image 107
marc_s Avatar answered Sep 27 '22 22:09

marc_s


As with all questions of this nature the answer is it depends. If you want a clear seperation of your data access logic and business layer I would say no. If that is what you are aiming for I would use a repository pattern and IoC to build the data access layer then you can swap in a stub DAL for unit testing purposes and then bring in the database access when you get to the functional testing.

like image 41
heartlandcoder Avatar answered Sep 27 '22 23:09

heartlandcoder