views:

1188

answers:

3

Hello,

In my ASP.NET MVC web application, I have:

  • Domain Model, created by LINQ to SQL

  • Repositories such as

    UserRepository and OrderRepository

  • IQueryable Fluents as IQueryable Extension Methods such as

    public IQueryable<Order> GetNewOrders(this IQueryable<Order>)

  • Services such as

    UserService and OrderService

  • Utility Classes and Extension Methods such as

    CryptoUtility (doing Hashing etc.) and String etc. extensions

  • ViewModels which are special for each MVC View

  • The ASP.NET MVC project itself (Controllers, Views)

I'm looking for the best project structure/organization for my case, especially separating into different assemblies and how dependencies should look like between these layers. Web resources unfortunately don't go into good detail about this.

One hint: Currently Repository, Services, IQueryable Fluents etc. work directly against domain model implementation, I don't have an interface definition for them. I considered it unnecessary but maybe this is needed for loose coupling? My Services have an interface (e.g. IOrderService), and my repositories implement IRepository<T>.

Appreciate your input on organizing this in a concise manner, and especially what layer should depend on what & assembly organization. Thank you!

A: 

You might want to check out s#arp architecture to see how they structure things. It uses NHibernate and their repos are kinda directly tied to them so you'll need to modify it.

Chance
+1  A: 

A couple of different projects do go in more detail about this (but mind you it also takes some effort to really understand how all the different parts work together)

olle
Code camp server is a great example.
Ryan Montgomery
+3  A: 

I would look at the article by Jeffrey Palermo on the Onion Architecture here. This basic architecture works well in any project and will allow you to separate your core project (domain layer, persistence, etc.) from your web project.

We use this with MVC/StructureMap/FluentNHibernate and have had great success.

We end up having a structure similar to the one below.

> trunk
  + build (build scripts)
  + lib (external libraries)
  > src (source code)    
   >> Organization.App (solution name)
     >> Organization.App.Core (code library)
        + Config
        > Domain
          > Model
          > Persistence
          > Queries
          > Services
        > Persistence
        > Services
     >> Organization.App.Web (mvc web app)
        > Assets
          + Images
          + Scripts
          + Stylesheets
        + Controllers
        + Views
        + ViewModels

That's the basic idea. The web app references the core app for domain entities our repository/unit of work. Check out this older project on google code for a similar example. The great part about this is we have been able to add new "UI" project types to the same solution and re-use our core project as intended. Like a console app or a second web app, or whatever you need.

Ryan Montgomery
Where do you put services? in the Core project?
Alex
You can. I like that to keep the number of dlls we need to build to a minimum. But you could put it in a separate project. I added it to the tree for you. We also have the notion of domain services so those would go under the domain folder/namespace. This would be a service that only works with domain entities though. The services folder you see under the project would be a "application" service in that it might have repositories injected as well as other application services to do work. It depends. :)
Ryan Montgomery