ASP.NET MVC @{ Introduction & Guidelines }
Speaker Introduction Dev Raj Gautam Application & Database Specialist Nutrition Innovation Lab: Nepal Active Contributor @ASP.NET Community http://blogs.devgautam.com.np/ Have been in industry since 2009. Worked with government, local companies, outsourcing companies and Ingo's . ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Agenda • Introduction to MVC • What Does MVC Offer? • Demo • Routing & Web API. • Web Forms Or MVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Introduction ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Asp.Net MVC1 Released on Mar 13, 2009 ASP.NET Web Forms is not part of ASP.NET 5 ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
I was already there from 70’s MVC ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
MVC • Stands for Model View Controller. Common design pattern for • MVC is a software architectural pattern that follows the 'Separation of Concerns' principle. This principle divides an application into three interconnected parts as Model, View and Controller each with very specific set of responsibilities. The goal of this pattern is that each of these parts can be developed and tested in relative isolation and then combine together to create a very robust application. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Model-View-Controller • Model: These are the classes that contain data. They can practically be any class that can be instantiated and can provide some data. These can be entity framework generated entities, collection, generics or even generic collections too. • Controllers: These are the classes that will be invoked on user requests. The main tasks of these are to generate the model class object, pass them to some view and tell the view to generate markup and render it on user browser. • Views: The part of the application that handles user interaction. Typically controllers read data from a view, control user input, and send input data to the model. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
How MVC Works? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
What does MVC Offer? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Performance • ASP.NET MVC don’t have support for view state, so there will not be any automatic state management which reduces the page size and so gain the performance. ViewState ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Separation Of Concern • When we talk about Views and Controllers, their ownership itself explains separation.The Views are just the presentation form of an application, it does not need to know specifically about the requests coming from the Controller. The Model is independent of Views and Controllers, it only holds business entities that can be ed to any View by the Controller as per the needs for exposing them to the end user. The Controller is independent of Views and Models, its sole purpose is to handle requests and them on as per the routes defined and as per the needs of the rendering Views. Thus our business entities (Model), business logic (Controllers) and presentation logic (Views) lie in logical/physical layers independent of each other. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Existing ASP.NET Features • ASP.NET MVC framework is built on top of matured ASP.NET framework and thus provides developer to use many good features such as forms authentication, windows authentication, caching, session and profile state management etc. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Parallel Development • In ASP.NET MVC layers are loosely coupled with each other, so one developer can work on Controller ,at the same time other on View and third developer on Model. This is called parallel development. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Full Control Over <HTML></HTML> • ASP.NET MVC doesn’t support server controls, only option available is using html input controls, so we will be sure about final html rendered at the end. We will also be aware about 'id' of every element. And so integration of ASP.NET MVC application with third party JavaScript libraries like jQuery becomes easy. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Testing • Asp.Net webforms is dependent on HttpContext.Current, our pages as part of the HttpApplication executing the request Which makes • These are all easily mockable! • HttpContextBase, HttpResponseBase, HttpRequestBase [TestMethod] public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewData["Message"], "Hello"); } ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Testing • [TestMethod] • public void TestInvalidCredentials() • { • LoginController controller = new LoginController(); • var mock = new Mock<System.Web.Security.MembershipProvider>(); • mock.Expect(m => m.ValidateUser("", "")).Returns(false); • controller.MembershipProviderInstance = mock.Object; • var result = controller.Authenticate("", "") as ViewResult; • Assert.IsNotNull(result); • Assert.AreEqual(result.ViewName, "Index"); • Assert.AreEqual(controller.ViewData["ErrorMessage"], "Invalid credentials! Please verify your username and password."); • } ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Clean URLS • ASP.NET MVC is so tightly integrated with the ASP.NET Routing engine, it is simple to take control over the URLs that your application exposes. Would you use: /Products.aspx?CategoryID={3F2504E0-4F89-11D3-9A0C-0305E82C3301} Or: /Products/Books SEO ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Demo ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
What’s In Demo • Understanding the MVC Project Structure • Understanding and Modifying the Layout and Templeates • CRUD with project template • CRUD DIY with code • Basics of Routing • Basics of Web API ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Routing & Web API ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Routing • Routing maps request URL to a specific controller action using a Routing Table • Routing Engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
WEB API • Restful framework for Building HTTP Based services • What is Rest? “Rest is an architectural style that abstracts the architectural elements within a distributed hypermedia system” • https://api.twitter.com/1.1/statuses/retweets/2.json • Web API is not MVC ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
What In the World Is WCF Then? HUH!!!! ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Use When? Web API • Only working with HTTP • Want RESTful endpoints • Don’t need multiple protocols • Don’t expose SOAP endpoints • Want Simple Configuration WCF • Needs to support multiple protocols • Need to expose SOAP endpoints • Need Complex Configuration • Need RPC Style Requests ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Web Forms or MVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Web Forms or MVC? Web Froms • Winforms-alike event model • Familiar controls & Rich • Familiar = rapid application development • Functionality per page • Uses viewstate for state management • Less control over rendered HTML • Event Driven Programming • Oh wow! I can turn of or control View State Size. MVC • Less complex: separation of concerns • Easier parallel development • TDD support • No viewstate, … • Full control over behavior and HTML • Extensive Javascript • Makes you think ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Web Forms or MVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Web Forms or MVC? • Mixing the Technologies can also do wonders for some solutions MVC Web Forms ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Verdict? • Please decide on your own on the basis of previous slides. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
Thank You ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/

