Building an enterprise app in silverlight 4 and NHibernate


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Gill
  • Gill
  • Gill
  • TraditionalN-tierleavestoomuchquestionsunansweredHow to handle the gap betweenvisualization and domain specification(how do youchangeyourrich domain model intosomethingthatcanbevisualized) Most business logicgotscatheredaround
  • Ons query model tonen(is ook opgebouwd met nHibernate)DataServiceKey attribuutTravelPlannerQueryContextTravelPlannerQueryServiceDataServiceKey even aanpassen naar CityNameAantal voorbeeld urls tonen:/Cities/Cities(‘Paris’)/Cities(‘Paris’)/CityNameCities(‘Paris’)/name/$value/$metadataLinqPad tonen voor complexe gevallen
  • Building an enterprise app in silverlight 4 and NHibernate

    1. 1. Building an enterprise application with Silverlight and NHibernate<br />Gill CleerenMicrosoft Regional Director/MVP ASP.NETVisugUsergroup lead - Ordina<br /><br />Bart Wullems<br />MCT, MCPD<br />Application Architect Ordina<br /><br />
    2. 2. Agenda<br />Building the foundation<br />CQRS<br />OData<br />NHibernate<br />Building the front-end in Silverlight<br />MVVM<br />What is MVVM?<br />Why and why not<br />The parts of MVVM<br />Implementing MVVM principles<br />Demo<br />Finding your VM using MEF<br />Commanding<br />Communication between VMs<br />
    3. 3. Scenario: <br />Planning your vacation <br />with NHibernate and Silverlight<br />
    4. 4. Building the foundation<br />
    5. 5. Bye bye 3-tier<br />
    6. 6. What is CQRS?<br />
    7. 7. CQS Defined<br />Bertrand Meyer (via Wikipedia)<br />“Command Query Separation”<br />“every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer.”<br />
    8. 8. CQRS defined<br />Meyer: <br />Separate command methods that change state from query methods that read state.<br />Greg Young: <br />Separate command messages that change state from query messages that read state.<br />Can have significant architectural implications<br />
    9. 9. Welcome CQRS<br />
    10. 10. Queries<br />Simple Query Layer<br />Simple views or sprocs or selects from denormalized tables<br />Simple DTOs, no mapping needed<br />Don’t go through the Domain Model, as it pollutes it<br />ViewModel per query perhaps<br />Why should the data come across 5 layers through 3 model transformations to populate a screen? (Udi)<br />Synchronous, no messaging needed<br />
    11. 11. Commands<br />Commands capture intent, DTOs don’t<br />CustomerDTOvsCustomerChangedAddressCommand<br />Handler per command<br />Can be validated outside of domain entities<br />This is why domain entities are never invalid, commands that would produce invalid state are rejected<br />
    12. 12. Commands<br />Separate Data Modification<br />Make preferred<br />Change address<br />A generic DTO could do these things, of course, but after the fact, how do you know what actually happened?<br />UI Implications<br />Grid-like screens don’t work<br />Commands require specific intent<br />
    13. 13. How do we implementthis?<br />
    14. 14. Implement the query model usingodata<br />
    15. 15.<br />
    16. 16. A RESTful Interface for Data<br />Just HTTP<br />Data as resources, HTTP methods to act on it<br />Leverage caching, proxies, authentication, …<br />Uniform URL syntax<br />Every piece of information is addressable<br />Predictable and flexible URL syntax<br />Multiple representations<br />Use regular HTTP content-type negotiation<br />AtomPub, JSON<br />
    17. 17. Model and Operation Semantics<br />Underlying data model<br />Entity Data Model<br />Entities Resources<br />AssociationsLinks<br />Operation semantics<br />Mapping of HTTP methods<br />GET  Retrieve resource<br />POST Create resource<br />PUT  Update resource<br />DELETE Delete resource<br />
    18. 18. WCF (Data) Services<br />WCF Data Services <br />WCF Services<br />REST<br />AtomPub<br />OData<br />SOAP<br />WS-Security<br />WS-*<br />
    19. 19. WCF Data Services<br />HTTP<br />Data Services Runtime<br />Reflection Provider<br />.NET Classes [+ LINQ provider]<br />Data Source(IQueryable)<br />
    20. 20. oData and Silverlight<br />Accessing Data Services<br />Silverlight ClientHttp stack still an option<br />Data Services Client more usable as it knows the details of the data service interface<br />Features<br />Full abstraction on top of the service – no need to deal with HTTP, serialization, etc<br />Data as objects with automatic change tracking<br />LINQ for queries<br />Data-binding friendly interfaces<br />Work same-domain and cross-domain<br />
    21. 21. CQRS and OData<br />OData<br />21<br />
    22. 22. Demo<br />UsingOData to implement the query part of CQRS<br />22<br />
    23. 23. Implement the domain model usingnhibernate<br />
    24. 24. NHibernate<br />Full-featured ORM solution<br />Open source<br />Based on Java’s Hibernate framework<br />Uses XML(by default) to map tables/columns to objects/properties<br />
    25. 25. NHibernate API<br />
    26. 26. NHibernateQuickstart<br />Create hibernate.cfg.xml or use app.config<br />varcfg = newConfiguration(); <br />cfg.Configure();<br />varsf = cfg.BuildSessionFactory();<br />using(var s = sf.OpenSession())<br />using(vartx = s.BeginTransaction()) <br />{<br />  var c = s.Get<Customer>(42);<br />tx.Commit();<br />}<br />
    27. 27. WhyNHibernate?<br />EntityFramework<br />GreatforRapidApplicationDevelopment<br />Falls short forEnterpriseApplicationDevelopment<br />NHibernate has <br />More maturity<br />More flexibility<br />Betterextensibility<br />
    28. 28.
    29. 29. CQRS and NHibernate<br />NHibernate<br />NHibernate<br />29<br />
    30. 30. Convention over Configuration<br />FluentNHibernate<br /><br />Replaces<br />XML mappingbyfluentmapping<br />XML configurationbyfluentconfiguration<br />Advantages<br />Type safety<br />Removestedious XML mappings<br />Intuitive interface<br />Conventions<br />
    31. 31. AutomaticSession management<br />DataContext per Request<br />IServiceBus<br />Request 1<br />Session 1<br />DB<br />Session 2<br />Request 2<br />
    32. 32. Demo<br />Using NHibernate to implement the domain part of CQRS<br />32<br />
    33. 33. Building the front-end in Silverlight<br />
    34. 34. What is MVVM?<br />
    35. 35. Understanding MVVM<br />MVVM :<br />is an architectural pattern created by John Gossman from WPF team<br />is a variation of MVC pattern<br />is similar to Martin Fowler’s PresentationModel pattern<br />works because of Silverlight data Binding & commanding<br />is typically used in WPF/SL-applications to leverage the power of XAML, so that Devs and Designers can work together easier<br />35<br />
    36. 36. Understanding MVVM<br />
    37. 37. Why and why not MVVM?<br />
    38. 38. Why MVVM<br />Better SoC (Seperation of Concerns)<br />More maintainable<br />Model never needs to be changed to support changes to the view<br />ViewModel rarely needs to be changed to support changes to the view<br />More testable <br />ViewModelis easier to unit test than code-behind or event driven code<br />Eliminates the need to do code behind which leaves the UI all in XAML <br />38<br />
    39. 39. Why MVVM<br />Because the framework (SL & WPF) have the power to support it<br />Databinding/DataContext<br />Increases the "Blendability" of your view<br />
    40. 40. Why not MVVM<br /><ul><li>Lack of standardization so everyone has own favor</li></ul>Message to community is not clear!!<br /><ul><li>For simple UI, M-V-VM can be overkill
    41. 41. Too much code needed</li></ul>INotifyPropertyChanged<br />Commands<br />40<br />
    42. 42. The parts of MVVM<br />
    43. 43. What we all do…<br />All UI code in code-behind<br />View<br />XAML<br />Data Model<br />Code-Behind<br />Event Handlers<br />
    44. 44. The MVVM way<br />View<br />XAML<br />Code-Behind<br />Change notification<br />Data-binding and commands<br />View Model<br />Data Model<br />State + Operations<br />
    45. 45. The parts of MVVM<br /><ul><li>View knows ViewModel
    46. 46. ViewModel knows Models
    47. 47. But not vice versa.</li></ul>View<br />ViewModel<br />Model<br />44<br />
    48. 48. The View<br /><ul><li>The view</li></ul>represents the user interface that the user will see.<br />can be a user control or Data Template<br />shouldn't contain any logic that you want to test<br />should be kept as simple as possible.<br />45<br />
    49. 49. The ViewModel<br /><ul><li>An abstraction of View
    50. 50. Connector between View and Model
    51. 51. Make VM as testable as possible</li></ul>46<br />
    52. 52. The Model<br /><ul><li>Can be Data Model, DTO, POCO, auto-generated proxy of domain class and UI Model based on how you want to have the separation between Domain Service and Presentation Layer
    53. 53. No reference to ViewModel</li></ul>47<br />
    54. 54. Where to start?<br />BING (or google is fine as well )<br />MVVM Light Toolkit<br />Prism<br />Caliburn<br />48<br />
    55. 55. Demo<br />Let’s take a look at an MVVM implementation<br />49<br />
    56. 56. Implementing MVVM principles<br />
    57. 57. View model base class<br />Implements INotifyPropertyChanged<br />Contains base code for all VMs to re-use<br />51<br />
    58. 58. Demo<br />BaseViewModel<br />52<br />
    59. 59. Which comes first...<br />2 options: <br />View first<br />ViewModel first<br />53<br />
    60. 60. ViewFirst<br />Based on XAML mostly<br />The View has a relationship to its ViewModel (usually through data binding).<br />DataContext={Binding ...}<br />Available at design time (Blend support)<br />54<br />View<br /><UserControl.DataContext><br /> <dive:PageViewModel /><br /></UserControl.DataContext><br />
    61. 61. ViewModel First<br />The ViewModel creates the view <br />usually through an IoCcontaineror MEF <br />55<br />View Model<br />public MyViewModel(IMyViewmyView)<br />{<br />myView.Model = this;<br />}<br />
    62. 62. How to implement this?<br />Locator pattern<br />Implemented through a class that contains all VMs as static properties<br />An instance is then made available as Resource<br />All Views can bind, no code needed in View<br /> Clean way <br />Not good since all VMs need to be known upfront<br />Property for each available VM<br />Difficult if application is MDI-like (more than one instance available)<br />56<br />
    63. 63. Demo<br />Locator<br />57<br />
    64. 64. How to implement this?<br />MEF<br />Based on <br />Import: class says it needs an instance of a specific type<br />Export: class says it wants to be made available for composition<br />Composition: MEF links imports and exports together<br />Can create one shared instance or dynamic number thereof<br />Solves earlier problem<br />Easy for testing and mocking<br />58<br />
    65. 65. Demo<br />MEF<br />59<br />
    66. 66. Commanding<br />Instead of having event handlers in code-behind, we use commands<br />SL4 has the ICommand interface<br />Execute method<br />CanExecuteproperty<br />CanExecuteChangedevent<br />Way to create commands:<br />Write ICommand implementation<br />Create instance on VM<br />Bind Command property of control to this instance<br />60<br />
    67. 67. Commanding<br />Commanding is supported on ButtonBase <br />Button, HyperlinkButton<br />Not on others like ComboBox SelectionChanged<br />Can be solved with event triggers <br />Can be added on every event<br />Part of System.Windows.Interactivity<br />Can be bound to every control<br />
    68. 68. Demo<br />Commanding<br />62<br />
    69. 69. Communication <br />63<br />View Model<br />View Model<br />View Model<br />View Model<br />View Model<br />View Model<br />View Model<br />View Model<br />
    70. 70. Communication<br />64<br />View<br />View<br />XAML<br />XAML<br />Code-Behind<br />Code-Behind<br />View Model<br />Data Model<br />View Model<br />State + Operations<br />Data Model<br />State + Operations<br />Message<br />Publish messages<br />View<br />XAML<br />Code-Behind<br />Subscribe to messages<br />Event Aggregator<br />View Model<br />Message<br />State + Operations<br />
    71. 71. Communication<br />VM’s need to be able to talk to each other, eg: send messages to each other<br />Not a good idea to have each VM reference all other VMs<br />Solution:<br />Event Aggregator/mediator/messenger<br />VM can register to receive messages of a certain type (for example string messages)<br />Another VM can register with the same messenger to send messages<br />This allows both VMs to communicate with each other without tight coupling<br />65<br />
    72. 72. Demo<br />Messaging between VMs<br />66<br />
    73. 73. Some tips to take home<br /><ul><li>Use MVVM in Blend
    74. 74. In View’s constructor, check DesignerProperties.IsInDesignTool</li></ul>Unit testing<br />VMs can easily be unit-tested<br />Silverlight Unit Testing Framework ships with Silverlight 4<br />Can be used from the browser<br />No real way to integrate with automated testing (MSBuild) yet<br />67<br />
    75. 75. Demo<br />Unit testing<br />68<br />
    76. 76. Summary<br />CQRS <br />Gives the necessary level of scalability and maintainabilityforenterpriseapplications<br />oData is greatfor the query part<br />NHibernate rocks for the command part<br />MVVM<br />Create better testable applications<br />Works for both Silverlight and WPF<br />
    77. 77. Questions?<br />