How can I share the auto-generated entity data model (generated object classes) amongst all layers of my C# web app whilst only granting query access in the data layer? This uses the typical 3 layer approach: data, business, presentation.
My data layer returns an IEnumerable<T>
to my business layer, but I cannot return type T to the presentation layer because I do not want the presentation layer to know of the existence of the data layer - which is where the entity framework auto-generated my classes.
It was recommended to have a seperate layer with just the data model, but I'm unsure how to seperate the data model from the query functionality the entity framework provides.
If you use POCO entities (.NET 4+), then this is easy (or at least easier). Is that a possibility?
You can create DTOs as Ben said, but then you're basically dumbing down and duplicating each of the entities. EF2 will create the "dumbed down" entities and dynamically add change tracking, lazy loading, etc. if you wish.
Otherwise the answer is you can't. If the entities depend on the Entity Framework, then you can't use them throughout your application without dragging that dependency along. In that case you have to use DTOs. Here's a 3rd party option for EF 1 or EF 2 without POCO entities.
http://automapper.codeplex.com/
Edit: Here are some useful links to learn more about all this:
- General MS guidelines:
http://msdn.microsoft.com/en-us/library/bb738470.aspx
- POCO templates:
http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx
- POCO templates, including how to
move to separate project:
http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx
- POCO proxies:
http://blogs.msdn.com/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
- How to split up model:
http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx
- Employee Tracker sample application
(layers, unit tests, mocking,
repository, etc.):
http://code.msdn.microsoft.com/ef4/Release/ProjectReleases.aspx?ReleaseId=4279
You could create DTOs from your data entities and pass your DTOs to the rpesentation layer.