Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros/cons of returning POCO objects from a Repository rathen than EF Entities?

Following the way Rob does it, I have the classes that are generated by the Linq to SQL wizard, and then a copy of those classes that are POCOs. In my repositories I return these POCOs rather than the Linq to SQL models:

return from c in DataContext.Customer
       where c.ID == id
       select new MyPocoModels.Customer { ID = c.ID, Name = c.Name }

I understand that the benefit of this is that the POCO models can be instantiated easier so this will make my code more testable.

I'm now moving from Linq to SQL over to Entity Framework and I'm about half way through an EF book. It seems there's a lot of goodness I'm going to lose out on by returning POCOs from my repositories rather than the EF entities.

I still haven't really embraced unit testing, so I feel like I'm wasting a lot of time creating these extra POCOs and writing the code to populate them, when all I appear to be gaining is testable code, yet I'm also gonna lose out on a lot of the benefits of the EF by not being able to track my objects.

Does anyone have any advice for a relative newb to all this ORM/Repository stuff?

Anthony

like image 603
littlecharva Avatar asked May 11 '09 08:05

littlecharva


People also ask

Should I use EF core Repository pattern?

TL;DR – summary. No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn't useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn't helpful.

Is EF a Repository?

EF is both Repository and UoW.

What is Repository in Entity Framework?

The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.

Is the Repository pattern dead?

That is, it's dead if you are using Entity Framework Core. If you're still using straight ADO.NET, or even just dapper, then the repository pattern still probably makes sense for you.


1 Answers

Another reason people don't like the auto-generated objects (in LINQ to SQL for example) is because of their built-in "magic".

Usually the magic is invisible and you never notice it, but when you try to do things like serialize one of those objects and then deserialize it (for example when using web services) its internal connection to the data source is broken and special hacks need to be employed to "put the magic back in".

With POCOs, you don't have to worry about those sorts of things and can get a better separation between your data and service layers. The downside of course is that you have to write lots of boring POCO -> magic object and magic object -> POCO conversion code. But in the end I think it's usually worth it, especially for large or complex projects.

like image 180
Eric Petroelje Avatar answered Oct 01 '22 18:10

Eric Petroelje