views:

138

answers:

1

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.

+3  A: 

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).

Robert Koritnik
so in your Objects project you have an interface for each POCO? Great explanation by the way.. Do you have a sample app using this implementation you can share?
JBeckton
@JBeckton: I tend to keep things reasonable and maintainable. There were no interfaces directly related to every POCO. No. Since POCOs were not 1:1 with EF it means they were manually written. So where interfaces where needed. **Second**: No, unfortunately I don't have such public code to share. I can only dispense the knowledge and experience.
Robert Koritnik
@Robert Koritnik: Actually i were using exactly same architecture but with L2S as my ORM and everything is OK. But when i tried to convert my code to use EF i've serious problem with "ToPoco()" extension i got "LINQ to Entities does not recognize the method" ... "this method cannot be translated into a store expression.". So please tell me how did you make this work ??!
Wahid Bitar
@Wahid: Your `ToPoco()` is called before you actually retrieve your data. I always get relevant data using something like `ToList()` or `FirstOrDefault()` etc. Then when I have data entities I'm able to easily call `ToPoco()` extension on them. The error you're getting is actually saying that your query is trying to parse (and covert to TSQL) your `ToPoco()`...
Robert Koritnik
Thanks Robert:)
Wahid Bitar