ASP .NET MVC Introduction & Guidelines

  • 1.
  • 2.
    Speaker Introduction Dev RajGautam Application & Database Specialist Nutrition Innovation Lab: Nepal Active Contributor @ASP.NET Community http://blogs.devgautam.com.np/ Have been in industry since 2009. Worked with government, local companies, outsourcing companies and Ingo's . ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 3.
    Agenda • Introduction toMVC • What Does MVC Offer? • Demo • Routing & Web API. • Web Forms Or MVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 4.
    Introduction ©Dev Raj Gautam,2015, http://blogs.devgautam.com.np/
  • 5.
    Asp.Net MVC1 Released onMar 13, 2009 ASP.NET Web Forms is not part of ASP.NET 5 ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 6.
    I was alreadythere from 70’s MVC ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 7.
    MVC • Stands forModel View Controller. Common design pattern for • MVC is a software architectural pattern that follows the 'Separation of Concerns' principle. This principle divides an application into three interconnected parts as Model, View and Controller each with very specific set of responsibilities. The goal of this pattern is that each of these parts can be developed and tested in relative isolation and then combine together to create a very robust application. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 8.
    Model-View-Controller • Model: Theseare the classes that contain data. They can practically be any class that can be instantiated and can provide some data. These can be entity framework generated entities, collection, generics or even generic collections too. • Controllers: These are the classes that will be invoked on user requests. The main tasks of these are to generate the model class object, pass them to some view and tell the view to generate markup and render it on user browser. • Views: The part of the application that handles user interaction. Typically controllers read data from a view, control user input, and send input data to the model. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 9.
    How MVC Works? ©DevRaj Gautam, 2015, http://blogs.devgautam.com.np/
  • 10.
    What does MVCOffer? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 11.
    Performance • ASP.NET MVCdon’t have support for view state, so there will not be any automatic state management which reduces the page size and so gain the performance. ViewState ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 12.
    Separation Of Concern •When we talk about Views and Controllers, their ownership itself explains separation.The Views are just the presentation form of an application, it does not need to know specifically about the requests coming from the Controller. The Model is independent of Views and Controllers, it only holds business entities that can be ed to any View by the Controller as per the needs for exposing them to the end user. The Controller is independent of Views and Models, its sole purpose is to handle requests and them on as per the routes defined and as per the needs of the rendering Views. Thus our business entities (Model), business logic (Controllers) and presentation logic (Views) lie in logical/physical layers independent of each other. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 13.
    Existing ASP.NET Features •ASP.NET MVC framework is built on top of matured ASP.NET framework and thus provides developer to use many good features such as forms authentication, windows authentication, caching, session and profile state management etc. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 14.
    Parallel Development • InASP.NET MVC layers are loosely coupled with each other, so one developer can work on Controller ,at the same time other on View and third developer on Model. This is called parallel development. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 15.
    Full Control Over<HTML></HTML> • ASP.NET MVC doesn’t support server controls, only option available is using html input controls, so we will be sure about final html rendered at the end. We will also be aware about 'id' of every element. And so integration of ASP.NET MVC application with third party JavaScript libraries like jQuery becomes easy. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 16.
    Testing • Asp.Net webformsis dependent on HttpContext.Current, our pages as part of the HttpApplication executing the request Which makes • These are all easily mockable! • HttpContextBase, HttpResponseBase, HttpRequestBase [TestMethod] public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewData["Message"], "Hello"); } ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 17.
    Testing • [TestMethod] • publicvoid TestInvalidCredentials() • { • LoginController controller = new LoginController(); • var mock = new Mock<System.Web.Security.MembershipProvider>(); • mock.Expect(m => m.ValidateUser("", "")).Returns(false); • controller.MembershipProviderInstance = mock.Object; • var result = controller.Authenticate("", "") as ViewResult; • Assert.IsNotNull(result); • Assert.AreEqual(result.ViewName, "Index"); • Assert.AreEqual(controller.ViewData["ErrorMessage"], "Invalid credentials! Please verify your username and password."); • } ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 18.
    Clean URLS • ASP.NETMVC is so tightly integrated with the ASP.NET Routing engine, it is simple to take control over the URLs that your application exposes. Would you use: /Products.aspx?CategoryID={3F2504E0-4F89-11D3-9A0C-0305E82C3301} Or: /Products/Books SEO ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 19.
    Demo ©Dev Raj Gautam,2015, http://blogs.devgautam.com.np/
  • 20.
    What’s In Demo •Understanding the MVC Project Structure • Understanding and Modifying the Layout and Templeates • CRUD with project template • CRUD DIY with code • Basics of Routing • Basics of Web API ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 21.
    Routing & WebAPI ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 22.
    Routing • Routing mapsrequest URL to a specific controller action using a Routing Table • Routing Engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 23.
    WEB API • Restfulframework for Building HTTP Based services • What is Rest? “Rest is an architectural style that abstracts the architectural elements within a distributed hypermedia system” • https://api.twitter.com/1.1/statuses/retweets/2.json • Web API is not MVC ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 24.
    What In the WorldIs WCF Then? HUH!!!! ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 25.
    Use When? Web API •Only working with HTTP • Want RESTful endpoints • Don’t need multiple protocols • Don’t expose SOAP endpoints • Want Simple Configuration WCF • Needs to support multiple protocols • Need to expose SOAP endpoints • Need Complex Configuration • Need RPC Style Requests ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 26.
    Web Forms orMVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 27.
    Web Forms orMVC? Web Froms • Winforms-alike event model • Familiar controls & Rich • Familiar = rapid application development • Functionality per page • Uses viewstate for state management • Less control over rendered HTML • Event Driven Programming • Oh wow! I can turn of or control View State Size. MVC • Less complex: separation of concerns • Easier parallel development • TDD support • No viewstate, … • Full control over behavior and HTML • Extensive Javascript • Makes you think ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 28.
    Web Forms orMVC? ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 29.
    Web Forms orMVC? • Mixing the Technologies can also do wonders for some solutions MVC Web Forms ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 30.
    Verdict? • Please decideon your own on the basis of previous slides. ©Dev Raj Gautam, 2015, http://blogs.devgautam.com.np/
  • 31.
    Thank You ©Dev RajGautam, 2015, http://blogs.devgautam.com.np/

Editor's Notes

  • #17 In most cases, in order to get the page to execute correctly, you need to execute it in a real HttpContext. Since many of the properties of HttpContext are not settable (like Request and Response), it is extremely difficult to construct fake requests to send to your page objects. This makes unit testing webforms pages a nightmare, since it couples all your tests to needing all kinds of context setup.
  • #23 routes are configured on application start by calling a staticmethod "RegisterRoutes" in RouteConfig class. We can find RouteConfig.cs file under App_Start folder in ASP.NET MVC 5 application. 
  • #29 Rapid application development  go with asp.net Web Forms, Unit Testing - If automatic unit testing is most important factor for you MVC  Does your team have experience on ASP or non-Microsoft technologies such as android, ios, JSP, ROR, PHP? Is JavaScript going to be used extensively? Looking for good performance? Planning to reuse the same input logic?
  • #30 How to Use it? Add a reference to three core librares System.Web.Routing,System.Web.Abstractions, System.Web.MVC Add the controllers and view directories Update the Web.Config to load the three assemblies at runtime and registering the URLRewriting Module Registering the Routes in Global Asax Model.