سؤال

I'm working on a project that is a API with many controllers and modules. Which of the following is the best architectural practice to organizing my API controllers by dll (.NET 4.7 WebAPI)? Why?

OBS: Each item is inside a module is a dll

  1. One dll for each controller. Each csproj contains only one Controller.cs file:

Module 1
-- API Controller 1

-- API Controller 2

-- Domain
-- Application
-- Infra

Module 2
-- API Controller 3

-- API Controller 4

-- Domain
-- Application
-- Infra

  1. Group controllers in projects by context

Module 1
-- API

-- Domain
-- Application
-- Infra

Module 2
-- API

-- Domain
-- Application
-- Infra

  1. Create only one csproj with all controllers.

Module 1
-- Domain
-- Application
-- Infra

Module 2
-- Domain
-- Application
-- Infra

API (controllers)

Note: I'm using DDD (as possible), separating my business rules by context. Each context has its layers: domain, application and infra.

I started centralizing all Controllers of all contexts in one api project. It started to be messy, so I break in separate projects. However, there is some performance issue or architectural pattern that should I follow to organize my project?

هل كانت مفيدة؟

المحلول

I see this as an extension of locality: code should live near where it will be used, when possible. Sometimes it makes sense to break things up differently, but that's the default we generally use, right?

Similarly, if you're working on the domain model for something, you're probably going to also work on the controller code soon if you didn't already. We know that different layers in an N-tier architecture are going to be tied together, and it's not just by convention.

Different elements of business logic and/or the model may tie together as well, but we know the different layers will, so that seems like the preferential grouping to me, if you know you're going to have a lot.

That said, you're using domain-driven design, so you're going to have a very robust model that you probably want to keep together. I usually end up with a layout something like this:

  • Model
    • ModelElement
    • ModelElement
  • Module 1
    • Controller
    • DAL
  • Module 2
    • Controller
    • DAL

I don't think I could point to any specific documents that state this as a best practice, but it's worked well for me and I hope it helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top