Tuesday, April 3, 2018

Building Enterprise Grade Web APIs - Use Feature Folders

One of the most important attributes of any code we write is maintainability.  Over the lifetime of a piece of code, it will be debugged, modified and upgraded numerous times, often by developers who were not on the original team that wrote the code. 

One of the most important aspects of maintainability is that the developer maintaining the code needs to be able to easily find what they are looking for.  This is where feature folders come in.  When using feature folders in our API, we don't use the traditional folders named Models and Controllers.  Instead, we name our folders after the endpoints of our API (the features) and place all the code related to to that endpoint (feature) within that folder.  Here is an example:

As you have probably guessed, this is an API related to Food Trucks.  If you looked at the endpoints for the API you would see URLs like /api/FoodTrucks and /api/Locations.  It is pretty easy to guess where the code is for those endpoints, in the FoodTrucks and Locations folders respectively.  For child endpoints, that is, endpoints like /api/FoodTrucks/{foodTruckId}/Reviews, I use a dot notation on the folder name like FoodTrucks.Reviews.  As you can see though, it is very easy to map from what an endpoint is to where the code is that supports that endpoint.

As you see in the screenshot above, I have expanded the Locations folder so we can have a look inside.  We see the following:
  • The controller - LocationsController
  • The view models used by this feature - LocationModel, CreateLocationModel and UpdateLocationModel
  • Two validator classes that are used to implement validation rules related to this feature  - CreateLocationModelValidator and UpdateLocationModelValidator
  • The AutoMapper profile used to map objects related to this feature - LocationsAutoMapperProfile.
Without even looking inside the files at the code, you already have a pretty good idea about what is going on.  And you know where everything is related to this feature.  Contrast this with the traditional folder layout in MVC projects:

Here if we need to debug and then make a change to the Locations endpoint, we have to navigate multiple folders to do so.  In fact, I'm not even showing the validator classes or the AutoMapper profile classes in this screenshot because they don't fit on the screen.  Sure, we could put our validators in with our models in the ApiModels folder, but for even for this relatively API, we are starting to get some folders that are unwieldy.

There is another benefit of using feature folders.  Since all of the code used for a feature is in one folder, the folder (and thereby the namespace) forms a boundary in our code that delineates this feature.   If we find ourselves needing to reach across folders, we are crossing a boundary in our code base and we have an indication that something is wrong.  This helps to discourage us from sharing model objects across two or more controllers where the object would have more than one responsibility.  Having boundaries defined like this in our code helps us be more modular and keep everything where it belongs.


I think feature folders are the way to go.  This is a pattern that you actually see quite a bit in the Angular and React communities, and I think there is no reason not to adopt the same in ASP.NET projects.  I think we end up with a much cleaner organization of the code and it is easier to find what you are looking for.  So for your next project, give feature folders a try.  I think once you complete a project using feature folders you will agree it is the way to go.


  1. wow really superb you had posted one nice information through this. Definitely it will be useful for many people. So please keep update like this.

    Manufacturing ERP
    Human Resources Management Software
    CCTV Camera Dealers in Chennai

  2. Really nice blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training Hyderabad

  3. Nice post. Very interesting and useful information you had posted. Thanks for sharing your knowledge.
    Hope it might be very useful for many people. keep on updating...!!

    Vehicle tracking system
    Fleet management software


  4. A great article! I also have an ERP system in my company and I am very satisfied with it - zero problems.
    Now I'm wondering about the internet of things. I've seen an interesting Pro4People offer (my friends say, contact us , but I'm wondering all the time ...) . Do you know this too and you could advise me?

  5. Nice blog. Thanks for sharing such great information.Develop xamarin application Hire Xamarin developer , Xamarin development company

  6. I really appreciate your efforts towards Console Jailbreak , I really hope to have one day fully jailbroken console to get full potential for the money I have paid to buy a machine. I was in search for PS3 Jailbreak Download free no survey and PS4 Games PKG Download finally found it.

  7. building enterprise grade web APIs is one of the best post

    our blog : http://www.shreeyainfotech.com

  8. CRM Development Company
    Working with a professional CRM Development Company can mean the difference between getting it right first time, or having to rework. Who has time for that

  9. Have you heard of the program aws cloud solutions - pro4people.com are working, amongst others, in the field of medical softwares. I believe they could greatly influence and help any establishment in prospering in a better way. It's always good to vote on the innovations.

  10. Eumaxindia is a leading Consultant for Times of India advertisement agency in Chennai, Times of India Newspaper Ad Booking in Chennai. For more details contact us – 98404 86666

    Times of India Chennai Advertisement Booking Centre

  11. CitySpidey is India's first and definitive platform for hyper local community news, RWA Management Solutions and Account Billing Software for Housing Societies. We also offer air quaity index and residential soceity news of Noida, Dwarka, Indirapuram, Gurgaon and Faridabad. You can place advertisement for your business on city spidey.

    Gate Management System
    Society Management App
    Society Management
    rwa Management App
    Neighbourhood Management App
    Apartment Management App
    Apartment Management System
    Visitors Management System
    Apartment Management Software
    Air Quality Index, Air Pollution
    Noida News
    Gurgaon News
    Ghaziabad News
    Delhi News
    Indirapuram News
    Dwarka News

  12. Buy Car and Inverter battery online in India. We are Noida based India's No. 1 online battery store offering you genuine batteries of all the well-known battery brands at best prices.

    Inverter Battery Dealer in Noida
    Battery Dealer in Noida
    Authorized Battery Dealer in Noida
    Car Battery Dealer in Noida
    Buy Battery Online

  13. Drive website traffic ''https://zarihz678.wixsite.com/seosolutionx'' Drive more traffic to your website has also initiated an ultimate SEO package. This package fits in the category of guaranteed quality package. This package provides everything that one could wish of. Our company’s major target is to serve small business owners.

  14. DescriptionFIFA, also known as FIFA Football or FIFA Soccer, is a series of association football video games or football simulator, released annually by Electronic Arts under the EA Sports label. fifa 19 hack 2019

  15. Such a nice blog. Very interesting and remarkable article. Business enterprise great web is very helpful to all.

  16. We provide services for each and every existing brand of antivirus namely Norton,Avast, Kaspersky, McAfee, Bit defender, . Antivirus support number is the key to several solutions. If you have any sort of problem with your antivirus then Dial Max Secure Antivirus Support Number +(1)-888-846-5560 This Team is basically is a group of proficient. Every technician working with this team is trained and qualified.who provide 24*7 Customer Support For User all over the world
    For More information Please Visit: Antivirus Support Number

  17. If you are facing any email related issue then Dial Our AOl Email Customer Support Phone Number +(1) 844 489 7268 who Provide 24*7 Customer Support Service all over the world
    For more Information Please Visit:Email Support Number

  18. If your Antivirus is not working properly And you are not getting the solution to that problem. you can connect with us on our Mcafee Antivirus Tech Support Phone Number +(1)-888-846-5560 for resolving your issues without any hassle and delay.Our tech team 24*7 available to solve your problem, And live chat.
    For More Information Visit here :https://www.onlinesupportpro.us/