Architecting an ASP.NET MVC Solution

23

description

Learn how to design a web solution that exploits the ASP.NET stack: in this talk we’ll find out how to set up an effective, idiomatic design that take advantage of both “out of the box” tools (e.g. MVC, Entity Framework) and bleeding edge, third party ones. Needing a SPA? We’ll understand how to take advantage of existing toolkits. Responsive design? Let’s talk Bootstrap looking at how it provides a useful and highly customizable taxonomy for UI elements. Having troubles implementing an efficient data access layer due to a lot of business rules? We’ll learn how to use LINQ as a mean to decompose those rules in simpler ones that can be composed in a flexible and efficient way. Are you concerned about performance and scalability issues? We’ll see how to implement CQRS in order to take advantage of ad hoc data models and introduce a service bus so to decouple front-end systems from back-end ones.

Transcript of Architecting an ASP.NET MVC Solution

Page 1: Architecting an ASP.NET MVC Solution
Page 2: Architecting an ASP.NET MVC Solution

Architecting an ASP.NET MVC Solution

Andrea SaltarelloCEO @ managed/designs

[email protected]://twitter.com/andysal74

http://slideshare.net/andysal

Page 3: Architecting an ASP.NET MVC Solution

Who I am

1. CEO @ Managed Designs, striving to discover the perfect «sustainablesoftware development process»

• Customer has to be satisfied and pay

• Supplier has to have a reasonable markup

• Team members should be satisfied with their work

2. Founder and leader of UGIdotNET (first Italian .NET User Group): I need to share experiences in a peer to peer way

3. (Co)Author (together with Dino) of .NET: Architecting Applications for the Enterprise, by Microsoft Press

4. Basically, a software developer

Page 4: Architecting an ASP.NET MVC Solution

Architecture: back to basics

The fundamental organization of a system embodied in its components, their relationships to each other, and to the environment, and the principles guiding its design and evolution.

[ISO/IEC 42010]

Page 5: Architecting an ASP.NET MVC Solution

2014: Web vs. Architecture

Those «components» are (at least) the front end and the back end(s), whilst the «environment» is the Internet.

• Solution architecture

– Blueprint/pattern (e.g.: thin client, SPA)

– Visual Studio project(s)

• Front end architecture

• Back end architecture

• Cross cutting concerns (e.g.: security)

Page 6: Architecting an ASP.NET MVC Solution

FRONT END

.Net DeveloperDays 2014

Page 7: Architecting an ASP.NET MVC Solution

To SPA, or not to SPA?

«It depends»

Pro:• Responsiveness• Less bandwith usage

Cons:• Need for client side toolkit(s)• SEO

It does not have to (and it shouldn’t) be an «all or nothing» decision

.Net DeveloperDays 2014

Page 8: Architecting an ASP.NET MVC Solution

DEMOAngularJS

Page 9: Architecting an ASP.NET MVC Solution

What’s (in) a page, anyway?

As humans, it’s easy to understand the meaning of a page’s content, buthow can we have browsers and crawlers be aware of it, too? Taxonomyis the key.

• Bootstrap (http://getbootstrap.com) allows us to map html elementsonto a set of conceptual UI items

• Microdata (a.k.a. Microformats - http://www.schema.org) allowcrawlers to understand our document’s structure and index themaccordingly

– SEO

– Screen readers

.Net DeveloperDays 2014

Page 10: Architecting an ASP.NET MVC Solution

DEMO

BootstrapLESS feat. BootstrapMicrodataGoogle Webmaster Tools feat Microdata

Page 11: Architecting an ASP.NET MVC Solution

BACK END

.Net DeveloperDays 2014

Page 12: Architecting an ASP.NET MVC Solution

By «MVC», you mean Model 2

In the Model 2 approach, end user requests are managed by a servlet that controls the flow, and uses components such as JavaBeans or EJBs to access and manipulate the data. It then uses JSPs to render the application content in a Web browser. [Java doc @ Oracle]

N.B.: Assert.AreSame(“servlet”, “controller”); //

.Net DeveloperDays 2014

Page 13: Architecting an ASP.NET MVC Solution

Implementing Model 2 with ASP.NET MVC

In the Model 2 approach, end user requests are managed by a controllerthat controls the flow, and uses worker services to access and manipulate the data, which are returned as view models. It then uses views to render the application content in a Web browser. [Andrea Saltarello ]

Tips:

• Use the Force routing, Luke!

• Always implement a UrlBuilder

Page 14: Architecting an ASP.NET MVC Solution

DEMOModel 2

Page 15: Architecting an ASP.NET MVC Solution

Let’s talk «Application content» (reprise)

“application content” can both be a page or some kind of data such as: files, sitemaps, json, … That’s what ActionResults are there for

• ViewResult

• JsonResult

• ContentResult

• YourOwnPersonalResult (e.g.: MvcMate)

We could also use WebAPI… Until vNext comes

.Net DeveloperDays 2014

Page 16: Architecting an ASP.NET MVC Solution

Sitemaps

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

[http://www.sitemaps.org/]

Important: both Bing and Google do support them

Page 17: Architecting an ASP.NET MVC Solution

DEMO

Services

MvcMate

Sitemaps

Page 18: Architecting an ASP.NET MVC Solution

Model 2: the icing on the cake stack

• Model 2 <3 IoC

• Model 2 <3 CQRS

• Model 2 <3 LET

.Net DeveloperDays 2014

Page 19: Architecting an ASP.NET MVC Solution

DEMO

IoC

CQRS

LET

Page 20: Architecting an ASP.NET MVC Solution

Crawler aware security

We know how to:

• have crawlers discovering our resources URLs…

• have crawlers better understand our application’s contents…

Is there a way to have crawlers doing those lovely tasks even on login protected content? Authorization filter you need, Luke!

Page 21: Architecting an ASP.NET MVC Solution

DEMOCustom AuthorizationFilter

Page 22: Architecting an ASP.NET MVC Solution

Demo code

[MVCM] MvcMate, https://mvcmate.codeplex.com (custom ActionResults).

– Binary package available for free on NuGet: http://www.nuget.org/packages/MvcMate/

[MERP] Merp, https://naa4e.codeplex.com/ (AngularJS, Bootstrap)

[UGI] UGIdotNET; have a look at the source code (Bootstrap and HTML+Microdata) of:

– Home page: http://www.ugidotnet.org

– Sitemap: http://www.ugidotnet.org/sitemap

– News page: http://www.ugidotnet.org/news/1369/Rilasciato-ReSharper-8-1

– Video page: http://www.ugidotnet.org/video/107800840/CPP03--Functional-Techniques-in-C++

Page 23: Architecting an ASP.NET MVC Solution