Model-View-Presenter using the Smart Client Software Factory (Introduction To CAB/SCSF Part 25)

Introduction

Part 23 and part 24 of this series of articles described the Model-View-Presenter pattern.

This article explains how the Smart Client Software Factory supports this pattern by generating appropriate classes.

Guidance Automation Packages in the Smart Client Software Factory

We saw how we could use the Smart Client Application Guidance Automation Package to set up a Smart Client Application in part 18. We can also set up a Model-View-Presenter pattern in a Smart Client application using another of the Guidance Automation Packages.

This will only work in an existing Smart Client Application.

Running the Model-View-Presenter Package

To use the Guidance Automation Package we right-click in Solution Explorer on a project or folder where we want to run the package. It is intended that we do this in the Views folder in a business module. On the right-click menu we select ‘Smart Client Factory/Add View (with presenter)’. We get a configuration screen that lets us name our view, and also lets us put the classes that get created into a folder. For the purposes of this example we name our view ‘Test’, and check the checkbox that says we do want to create a folder for the view.

When we click ‘Finish’ we get three classes and a TestView folder as below:

mvpsolutionexplorer.jpg

Classes Created

  1. TestView
    This is (obviously) our View class. It is intended that this contain the auto-generated code to display the View. As discussed in the previous articles any complex view logic will not go into this class, but will go into the Presenter.
  2. TestViewPresenter
    This is our Presenter class. As discussed in previous articles this should contain logic to deal with user events. It should also contain any complex view logic, and should directly update the View with the results of an view logic calculations. It has access to the View class via an interface.
  3. ITestView
    This is the interface that the View implements. The Presenter can only update the View through this interface.

Diagram

In terms of the diagrams shown in parts 23 and 24 this looks as below. Remember that we may or may not have arrows between the Model and the View depending on whether we are using the active View or passive View version of Model-View-Presenter:

mvpdiagram2.jpg

Where’s the Model?

The Guidance Automation package does not set up a Model class for us. As we have seen, the Model has no direct references to a View/Presenter pair (it raises events), and there may be multiple View/Presenter pairs for one Model. Further the Model would not usually be in the same folder, or even in the same component, as our View and Presenter.

For these reasons we are expected to set up our Model classes separately by hand.

Note that the Presenter (and the View as well if we are using the active View pattern) will have a direct reference to the Model. We will have to add these references manually.

Active and Passive View: a Quick Recap

Remember that in Model-View-Presenter the Presenter updates the View via an interface. We can set this up so only the Presenter is allowed to update the View. This is the ‘passive View’ pattern. We can also set this up so that the Presenter can update the View in complex cases, but the View can also update itself (in response to an event or user request) in simple cases. This is the ‘active View’ pattern.

Active and Passive View: Which Should We Use?

The pattern described in the SCSF documentation is the passive View: the documentation implies that all updates to the View should be done by the Presenter.

However there is nothing to stop us using the active View pattern with the classes generated by the Guidance Automation Package. We can add code to update the View wherever we like. In fact I would recommend using active View in simple cases: passive View should only be used where we are putting too much logic into the View class.

Should We Use Model-View-Presenter for Every Screen? A Personal View

Let me also reiterate a point made in part 24. It’s easy to get obsessive about the use of patterns and use them everywhere without thinking. My personal opinion is that we should only use the full Model-View-Presenter pattern where we have a complex screen that will benefit from the separation of the View and Presenter classes. For very basic screens the pattern is really too complex to give us benefit. In simple cases I think it is fine to put event handling and screen update logic directly behind the screen.

Note that I don’t think this applies to the use of the Model. We should always separate out the business logic from our screens into separate classes (this is what Martin Fowler calls ‘Separated Presentation’). However, we frequently have screens that don’t show any business logic or business data, so we may not need a Model class either.

For example an About screen that just shows the system name and version won’t need separate View and Presenter classes, and probably won’t need anything in a Model class either.

Equally a screen that shows a read-only grid of currencies used in a trading system probably doesn’t need separate View and Presenter classes. In this case the currencies themselves should be in a Model class so that other screens can access them.

Implementation Details: What We’d Expect

If we examine the diagram above, we expect the Presenter to have a data member with type of our ITestView interface that it will use to access the View. We expect the View to implement the ITestView interface to allow this. We further expect the View to have a direct reference to the Presenter class (a data member), which it will use to invoke code relating to user events. We’d probably expect both the View and the Presenter classes to be created the first time the View is needed.

Implementation Details: the Base Presenter Class

The actual details of the implementation of the Presenter are a little unusual.

