views:

360

answers:

2

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.

+1  A: 

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.

b_richardson
I added a bit more clarification if this helps...
Greg
+1  A: 

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.

marc_s
That best practice gets you fired in any team doing object orientation. The data layer IS the entity framework runtime - not your business objects. entity objects, properly coded, live in the business layer.
TomTom
TomTom - when you say entity object do you mean "entity framework objects" do you? Do you mean that you should build your domain model in the EF designer and call these the business objects, then use EF to map these to database tables.
Greg
@TomTom - how would you approach it?
Chris Stewart