46
MVC Controller Architecture Deep Dive

Deep Dive: MVC Controller Architecture

Embed Size (px)

DESCRIPTION

The driving force behind the MVC architecture is the controller. It returns the appropriate view and model for a request, but that is not the end of the story. In ASP.NET MVC, the controller is much more powerful. I will go deep into the infrastructure of the controller. You will discover that there is much more to this powerful framework than is shown in typical examples, and you will be able to use this knowledge for business scenarios that go beyond serving simple web pages.

Citation preview

Page 1: Deep Dive: MVC Controller Architecture

MVC Controller Architecture

Deep Dive

Page 2: Deep Dive: MVC Controller Architecture

Holistic Look

Where do controllers fit?

Page 3: Deep Dive: MVC Controller Architecture

Simple Application

• A web site• Abstraction costs higher than return• Does not integrate with business

processes• Solo project

Page 4: Deep Dive: MVC Controller Architecture

Simple Application

Controller is the life of this app!

Page 5: Deep Dive: MVC Controller Architecture

Simple Application

ModelView

Controller

Page 6: Deep Dive: MVC Controller Architecture

Layered Application

• More thought placed in design• Integrates into larger infrastructures• Abstraction necessary for reuse• Team project

Page 7: Deep Dive: MVC Controller Architecture

Layered Application

Controller is the public interface

Page 8: Deep Dive: MVC Controller Architecture

Layered Application

Workflow Component Model

Business Layer

DAL Component Gateway

Data Access Layer

ControllerViewViewModel

Presentation LayerCrosscutti

ng Concerns

Page 9: Deep Dive: MVC Controller Architecture

Enterprise Application

• Complexity determined by business needs– But strive for simplicity• And reusability

• Will integrate into larger infrastructure

Page 10: Deep Dive: MVC Controller Architecture

Enterprise Application

Controller is still the public interface

Page 11: Deep Dive: MVC Controller Architecture

Enterprise Application

BUT

Page 12: Deep Dive: MVC Controller Architecture

Enterprise Application

It may be the life of an app if small…

Page 13: Deep Dive: MVC Controller Architecture

Enterprise Application

DB

Workflow ServiceData

Service

Business Service

WPF AppConsole

App Web App

This is reduced…

Public Service

Page 14: Deep Dive: MVC Controller Architecture

Enterprise ApplicationIt probably looks like this...

Page 15: Deep Dive: MVC Controller Architecture

The Controller

Designing the perfect cog

Page 16: Deep Dive: MVC Controller Architecture

Question

Should I use repositories?

Page 17: Deep Dive: MVC Controller Architecture

Should I Use Repositories?

Yes

Page 18: Deep Dive: MVC Controller Architecture

Should I Use Repositories?

Only composition logic

Encapsulate and abstract other logic

Page 19: Deep Dive: MVC Controller Architecture

Considerations

• Flexibility• Maintainability• Related entities• Are you using more than one

repository per controller?

Page 20: Deep Dive: MVC Controller Architecture

DEMO

Encapsulation is simple

Page 21: Deep Dive: MVC Controller Architecture

Question

Do I need one repository per model?

Page 22: Deep Dive: MVC Controller Architecture

One Repository Per Model?

No

Page 23: Deep Dive: MVC Controller Architecture

One Repository Per Model?

Probably

Page 24: Deep Dive: MVC Controller Architecture

Repository Style

Coarse-Grained

Fine-Grained

IEventRepository

IEventRepository IAttendeeRepository

Page 25: Deep Dive: MVC Controller Architecture

Question

How do I transition to n-tier?

Page 26: Deep Dive: MVC Controller Architecture

Transition to n-tier?

Easier

Page 27: Deep Dive: MVC Controller Architecture

Transition to n-tier?

If you followed my previous recommendations

Page 28: Deep Dive: MVC Controller Architecture

Breaking It Up

Workflow Component Model

Business Layer

DAL Component Gateway

Data Access Layer

ControllerViewViewModel

Presentation LayerCrosscutti

ng Concerns

Page 29: Deep Dive: MVC Controller Architecture

DEMO

Moving to a tierWithout affecting your Controller!!!

Page 30: Deep Dive: MVC Controller Architecture

The Controller

• Routing• Verbs• Action Results• Standard Controllers• Custom Controllers

Page 31: Deep Dive: MVC Controller Architecture

Routing

• HTTP Request• web.config• Global.asax• Favor Resources

Page 32: Deep Dive: MVC Controller Architecture

Routing

Logical

Physical Controllers

Routes

Page 33: Deep Dive: MVC Controller Architecture

DEMO

Routing

Page 34: Deep Dive: MVC Controller Architecture

Verbs

• HTTP Methods• AcceptVerbs Attribute• RESTful CRUD– GET– PUT– POST– DELETE

Page 35: Deep Dive: MVC Controller Architecture

Verbs

• HEAD• OPTIONS• CONNECT• TRACE• Custom Verbs

Page 36: Deep Dive: MVC Controller Architecture

DEMO

Verbs

Page 37: Deep Dive: MVC Controller Architecture

Action Results

• Controllers do something when called• Standard action results– ViewResult– JsonResult– ContentResult– EmptyResult– FileResult– HttpUnauthorizedResult

Page 38: Deep Dive: MVC Controller Architecture

Action Results

• More Standard actions– JavaScriptResult– RedirectResult– RedirectToRouteResult

• Make your own!

Page 39: Deep Dive: MVC Controller Architecture

DEMO

Custom Action Results

Page 40: Deep Dive: MVC Controller Architecture

Standard Controllers

• Great for standard web applications• Convention-based– Even non-standard controllers are

convention based when used with views

Page 41: Deep Dive: MVC Controller Architecture

Custom Controllers

• Can decorate the standard class• Can inherit from ControllerBase• Can implement IController• Use for non-standard applications– or for more control

Page 42: Deep Dive: MVC Controller Architecture

DEMO

Custom Controllers

Page 43: Deep Dive: MVC Controller Architecture

Controller as a Service

• MVC apps are typically web sites• They can also be web services• Do you have a mixed site/service?• It can also be a pure service without

JSON

Page 44: Deep Dive: MVC Controller Architecture

DEMO

Controller as a Service

Page 45: Deep Dive: MVC Controller Architecture

ResourcesSoftware Application

Developers

http://msdn.microsoft.com/

Infrastructure Professionals

http://technet.microsoft.com/

msdnindia technetindia @msdnindia @technetindia

Page 46: Deep Dive: MVC Controller Architecture

© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and

Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.