Los Techies : Blogs about software and anything tech!

Organizing ASP.NET MVC solutions


Recently, a question came up on Twitter around your favorite project structure/solution organizing for ASP.NET MVC projects.  I’ve toyed around with quite a few different strategies for structuring projects, and I’m currently settled around one that gives me the most flexibility.  It’s extremely simple:

image

That’s it, two projects.  So what goes in each project?  Let’s first look at the UI project.  The UI project contains only website content and contains no code.  And I mean no code.  This includes:

  • No controllers
  • No models
  • No Global.asax.cs
  • NO CODE AT ALL

Why no code?  Because when the UI project contains only UI content and no code, my UI project now matches the deployment structure.  It contains:

  • Views
  • CSS
  • Images
  • Global.asax
  • Web.config
  • Reference to the Core project

Since my UI project structure matches my deployment structure, it makes it a lot easier to figure out how the deployment should work.  With controllers, models and so on in the mix, it becomes more difficult to see what gets included for deployment (the Content, Scripts and Views folder) versus what is left out.  We can then organize two different concerns separately: our content and our code.

So where does code go?  That’s the other project.  I call it “Core”, but it can be anything.  All code goes in one project, that includes our persistence model, view model, controllers, repositories, ORM mapping definitions, EVERYTHING.

Organizing the code

As far as organizing the code – I prefer keeping things simple.  If I had my druthers, the UI project wouldn’t compile at all – it would just be a folder, holding content.  Its “bin” folder would merely get populated by the output of the Core project.

Otherwise, I use folders to organize code.  Projects are okay for organization, but they’re pretty rigid and tend to lock you in to layers and structure that are quite, quite difficult to change.  I’ve already been burned a couple of times on large projects making a mistake in my project structure, and found that this approach tends not to scale.  I’ve even run into teams with upwards of 100 projects, with only a half-dozen actual deploy targets!  Remember, compile time is largely a function of the number of projects.  1000 files in a single project will compile much much faster than 100 files in 10 projects.  At least an order of magnitude faster in cases I’ve seen.

Another issue I ran into was the difficulty in re-organizing code if you’re locked into a project structure.  Besides just a sloooooooow Ctrl+F5 experience, it can be quite frustrating to realize you’re going to hose your entire source control log history because you want to move a file to a different project.  In one recent hair-pulling re-organization experience, we basically lost our entire log history because we couldn’t do basic source control commands for a re-organization.  This is exacerbated with source control systems like TFS which embed source control information in the actual project files.  In our case, we had to delete all of our projects and re-create them by hand.  Moving folders is waaaaaaay easier than dealing with projects and dependencies.  You will want to re-organize your code, in a major, major way at some point.

If you’re having problems with layering your codebase – projects are a great way of dealing with this problem.  It enforces dependencies quite nicely, basically forcing you to follow certain rules.  However, once you get to the point where you’re starting to create a “Common” project, a “Configuration” project, a “Mappings” project and so on, consider rolling things back up into one project.  Continuing down the project path will introduce friction in just basic everyday coding tasks, so at some point, it’s just not worth it.

So why not just do one UI project?  Put all the code in there, and get the absolute fastest and the most flexible experience?  Content structure is a completely different concern with completely different reasons for change than organizing code.  However you decide to organize your code, keeping the code out of the UI project ensures that you don’t mix code and content organization.

Kick It on DotNetKicks.com
Posted Dec 08 2009, 09:27 PM by bogardj
Filed under:

Comments

DotNetKicks.com wrote Organizing ASP.NET MVC solutions
on 12-08-2009 9:50 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Twitter Trackbacks for Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com wrote Twitter Trackbacks for Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com
on 12-08-2009 9:51 PM

Pingback from  Twitter Trackbacks for                 Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech!         [lostechies.com]        on Topsy.com

Jake Scott wrote re: Organizing ASP.NET MVC solutions
on 12-08-2009 10:14 PM

How do get around the default page issue? This nasty code:

string originalPath = Request.Path; HttpContext.Current.RewritePath(Request.ApplicationPath, false);

IHttpHandler httpHandler = new MvcHttpHandler();

httpHandler.ProcessRequest(HttpContext.Current);

