- Controllers
-
Repository/Data Access Layer
- 5. Query Database
- Models map to Tables
- Data Context Database
-
Logic
-
Optional Layer between Controller and DAL
- Can be Very Complex
- Or a simple Adaptor Pattern
- 3 Process Actions
- 7. Data ready for return
- Razor Views
-
View Models
- Hold Data for particular views
- Lightweight
- User REST request
-
Logic (Instantiates DAL Interface)
-
Calculate(x,y)
- DAL.Get(x)
- DAL.Get(y)
-
Retrieve(something)
- DAL.Get(something)
-
Delete(something)
- Dal.Delete(something)
- New()
-
Data Access Layer (Instansiates DB Interface)
- Get()
- Delete()
- Query
- Add()
-
Controller (Instantiates logic interface)
-
Create
- logic.New()
-
Read
- logic.Retrieve()
-
Update
- logic.Update()
-
Destroy
- logic.Delete(x)
-
More Layers
- DBContext
- Model Entities
-
Why have ViewModels?
- Pros and Cons
-
Pros
- Neat looking code
- Logic returns predictable data model
- Programmer has less to think about
-
Cons
- You can't always fit all data neatly together
- May not need all data initially
-
Alternatives
- Simple requests may directly use model entities
- Pass individual variables in ViewBag or ViewData
- Use JavaScript to request JSON via REST API
-
REST % CRUD == 0
- Ideally, one web resource has 4 user actions
- Create, Read, Update, Destroy
- REST is how the URL is structured to achieve CRUD
-
Pros
- CRUD maps nicely to SQL actions
- Enforcing CRUD Keeps Code Simple
- Forces Programmer to think of User Tasks
- User Centered Design?
- The View can stay simple and dumb
-
Cons
- You could need many controllers if strictly keeping each controller to one task
- Not all user actions need CRUD on resources.. e.g. simple static pages
-
It can be impossible to map one user task to only one resource..
- e.g. Order Pizza action:
- new order
- new payment
- payment confirmation
- Simple actions may not require all 4 actions
- We may also want another MVC layer in the View itself.. difficulty in communication between 2 MVCs?
- Low Coupling
- Program to Interface,
- Seperate
- not to Specific Objects
- Abstraction