views:

244

answers:

2

I'm struggling with understanding Entity Framework and POCO objects. Here's what I'm trying to achieve.

1) Separate the DAL from the Business Layer by having my business layer use an interface to my DAL. Maybe use Unity to create my context.

2) Use Entity Framework inside my DAL.

I have a Domain model with objects that reside in my business layer. I also have a database full of tables that doesn't really represent my domain model. I setup Entity Framework and generated POCO objects by using the ADO.NET POCO Generator extension. This gave me an object for each table in my database. Now I want to be able to say context.GetAll<User>(); and have it return a list of my User objects. The User object is in my business layer. Is that possible?

Does that make sense or am I totally off and should start over? I'm guessing I need to use the repository pattern to achieve this, but I'm not sure.

Can anyone help?

A: 

In general, with EF, the way to get all of the objects in your context as a List<T> would be something like:

context.Users.ToList();

But since Users would be an IEnumerable in your context anyway, you can do virtually any kind of iteration or querying directly on context.Users

AllenG
Ok, but how do I get my EF ObjectContext to know about my User domain object and then get it to populate my User object from the POCO it created from the database table? It already created a POCO called TBL_USER, but I don't want to use that object, I want to be able to use my User object in my business layer.
Dan H
@Dan, there may be frameworks that do this for you, but if so, I'm not familiar with any. Based on that, I would create a method on my user type that takes an object of TBL_USER and converts to your BL type. Populate a list with your created objects.I'm not sure, however, that's the best answer. If you're already moving from a DB to an Entity POCO object, you may want to look at your problem-space to be sure you really then need an extra business object...
AllenG
I'm not sure I need my extra business objects now. But I still have the issue of EF creating the POCO objects and they reflect my tables in my database, I want my business objects to reflect my Domain Model. Know what I mean? Maybe I need to look closer and see exactly how the POCOs are created and see if they would work.
Dan H