© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Noam King CTO Sela College Noam King CTO Sela College
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Classic ASP.NET
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Why should I care? 3 main reasons : –Build applications faster –Scale easily –Test better
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Two main players Dynamic Data –Build a presentation layer based on a database schema in just a few minutes MVC – Clean separation of Layers
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel What are you going to see ? Dynamic Data projects MVC Framework Customizing DDC and MVC MVC and Dynamic data altogether Much more…
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel
What Is Dynamic Data? An easy powerful way to build a data-driven Web site Allows you to get started quickly using scaffolding completely customized UI through templates and pages Validation and business rules specified on data model flow through to UI Flexible URL routing, based on data schema and separate from the physical page New DDC make scenarios like sorting, paging, editing, and filtering remarkably easy to implement Now in Microsoft.NET Framework 3.5 SP1
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data – First Steps
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Scaffolding // Global.asax: Application_Start model.RegisterContext (typeof(NorthwindDataContext), new ContextConfiguration() { ScaffoldAllTables = true }); // WARNING: This enables scaffolding for every table in the model. To enable scaffolding on a per-table or per-column basis, you can use attributes on the model: [MetadataType(typeof(ProductMeta))]public partial class ProductMeta [ScaffoldTable(true)]{ public partial class Product [ScaffoldColumn(true)] { public object UnitPrice { get; set; } …} } // WARNING: This enables scaffolding for every table in the model. To enable scaffolding on a per-table or per-column basis, you can use attributes on the model: [MetadataType(typeof(ProductMeta))]public partial class ProductMeta [ScaffoldTable(true)]{ public partial class Product [ScaffoldColumn(true)] { public object UnitPrice { get; set; } …} }
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Routing // Global.asax: Application_Start RouteTable.Routes.routes.Add (new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model }); RouteTable.Routes.routes.Add (new DynamicDataRoute("{table}/List.aspx") { Action = PageAction.List, ViewName = "List", Model = model }); RouteTable.Routes.Add(new DynamicDataRoute("MyCategoryDetail/{CategoryID}") { Model = model, Table = "Categories", Action = "Details" });
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Scaffolding And Routing
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Data Model Annotations System.ComponentModel.DataAnnotations namespace –Scaffold attribute // as seen in previous demo –Display attributes DisplayField DisplayFormat DisplayColumn Description –Validation attributes Required Range RegularExpression StringLength DataType (e.g., , phone number, …)
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MetaModel And MetaTable MetaModel –RegisterContext methods –Access to MetaTable objects MetaTable –Schema information, MetaColumns –Attributes collection // Model annotations –Scaffold property –ListActionPath and GetActionPath() method –GetQuery() method
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Metadata And Data Model Validation
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel UI Customization Dynamic Data provides UI customization at many levels (from least to most control) PageTemplatesFieldTemplatesUIHint/DataType on ModelCustomPages UIHints on DynamicField/DynamicControl
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Field Template Controls Allow customization of columns using template based controls –Based on the type of the column –Source code for standard templates is provided –Can be overriden with DataType and UIHint attributes or properties Easy to create –Implement method to set and get values
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data Controls DynamicDataManager GridView, DetailsView –DynamicDataField ListView, FormView –DynamicControl LinqDataSource, EntitiesDataSource –DynamicControlParameter –DynamicQueryStringParameter DynamicValidator DynamicFilter (UserControl), FilterRepeater
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel UI Customization
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data Wizard Preview Scenario: Quickly create partial classes and custom pages for Dynamic Data Current features –Generates a data model from a database –Generates partial classes based on data model –Creates custom List, details pages based on model –Uses ListView control for CSS-friendly markup Preview download available: –
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data Wizard Preview Upcoming features –Ability to run within existing Web site –Ability to choose existing data model –Entities data model support –Master page and CSS support –Edit and insert forms –Additional layouts (div, ul)
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data Wizard
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Migrating Existing Pages Scenario: Reduce code in existing pages Validation –Move validation to data model instead of in individual pages. Change in one place affects entire site Field templates –Add custom controls in one place instead of in each of your pages –Enable custom controls on model
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Migrating Existing Pages
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel DDC – Short Summary Easy to Create Easy to Customize Everything Easy to integrate
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data Roadmap Support for more data sources ADO.NET Data Services (also known as Project code name “Astoria”) Plain CLR objects Binding using ObjectDataSource Support for more UI customization Attribute for ordering columns Localized display name and description attributes Insert page take defaults from fitlers More field templates Enum based columns Images from database Support for many to many columns Support for object inheritence (Linq and Entity Framework)
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Additional Resources Dynamic Data home: – Code Gallery releases: – Product Team contacts and blogs:
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel
So… What is MVC? Model ControllerView Browser requests /Artists/ Route is determined Controller is activated Method on Controller is invoke Controller does some stuff Renders View, passing in custom ViewData URLs are rendered, pointing to other Controllers
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC – First Steps
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel What MVC is not ? Not the new Web Forms 4.0 Not replacing Web Forms, but Adds to it It can not use Web Controls Not a whole new engine but sits on ASP.NET engine Not the best solution for REST
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel What MVC is? Maintain Clean Separation of Concerns –Easy Testing –Red/Green TDD –Highly maintainable applications by default Extensible and Pluggable –Support replacing any component of the system –Enables Inversion Of Control (IOC) / Dependency Injection (DI)
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel What MVC is ? (cont.) Enable clean URLs and HTML –SEO and REST friendly URL structures Great integration within ASP.NET –All the same providers still work –Membership, Session, Caching, etc. –ASP.NET Designer Surface in VS2008 Tooling Support –Intellisense, Refactoring, Debugging –Project Templates –.aspx View pages have Visual Studio designer support
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Flow Request ControllerController Step 1 Incoming request directed to Controller
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Flow ControllerController Model Step 2 Controller processes request and forms a data Model
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Flow ControllerController ViewView Step 3 Model is passed to View
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Flow ControllerController ViewView Step 4 View transforms Model into appropriate output format
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Flow ResponseResponse ControllerController ViewView Step 5 Response is rendered
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Request Flow – in more detail Request HTTP Routing Route Route Handler Http Handler Controller View Engine View Response
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Customization
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Routing Engine Map incoming URLs to the application and route them so that the right Controller and Action method executes to process them Construct outgoing URLs that can be used to call back to Controllers/Actions
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Routing Demo
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Action Filters Inject code interceptors into the request of a MVC controller Package-up and re-use functionality in a clean declarative way Predefined Action Filters Custom Filters
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Action Filters
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel TempData
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel TempData
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Ajax Support Ajax is not just “Update Panel” You can use –Page methods –Web Services They all require you to write a lot of javascript MVC Ajax helps with Forms and ActionLinks !!
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Ajax Support
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Extensibility Views Controllers Models Routes …are all Pluggable
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel ViewEngines View Engines render output You get WebForms by default Can implement your own –MVCContrib has ones for Brail, Nvelocity –NHaml is an interesting one to watch View Engines can be used to –Offer new DSLs to make HTML easier –Generate totally different mime/types Images, RSS, JSON, XML, OFX, VCards, whatever.
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel View Engine IViewEngine public interface ViewEngineBase { public void RenderView(ViewContext viewContext); }
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel NHaml – Extreme Custom Views Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> ( ) ( ) </asp:Content>
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel NHaml – Extreme Custom Views %h2= ViewData.CategoryName %ul - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" }) - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Extensibility
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Testing 1,2,3… Mockable Intrinsics –HttpContextBase, HttpResponseBase, HttpRequestBase Extensibility –IController –IControllerFactory –IRouteHandler –ViewEngineBase
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Testing Controller Actions No requirement to test within ASP.NET runtime. –Use Moq or RhinoMocks or TypeMock –Create Test versions of the parts of the runtime you want to stub
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Testing
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data and MVC Initial release targets web forms Later release will include MVC support Same concepts apply –Model metadata –Page templates –Field templates
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Dynamic Data and MVC
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC RC Views without Code-Behind Html Helpers with Expression Support Scaffolding Support FileResult, JavaScriptResult and more… Much more to come…
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Roadmap –Improved Action Invoker - Refactorings to enable Async, DLR, and Dynamic Action Filters –Dynamic Action Filters - Provide ability to apply filters at runtime, and not just compile time via attributes. –Subcontrollers - More details to follow. –Asynchronous Controller Actions - Provide an asynchronous programming model for controller actions.
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Roadmap –Sessionless controllers - Allow controllers to not use session state on a controller by controller basis (without this, you can turn session on or off for the entire site) –MVC Declarative Helpers/Designers - Allow using declarative design time markup for action links, repeater, etc… (These would be a thin veneer over our current helpers) –ScriptManager-Lite - Some means for script manager style support for embedded scripts
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel MVC Roadmap –MVC Ajax Extenders - Ajax.Extend method along with some included extenders –Ajax View Package - Package up multiple views to be rendered on the client
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Sexy Microsoft Framework Easy from the first date, yet respectful Staying young in and The right way to do get the job done. Customizable all over. Inventing and not Reinventing
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Resources My Blog – Noam King Scott Guthrie Blog Phil Hack The Official ASP.NET Site Stephen Walther’s blog test-driven-development-with-visual-studio-2008-unit- tests.aspx test-driven-development-with-visual-studio-2008-unit- tests.aspx
© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel