Search code examples
c#asp.net-mvcmulti-layer

Where should the controllers be in a multi layered solution


Description: I have a solution in Visual Studio which has multiple projects as application layers.

  1. Business Logic (.NET Standard)
  2. Data Access Layer for database operations (.NET Standard)
  3. Web Application (.NET Core)
  4. Xamarin Forms Mobile
    1. Xamarin Android
    2. Xamarin IOS
    3. Xamarin UWP

Question: In which layer should i create my MVC controllers as a good design practice for this scenario?


Solution

  • Controllers belong in the web layer as they handle web related activities such as routing, handling incoming http requests, etc.

    Ask yourself what a controller is meant to do and then see which layer those activities make sense in... does handling incoming http requests make the most sense in a data layer? no, the data layer should be related to data I/O.

    Does handling incoming http requests make the most sense in a business logic layer? no, the business logic should contain logic only needed to do the business/domain logic of what you're building.

    Does handling incoming http requests make the most sense in a web layer? Well yes, http requests are part of the web as we know it.

    There is an awful lot of information about n tier (aka onion) architecture on the web. Here is one example I quickly dug up that goes into more detail than my answer above