Friday Links 0.0.23 - Thinking About Feature Folders

This is based on an email I send my .NET team at work

Happy Friday,

I’ve been thinking about trying feature folders on a future .NET MVC project to see how I like it. A feature folder architecture bundles the source code files together by business domain feature, instead of by what type of file it is.

So for example, in our Overnight Website Challenge project, we have a folder for Controllers, a folder for Views, and a folder full of Models. Pretty typical stuff, and it looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── Controllers
│   ├── CampSignupController.cs
│   ├── ContactFormController.cs
│   └── SchoolRatingController.cs
├── Models
│   ├── Camper.cs
│   ├── CampSignupViewModel.cs
│   ├── ContactForm.cs
│   ├── Forms.cs
│   ├── Parent.cs
│   ├── Rating.cs
│   ├── School.cs
│   └── SchoolRatingViewModel.cs
└── Views
├── CampSignup.cshtml
├── ContactForm.cshtml
└── SchoolRating.cshtml

If you’re new to the project and asked to update the Camp Signup feature, you have a lot of files to bounce around in and sort through. Imagine this were a bigger site with dozens of controllers and related views and models. It can get unweildly quickly.

The feature folder architecture structures those same files like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── CampSignup
│   ├── Camper.cs
│   ├── CampSignupController.cs
│   ├── CampSignup.cshtml
│   ├── CampSignupViewModel.cs
│   ├── Forms.cs
│   └── Parent.cs
├── ContactUs
│   ├── ContactFormController.cs
│   ├── ContactForm.cs
│   └── ContactForm.cshtml
└── SchoolRating
├── Rating.cs
├── School.cs
├── SchoolRatingController.cs
├── SchoolRating.cshtml
└── SchoolRatingViewModel.cs

Now when you want to work on the CampSignup feature, every thing you need is right there and easily available.

Feature agnostic components like routing, logging, persistance can still be stored elsewhere where they can be reused.

Feature Folders in ASP.NET MVC

http://timgthomas.com/2013/10/feature-folders-in-asp-net-mvc/

If you’re interested in how to set this up so that MVC can find the views and models, this post has some samples for how to customize the view engine to know where to look.

NDC talk on SOLID in slices not layers video online

https://lostechies.com/jimmybogard/2015/07/02/ndc-talk-on-solid-in-slices-not-layers-video-online/

Jimmy Bogard (author of AutoMapper) has been a big proponent of this solution architecture for a long time. Here’s a talk he gave on why he prefers to structure solutions around features rather than around layers. Pay attention to the diagram he has contrasting this structure with the more common tier structure.

Keep in mind, his experience is with enormous and complex sites that have hundreds of controllers. This approach is probably overkill for a small project.

If you want to see it in action, he has a sample on github: https://github.com/jbogard/ContosoUniversityCore