If we look at the code generated by the Guidance Automation Package we see that the TestViewPresenter above has been given its core functionality by inheriting from an abstract Presenter<TView> class. Remember that the generic ‘TView’ simply lets us provide a type whenever we use the Presenter class. Here we inherit from Presenter, and provide the type when we inherit:

    public partial class TestViewPresenter : Presenter<ITestView>
    {

This allows the base Presenter class to have a data member of type ITestView (which is what we expect), rather than it being directly in the TestViewPresenter class. Note that the base Presenter is in the Infrastructure.Interface project (which is one of the reasons why we have to use this pattern in a Smart Client application).

The base Presenter class exposes our ITestView data member publicly, contains a reference to our WorkItem, and has disposal code and a CloseView method. It also has virtual OnViewReady and OnViewSet methods. These get called when you’d expect from the name and let us respond at the appropriate times by overriding the methods in our TestViewPresenter class.

All the above functionality in the base Presenter class means that the derived TestViewPresenter class is basically empty when it is created. It is up to us to put logic in there to handle user events and complex view logic.

The TestView class is a normal user control. It implements ITestView and contains a reference to the TestViewPresenter as we’d expect. It also calls OnViewReady as appropriate (in the OnLoad event of the user control). Again other than this TestView is also basically empty.

Conclusion

This article has shown us how to set up Model-View-Presenter classes using the Smart Client Software Factory, and discussed some issues surrounding it.

29 thoughts on “Model-View-Presenter using the Smart Client Software Factory (Introduction To CAB/SCSF Part 25)

  1. Ahhh no part 26 yet 🙂

    Microsoft should leave out those CAB quickstarts like BankTeller or seperate them
    More a SCSF project with Model-View-Presenter examples is what im looking for

    Again: Awesome introduction!

    I ordered a book by “David S. Platt” called “Programming Microsoft Composite UI Application Block and Smart Client Software Factory”. When i have it i wonder if that is anything near this documentation. Anyway he get paid to write about it and u do it freely 🙂

  2. I must say this is the type of documentation that should have been provided.
    Maybe the P&P guys can leave the real high end blah to themselves and provide this type of info in the future.

    rfcdejong, i would not get your hopes up over the book you ordered, i didnt find it usefull at all MS Press should have got these blogs posted instead of the book, but thats my opinion.

  3. How do we have different presenters present to the same view? or
    supply a different model to the same presenter?

    This may be required for forms that can handle more than one scenario, like new & edit.

  4. Sorry for the above anonymous comment (my mistake). Rick i have been following your excellent series and havn’t found any material better to read yet. Kindly, in the future, do explain on how to build a sample smart client application. I havn’t found any VS2005 (although VS2008 is out now) smart client sample application. I am interested in an app working in an offline/online mode.

  5. Thanks for taking the time to write all these, and I am looking forward to number 26 🙂

    I have looked at using SCSF for almost a year now and have always backed away again. But now the time came to do some serious user interface stuff in our SOA environment so I needed some finished plumbing. I stumbled onto your series yesterday, and I am now ready to get my feet wet. I have now started the downloads and tomorrow the first spike will be created…

  6. Oh no 26, btw.. richnewman, u have some more new stuff to write about with the release of SCSF 2008 🙂

  7. As a newbie with MVP I could use an article number 26 right about now. 🙂

    Any estimates on delivery?

    Morten

  8. Fantastic articles, I got better understanding on MVP and MVC pattern. Really appreciate. Thanks a lot!

  9. Please do not stop writing about SCSF and CAP! Your articles are the best documentation I have seen. You explanations are short and clear, there is no water and long introduction.

    And could you write a few articles about Security Application Block please?

    Thanks in advance!
    Stan

  10. Good Work!
    I was already familiar with CAB & a variation of SCSF that was developed internally, but I have to say that your articles have cleared up some important things for me. Especially Workitems, how are they used and the little nuances that you wouldn’t normaly know unless you went and read the CAB & SCSF code.

    I find the CAB/SCSF a very “promising” Framework that does provides many of the best practices any developer should use in thier applications. However, I still think it is not mature enough to be called a fully functional and easily usable/extensible framework.

    The learning curve is steep (even though you do help quite a bit) and I am not even sure that throught its development the p&p guys were excatly sure of everything they want to do. It’s still the best Framework of it’s kind geared towards to Microsoft’s presentation technology and .NET but it still has a long way to go before it reaches the level of clarity .NET has.

    That being said, I’m still going to use it, although one Guidance receipe that I am desperatly in need of is a “Create a Workitem with View/Presenter”. I think that would help in the logical separation of Use Cases instead of having to do it all by hand.

    In any case, looking forward to the rest of the articles, although I think No. 26 will come in September 😉

  11. I was throwed with the task to develop a smartclient application with SCSF and I didn’t know where to start. After reading all of your 25 articles, it’s like seeing light in the midst of total darkness. Thank you.

  12. Rich,

    Thanks very much for your great articles. I found the MS documentation and quick start tutorial almost useless so was a great relief to find your excellent articles. I have also bought the Composite UI Application Block and Smart Client Software Factory so will be interesting to see how the book fairs with your articles.

    In your last few articles you talk about the MVC and MVP patterns and passive and active MVC/MVP. Can you not have an active View without the view referencing the Model? For example could you not expose another Interface that the Presenter implements that will raise events when the model updates its state. So the Presenter would subscribe for changes in the model and this could then cause an event to be raised by the Presenter (event is in interface) which the View subscribes? Not sure if that is breaking the pattern or i have misunderstood something. Alternatively the presenter can subscribe to state updates from the model and update the view using the ITestView interface? Would that not achieve the active view without the view having to reference the model?

    Also, given the lack of decent documentation (apart from yours) and the number of comments regarding the difficulties of learning CAB what are your experiences when hiring new developers who have not worked with CAB? Do they have reservations about the framework or is it just a matter of taking the time to learn it and with time it becomes clearer?

    Kind Regards,

  13. Hi Rich

    When I try to do this or follow the examples, in the business module when I right click, I do NOT get an “Add View (with Presenter)” option only an “Create Disconnected Service Agent” in the Smart Client Factory right click menu..

    How do I get the add view with presenter option to work and/or do it by hand? This is with VS 2008 and latest Smart Client…

    Jordan

  14. Rich,

    I think your articles are very clear and easy to understand.
    I request you to provide us a link in ur site for examples download.

    I would read all your articles again about CAB and SCSF.
    Can you help us about displaying/relaoding a view in right workspace based on some selecctions/criteria in left workspace UI? -is it in any specific article..if not can u write a quick one..would be of great help for my project..Thanks a Ton!

    Rgds,
    SA.

  15. I’ve your 25 articles and they were really great at understanding and building a SCSF solution. Now, I see there is a lot of hype around Composite WPF. Question: Does this replace the SCSF and is it time to learn a new practice? How do the two guidance’s compare?

  16. HI Rich
    Many Many Thanks for such a great article series on CAB & SCSF
    Keep going the good work
    Eagerly waiting for new articles in this series…..!
    Thanks & Regards
    Swakesh

  17. Jordon,

    I have the same problem as you: no “Add View (with Presenter)” option only an “Create Disconnected Service Agent” in the Smart Client Factory right click menu.

    Did you find a solution for this?

  18. Hi Rich,
    first of all, I’d like to thank you so much!
    Your articles are simply super: they demistified the SCFS, they are well structured and good explained. They are a very good reference to beginners.
    The stuff is very simple to assimilate and understand, but Microsoft (my own opinion) make it very complex and ununderstandable.

    I’d like to add that if one perefers not to use the default implementation (WindowsPrincipalAuthenticationService) of the IAuthenticationService Service and writes his/her own, this must be added in the ShellApplication’s AddServices method and not in the AddServices of any other Module, as the Modules Loading & Initialization take place after the authentication. In the “Hospital example” of the book “Programming Microsoft® Composite UI Application Block and Smart Client Software Factory” of “The Mad Professor” (David S. Platt), an AutenticatinService is added in the AddServices of a loaded Module. This is wrong.
    In the same Example, the same AutenticatinService is added via the App.Config file: that’s what let the application (the Authentication) work.

    To Roy and Jordon:
    I had the same problem: the solution is to (unfortunately) generate a new SCSF Application and add a new BusinessModule. As long as I remove a (already added) BusinessModule, it’s not more possible to add a View (with Presenter), just the option “Create Disconnected Service Agent” is available.

    Meziano

  19. Rich, your articles are very well written and are a great start for anyone who wants to pick up CAB from the start of a project or get to understand it in an already ongoing project.

    To Roy and Jordon:
    I had the same problem and found why it was happening. The recipe that add the “Add View (with Presenter)” option to the right-click menu look for a reference to the Infrastructure.Interface.dll. If you changed the output name of this library after creating your SCFS Application, it may not find it and will not present the option. It also uses the RootNamespace you provided at the creation of the project. Rename the library back to Infrastructure.Interface insated of, for exemple, MyProject.Infrastructure.Interface and the recipe should work fine!

    Rider.

  20. For those that are having problems with VS 2008 SP1 not displaying the Add View menu item, a fix has been packaged as an MSI at http://artazor.wordpress.com/2008/12/07/scsf-april-2008-fix-for-visual-studio-2008-sp1/. It will work on new solutions only.

    The SCSF issues are also documented here http://smartclient.codeplex.com/Wiki/View.aspx?title=Known%20Issues%3a%20SC-SF%20April%202008%20with%20Visual%20Studio%202008%20and%20SP1%20Beta&referringTitle=SCSF%20Knowledge%20Base#Fix3

  21. Hi Rich,

    I hope you are well.

    We are missing the rest: we have know understood the basics, we need to know how to make this knowledge into practice.

    The following points are very important in my opinion:

    1- The startup sequence of SCSF Application??
    2- What is “Action Catalog Service” and how to use it?
    3- How to put the puzzle together?

    A real-world example (with a DB as Back-end) would be very welcome: it will shows us when the differnts modules will be loaded? When the different SmartParts will be shown?
    Hopping to read you soon..

    Meziano

  22. Hi Rich,

    Kindly Provide us some basic Examples to start with the Practical Implementation of SCSF, I am very much thankful to you for giving us this great Series of Articles, wish you all the success in your life

    Thanks and regards,

    Rajesh KUmar.S

  23. Many thanks for such a great series of articles! I have just finished reading part 25. Good luck in your work and thanks again for spending your time on it! If you’ll find time for part 26 it will be a day of interesting reading for many people.

  24. Great series of articles !!!
    u make the complicated CAB & SCSF easy to go…
    where can i find more articles you wrote about new technologies?

Leave a reply to Eric Cancel reply