HttpContext.Current.RewritePath(originalPath, false);

ASP.NET MVC Archived Buzz, Page 1 wrote ASP.NET MVC Archived Buzz, Page 1
on 12-08-2009 11:10 PM

Pingback from  ASP.NET MVC Archived Buzz, Page 1

Paul Batum wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 12:49 AM

"NO CODE AT ALL"

What about JavaScript?

Arnis L. wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 2:51 AM

I'm not structuring it this way (UI project) but I agree fully about preferring folders. That's the same reason why tests shouldn't 'mirror' project structure. Saw recently security assembly which had 2 tiny classes. I could live with managing dependencies but i can't peacefully watch how build get's damn slow with no good reason.

What are reasons you use internal development server instead of IIS?

I see IIS more 'production environment' like. And there is no need for waiting on firing up dev server.

Kornelije Sajler wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 3:02 AM

And how about Controllers from MVC2 Area, how they could be added to Core, maybe with same structure, I could try!

Nice post!

Reflective Perspective - Chris Alcock » The Morning Brew #494 wrote Reflective Perspective - Chris Alcock » The Morning Brew #494
on 12-09-2009 3:23 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #494

DotNetShoutout wrote Organizing ASP.NET MVC solutions - Jimmy Bogard
on 12-09-2009 4:43 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - December 9-10, 2009
on 12-09-2009 5:59 AM

Daily tech links for .net and related technologies - December 9-10, 2009 Web Development Organizing ASP

Webmaster Crap » Blog Archive » Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … wrote Webmaster Crap » Blog Archive » Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies …
on 12-09-2009 6:28 AM

Pingback from  Webmaster Crap  » Blog Archive   » Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies …

David wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 7:03 AM

Its not big deal, but doing that, you lose some of the asp.net mvc tools

Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … Scripts Rss wrote Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … Scripts Rss
on 12-09-2009 8:17 AM

Pingback from  Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … Scripts Rss

Erik wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 8:30 AM

"If I had my druthers" -- Who took your druthers, Jimmy?

Twitter Trackbacks for Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com wrote Twitter Trackbacks for Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech! [lostechies.com] on Topsy.com
on 12-09-2009 8:51 AM

Pingback from  Twitter Trackbacks for                 Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies : Blogs about software and anything tech!         [lostechies.com]        on Topsy.com

West wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:06 AM

Deciding on your project structure just so if you need to change it in the future you can get round TFS loosing the history seems a bit odd.

You can keep the history of files in TFS between projects you just have to exclude them from the project itself, move it in source control view and then include it in the new project (if I remember correctly).

Hope this helps.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:10 AM

@Jake

Ah yes, the default.aspx.  MVC2 has a way of handling this, I'll post about in the future.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:11 AM

@Paul

lol, yes, scripts go in UI.  Managed code, no.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:12 AM

@Arnis

No reason for not using IIS, other than it's one less thing to configure on everyone's boxes

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:13 AM

@David

Meh. I never use those tools anyway.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:18 AM

@West

That's a good tip, thanks!  It's not just that, though.  Folders are just easier to deal with than projects.  Project explosion leads to dependency management problems, as well as slower compile times.

John Sonmez wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 9:51 AM

Thanks for this simple solution.  I will use it in my next project!

Bill Pierce wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 11:02 AM

I would generalize the advice as one project per appdomain, plus one.  In your example you have one appdomain (UI) and Core is your plus one.

-Bill

Deran wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 11:41 AM

I'm curious how you structure your folders in the Core project. Do you do something like below?

Helpers

 - Configuration

 - Extensions

 - Filters

UI

 - Controllers

 - Models

Domain

 - Models

Impl

 - Mappers

or something like this:

Controllers

Helpers

 - Configuration

 - Extensions

 - Filters

Models

 - Domain

 - UI

Mappers

Kevin Radcliffe wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 12:00 PM

Jimmy, do you have a sample project like this available somewhere?

I get the feeling I'm just missing the link, so apologies in advance.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 12:47 PM

@Bill

YES - exactly. yagni until you need to share code between deployed appdomains

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 12:48 PM

@Deran

