EF 4 with POCO in class library as MVC 2 model

眉间皱痕 提交于 2019-12-03 22:53:30

问题


I am exploring Entity Framework 4 with POCO as my Model for an MVC2 web app. The reason I'll need the model and data access code in a separate library is so I can share it with another web application that serves as a portal for our clients to access our data.

My question is, will I lose any of the typical MVC2 Model features by having my EF4 and POCO code in another project? Or maybe another way to ask this is will the MVC framework be able to work with the Model layer just the same if it is in a separate project versus in the Models folder?

Edit: I forgot to mention that I am using POCO rather than the generated classes you normally get with EF for each entity.


回答1:


Single assembly projects only for small projects

I never liked "Models" folder anyway. I rather put separate layers/tiers in separate assemblies. MVC project template just makes a layered application in a single assembly.

I prefer solutions made of several projects:

  • Web - front end web application ie. Asp.net MVC (references Services and Objects)
  • Services - business tier assembly (references Data and Objects)
  • Objects - POCO classes, interfaces etc. that are shared all over (doesn't reference any other solution project)
  • Data - The actual EF model and extensions, repositories etc (references Objects)

This is a much more controllable and structured way if your application is larger than just a few trivial screens.

So in a past project: EF model was in the Data project (as well as repositories etc), and POCOs where in Objects. All communication between projects was done with classes from Objects project.

So Web used Services,
which in turn used repositories (in Data),
which in turn called EF to manipulate data.
All data passing back and forth was using POCOs in Objects.

So repositories had to transform entities to actual POCOs (they were not 1:1) when returning data and creating entities when there were write operations. And this transformation was done via extension methods on entities, so transformation was always done in a single method, which made it simpler to maintain when we either changed an entity or a POCO class. We didn't have to scan all the code for all conversions, we just adjusted our ToPoco() extension method (they were actually called like this).



来源:https://stackoverflow.com/questions/3478964/ef-4-with-poco-in-class-library-as-mvc-2-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!