0

Say I have a Visual studio solution with two projects: one web project, and one data project.

Does it really matter whether I pass the EF object from the data project to the web project, or do I need to explicitly define POCOs in the data project to pass to the web project?

It seems to me that needing to create POCOs instead of simply using EF objects adds yet another thing that needs to be done...and I don't particularly see the purpose.

tereško
  • 58,060
  • 25
  • 98
  • 150
Cody
  • 8,686
  • 18
  • 71
  • 126

2 Answers2

1

If you're using Entity Framework 4, the EF objects are POCO objects, and so should be used in any situation where your Model matches your EF object (CRUD operations are the typical ones). However there will more than likely be situations whereby the standard POCO object doesn't encapsulate all of the fields that are needed for a View Model (typical ones I have this with are account pages where you have two password fields) and so you will need to create a Model for the page, which then passes the data into your EF POCO objects.

So if you're using DbContext (which creates POCO objects for you) there is no reason not to use those objects.

Ryan Amies
  • 4,902
  • 1
  • 21
  • 36
  • I think I'm on the same page as you. Just to clarify, I generally pass only the EF objects - but if more data IS needed for the view I will create a POCO which contains those EF objects, along with the additional data. Is this the same thing you're saying? – Cody Oct 27 '12 at 17:52
  • Yes this is exactly what I mean, don't be afraid to make lots of POCO ViewModels, as this will just cause you problems down the line. I've had a situation where we tried to use an EF object to capture data, but split over several pages (like a wizard, step 1,2 etc), in the end we had to scrap a lot of work because trying to bend the EF objects to do something they're not meant for is just a headache – Ryan Amies Oct 28 '12 at 07:38
0

It's not a good practice to pass the EF objects directly to the web project. As the state changes occurring in the EF objects can directly reflect on the database. Because of this you should explicitly define POC objects for web project.(we can call this as Data Model or simply Model. The POC object used for retrieving data from the View can be called as View Model. In appropriate situations you can use the same POCO class as a Data Model as well a View Model)

amesh
  • 1,311
  • 3
  • 21
  • 51