views:

431

answers:

3

I have been working with ASP.NET MVC for a couple of months now and I'm still not happy with the layout of my project's solution. I am trying to construct a mid-sized website CMS that is as portable and reusable as possible and there are some obvious problems in the design of it. I am looking for some advice regarding how I should structure my solution in consideration of separation of concerns. I've found a similar question here, but it doesn't really target some of the issues I am facing.

Right now this is how my solution is laid out:

+Project.Controllers - All Controller classes
P+roject.Controllers.Tests

+Project.Core - Utility classes including repetitive tasks and some configuration handlers (this project needs to be better fleshed out)
+Project.Core.Tests

+Project.Models - Model classes, Entity Framework context, and Repository classes
+Project.Models.Tests

+Project.Web - All Views and Content

One major thing I am currently missing is a place to stick my business logic and I feel I've been wrongly placing business logic in my repository classes as well as intermingling it in controller actions. Obviously, I'm very aware of this problem, I'm just not sure where I should placing my business logic in that solution layout. Does my solution structure need to change or can I safely keep that business logic in my Models project? Also, I really don't like that my EF Context is in the Models class, but I don't know of a way to isolate the data layer code from the Entity Classes needed in my model.

How is everyone else laying out their production ASP.NET MVC solutions?

+1  A: 

Personally I'm only learning MVC. My experience comes from ASP.NET WebForms but I would go with the layout proposed in the link you gave. The second answer, that is:

  • Models
  • Views
  • Controller
  • Services
  • Tests - one for each project.
Finglas
No that would be your models. Services aka web services would be consumed by the model though so I suppose you can say that in a way.
Finglas
Alright that makes sense.
Nathan Taylor
+3  A: 

You might want to check out the layout the S#arp architecture project uses or the onion architecture as used in the Code Camp Server MVC reference application. Both projects have had allot of effort put into them by different people to get a good sepperation of concerns in the context of asp.net MVC and domain driven design.

olle
Where would I stick my Entity Framework context in the S#arp architecture?
Nathan Taylor
I don't know the entityframework so take this with a grain of salt. S#arp arch uses nhibernate for data access. What you can do is make the Repository require the ef context in it's constructor and then inject it using a Inversion of Control container. http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/ contains some sample code how to add a lazy loading mechanism keeping the context in the HttpContext once created for a single request. Although also nhibernate specific sample it shouldn be very different in EF.
olle
A: 

I would take EF Context and Repositories out of Models and into a data access layer, Project.Data and put your business objects in Project.BusinessLogic (?).

This gives the benefit of putting the two assemblies (Project.Data and Project.BusinessLogic) in other apps you might build on the same domain. That means your next project has a very useful starting point.

Hope that helps,

Dan

Daniel Elliott
If I move the EF Context into a new project then my Entity Classes would move with it, is there any way to combat this?
Nathan Taylor
You would reference your BusinessLogic in your model.
Daniel Elliott