2

Ok, So I have been taking in all sorts of great information about MVC, EF4, Repository Pattern, UoW, Service Layers, etc and now I am going to attempt to put it all together.

My question is, How should these be separated?

I was thinking projects like this:

1) Application - MVC App
2) Repository Layer 3) Entities - EF4/Partial Entity Classes

Any suggestions would be great!!

Thanks, Sam

1
  • 1
    This is a great question for those of us new to the MVC pattern and wanting to gain more knowledge of proper architecture. I am looking forward to the answers Commented Feb 18, 2011 at 18:03

3 Answers 3

5

Here's one example of how you could structure your application:

  1. Domain Models (Primary POCO objects)
  2. Repositories (Implementation of some data access technology depending on the project requirements : EF, NHibernate, LINQ to XML, Remote web service calls, ...)
  3. Service (business operations aggregating multiple CRUD operations into a business operation that will be exposed with the domain objects)
  4. ASP.NET MVC application (Controllers, Views, View Models, Mappers between the Domain objects and the view models)

This layers could represent a physical separation (assemblies) or logical separation into the same assembly.

Sign up to request clarification or add additional context in comments.

3 Comments

Would you happen to have a sample I could look at? I am little confused on 1 & 2. Could you elaborate on those a bit? Also, the Service layer, is it working with EF generated objects? Are the domain objects the EF generated objects? - Thanks!!
@Sam Striano, here's one sample MVC project structure I wrote (no service layer => controllers call the repository): github.com/darind/samplemvc . Let me try to elaborate: the service layer is not working with EF generated objects. It is working with domain models you have defined in 1. EF objects are polluted with EF specific stuff (unless you use Code First in which case your EF Code First models become point 1.). The model shouldn't be related to any data access technology you are using. For me autogenerated EF classes should never leave the boundary of their respective layer.
In your sample application structure, where does the EF fit in at, and where is the service layer? - Thanks!!
3

Don't separate code physically until you have a physical reason to do so such as deployment or code sharing reasons. Use folders and namespaces.

1 Comment

Creating separate projects is really trivial to do when you start a project. It enforces good separation in your code (no cross dependencies between layers) and reduces a potential refactoring headache down the road. I don't see any reason not to do this up front. Plus if you're doing TDD it's less code to constantly rebuild thus speeding up your development cycle/not interrupting your thought process as often.
3

I've just been through this myself and by far the best approach i found is S#arp Architecture.

Excellent templates generate the project scaffolding for you and they have a good explanation why u really do want physical project separation. Good argument here. Theyre tutorial shows how TDD with this model is a breeze.

The beauty of this model is the separation of concerns it offers so if you did want to use EF, its a snip to swap out NHibernate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.