Either one of those is fine.  Ours is something like:

Core

- Domain

- Common

UI

- Controllers

- Filters

Infrastructure

- Repositories

etc.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 12:49 PM

@Kevin

No, unfortunately.  I can whip something up though!

AdamA wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 3:06 PM

Thanks for the article! I would appreciate a sample project as well, as I am a bit confused on how to return views from controllers which are in a different assembly. It seems like this would cause an unwanted dependency on UI from Core.

Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … | twittersoftware.org wrote Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … | twittersoftware.org
on 12-09-2009 3:09 PM

Pingback from  Organizing ASP.NET MVC solutions - Jimmy Bogard - Los Techies … | twittersoftware.org

Bayard Randel wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 4:35 PM

Are you making use of the new areas feature in MVC.Net 2 yet? They look like an excellent way of making the organisation of views and controllers more granular.

mgroves wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 5:52 PM

I like that the approach really highlights the separation of concerns, especially if you're dealing with separate developer(s) and designer(s).

That being said, I'm not sure I like the "everything" in the "Core" project approach--I'd like to see some more layering and separation expressed by separate projects.

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-09-2009 7:39 PM

@Bayard

We're in the process of migrating our project over.  Since we use Haack's areas prototype, it's not as quick and easy as we would like.

@mgroves

That's cool, I just prefer the flexibility folders gives me.

Sr. Business Intelligence Consultant | Chicago Tech Jobs | Management Business Wisdom wrote Sr. Business Intelligence Consultant | Chicago Tech Jobs | Management Business Wisdom
on 12-10-2009 12:00 PM

Pingback from  Sr. Business Intelligence Consultant | Chicago Tech Jobs | Management Business Wisdom

chad wrote re: Organizing ASP.NET MVC solutions
on 12-10-2009 2:07 PM

I think your ideas have merit, but like AdamA and Kevin suggested...an example would be nice.

Thanks.

Fatih Sever wrote re: Organizing ASP.NET MVC solutions
on 12-10-2009 3:19 PM

Hi,

I absolutely agree with you. This approach is necessary for "separation of concern" (en.wikipedia.org/.../Separation_of_concerns).

I'm using another name for code layer. There is "UI Process Components" for this in Microsoft's Application Architecture Guide (www.codeplex.com/AppArchGuide). I think it's for this purpose ha? If I'm wrong, please warn me. Anyway, my structure of asp.net mvc presentation layer is:

- ProjectName.Web.Process

    - ActionFilters

    - Controllers

    - Extensions

    - Helpers

    - ViewModels

- ProjectName.Web.UI

   - Assets

        - image

        - flash

        - video

        - icon

   - Scripts

   - Styles

   - Views

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-10-2009 10:10 PM

@Faith

Yep, looks good to me!  Basically, the web's content (images, scripts, css, views, etc) are separated from code.

Klenne wrote re: Organizing ASP.NET MVC solutions
on 12-11-2009 11:03 AM

Problem I see is if you are developing a distributed app where your business components need to run on a different server.

This structure does not allow this...

bogardj wrote re: Organizing ASP.NET MVC solutions
on 12-11-2009 12:05 PM

@Klenne

Then you have multiple deployment targets.  Multiple deployment targets = more projects.  This example is a 1 deployment target solution.

CareySon wrote 【译】组织好你的Asp.Net MVC解决方案
on 12-12-2009 7:25 AM
CareySon wrote re: Organizing ASP.NET MVC solutions
on 12-12-2009 7:38 AM

Hi,bogardj,great post..

and i had translated it into chinese and published it on chinese .net community.hope u don't mind~~

CareySon wrote re: Organizing ASP.NET MVC solutions
on 12-12-2009 7:41 AM

hi,bogardj,great post...

i had translated it into chinese and post it on a chinese .net community.hope u don't mind..

the URL is:www.cnblogs.com/.../1622679.html

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 12-15-2009 1:16 AM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

RichB wrote re: Organizing ASP.NET MVC solutions
on 12-19-2009 3:42 AM

You never move files in Solution Explorer between projects. You move them in TFS Source Control Explorer, then re-add them back into the project using Solution Explorer. That way you keep your history.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems