• Save
Building an enterprise app in silverlight 4 and NHibernate
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Building an enterprise app in silverlight 4 and NHibernate

  • 11,441 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
11,441
On Slideshare
9,560
From Embeds
1,881
Number of Embeds
34

Actions

Shares
Downloads
0
Comments
0
Likes
12

Embeds 1,881

http://randomware.blogspot.com 1,482
http://bartwullems.blogspot.com 174
http://randomware.blogspot.it 117
http://blogs.msdn.com 38
http://bartwullems.blogspot.co.uk 7
http://bartwullems.blogspot.in 7
http://bartwullems.blogspot.ca 6
http://bartwullems.blogspot.pt 4
http://bartwullems.blogspot.de 4
http://translate.googleusercontent.com 3
http://bartwullems.blogspot.it 3
http://bartwullems.blogspot.com.es 3
http://bartwullems.blogspot.nl 2
http://bartwullems.blogspot.jp 2
http://bartwullems.blogspot.ru 2
http://randomware.blogspot.ch 2
http://bartwullems.blogspot.com.br 2
http://bartwullems.blogspot.kr 2
http://twitter.com 2
http://bartwullems.blogspot.fr 2
http://webcache.googleusercontent.com 2
http://bartwullems.blogspot.com.au 2
http://bartwullems.blogspot.se 2
http://bartwullems.blogspot.dk 1
http://www.slideshare.net 1
http://bartwullems.blogspot.be 1
http://bartwullems.blogspot.hk 1
http://bartwullems.blogspot.tw 1
http://bartwullems.blogspot.co.at 1
http://bartwullems.blogspot.ch 1
http://bartwullems.blogspot.fi 1
http://weblog.chrisricard.net 1
http://bartwullems.blogspot.ie 1
http://bartwullems.blogspot.co.il 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

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