简体   繁体   中英

Layered architecture practice

I'm building a layered WebAPI. I have

  • Data access layer
  • Business logic layer
  • Model (for database models)
  • Presentation layer (MVC)

Database looks like this:

Courses
    - CourseID

Users
    - UserID
    - FirstName
    - LastName

Attendants
    - CourseID *
    - UserID *

Dates
    - CourseID *
    - Date

I have made models (Model layer) which are 1:1 fields on database.

Now I want to create a model (business model?) which has:

class Course
    List<User> Attendants { get; set; }
    List<DateTime> Dates { get; set; }

The goal is to return this model as JSON.

What layer does this model belong in?

The goal is to return this model as JSON

If that's the only goal then it's not a business model, it's a view model. As such, it would belong in the presentation layer.

Business models serve the purpose of defining and structuring the domain logic, independent of any particular technology being used. View models serve the purpose of binding data to a particular technology for presentation.

Ultimately, the question is... To which part of the system should a particular component be coupled? Which other parts of the system will need to rely on it? In this case, it's coupled to a presentation technology and only the presentation layer will rely on it.

I would re-word your layers this way:

  • (DSL) Data storage layer - your SQL database
  • (DAL) Data access layer - entity framework
  • (BOL) Business objects layer - your entities (Model in MVC)
  • (PL) Presentation layer - your controllers logic with viewModels (Controller ins MVC)
  • (VL) Views layer - your MVC views. (View in MVC)

Since your Course will be assembled on Control level into view model which will be passed to the view I would suggest to put it into Presentation Layer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM