Composite WPF

33
Composite Smart Clients with WPF Composite Application Guidance for WPF – “PRISM” Max Knor Developer Evangelist Microsoft Austria http://www.knor.net/

Transcript of Composite WPF

Page 1: Composite WPF

Composite Smart Clients with WPFComposite Application Guidance for WPF – “PRISM”

Max KnorDeveloper Evangelist

Microsoft Austria

http://www.knor.net/

Page 2: Composite WPF

2

Agenda

A solution for…

Complex WPF Applications

Developing in Modules

Composite Smart Clients

No introduction to WPF

Page 3: Composite WPF

3

Your WPF Application ?!

Page 4: Composite WPF

4

Your WPF Application ?!

Page 6: Composite WPF

6

WPF Composite Application Guidance

Solution Package by Patterns & Practices Team

Available in Source

http://www.codeplex.com/CompositeWPF/

Reference Implementation (Stock Trader RI)

Composite Application Library (Framework)

Guidance (Documentation)

http://www.codeplex.com/CompositeWPFContrib/

Community Add-Ons

OutlookBar, Infragistics Support, …

Page 7: Composite WPF

7

Architecture of Composite Smart Clients

Module

View Presenter

Model

Main Application

Shell

ShellPresenter

Custom Module Services

Serv

ice

/ D

ep

en

de

ncy

Inje

ctio

n C

on

tain

er

Core-Services

Module-Loader Svc.

RegionRegionRegion

ModuleEnumerator

ModuleLoader

RegionManger

Logging

EventAggregator

Page 8: Composite WPF

8

Starting the Application

Bootstrapper

Application

Initialize Container

Display Shell

Initialize Modules

Shell

Region

Unity Container

ViewModule

Enumerator

Module Loader

Module 1

Module 2

Module 3

Module 4Initialize

Region ManagerSomeClass

Region Manager

Page 9: Composite WPF

9

Dependency Injection mit Unity

this.Container.RegisterType<IMyView, MyView>();

this.Container.RegisterType<IShell, ShellWindow>();

1.) Register types with UnityContainer:

IMyView view = this.Container.Resolve<IMyView>();

2.) Instantiate a new object (resolve instead of new)

public class MyView : UserControl, IMyView

{

public MyView(IUnityContainer container, IShell shell)

{

// container & shell wurden automatisch befüllt

}

}

Constructor is called, dependencies are filled automatically

Page 10: Composite WPF

The Shell

Page 11: Composite WPF

11

The ShellToolbars, ….

Regions

Page 12: Composite WPF

12

Starting the Application

Bootstrapper

Application

Initialize Container

Display ShellShell

Region

Unity Container

Region ManagerSomeClass

Page 13: Composite WPF

demo

Shell & RegionsBootstrapper & Container

Page 14: Composite WPF

Modules

Page 15: Composite WPF

15

Modules

Grouped by UseCases, Functionality, …

No reference to the Shell

Shared Components

Services, Interfaces, … in Shared Libraries

Modules are dynamically loaded

At runtime

Page 16: Composite WPF

16

Starting the Application

Bootstrapper

Application

Initialize Container

Display Shell

Initialize Modules

Shell

Region

Unity Container

Module Enumerator

Module Loader

Module 1

Module 2

Module 3

Module 4Initialize

Region ManagerSomeClass

Page 17: Composite WPF

demo

ModulesModule Loading

Page 18: Composite WPF

18

Dynamic Module Loading

public class MyBootstrapper : UnityPrismBootstrapper

{

...

protected override IModuleEnumerator GetModuleEnumerator()

{

return new StaticModuleEnumerator()

.AddModule(typeof(SomeModule))

.AddModule(typeof(AnotherModule), new[] {"SomeModule")

.AddModule(typeof(YetAnotherModule), new[] {"AnotherModule"

})

}

}

public class MyBootstrapper : UnityPrismBootstrapper{...protected override IModuleEnumerator GetModuleEnumerator(){return new DirectoryLookupModuleEnumerator(@".\Modules");

}}

Searching for modulesin directory

Page 19: Composite WPF

Views & Controllers

Page 20: Composite WPF

20

Implementing the Views

Seperation of UI, logic and data

Presentation Model

Responsible for data loading (from model)

Instantiates the view

View

Binding to PM‘s commands and data

ViewPresentation

ModelModel

Page 21: Composite WPF

21

Starting the Application

Bootstrapper

Application

Initialize Container

Display Shell

Initialize Modules

Shell

Region

Unity Container

View (…)Module

Enumerator

Module Loader

Module 1

Module 2

Module 3

Module 4Initialize

Region ManagerSomeClass

Region Manager

Page 22: Composite WPF

22

public class MyModule : IModule{public void Initialize (IRegionManager manager){

RegisterViewsAndServices();

IRegion orderRegion =manager.GetRegion("OrderRegion");

var myOrderView = new OrderView(myOrder);orderRegion.Add(myOrderView);

}}

Displaying views

Region manager is needed

View is created

Region is retrieved

View is shown

Page 23: Composite WPF

demo

View & PresentationModell

Page 24: Composite WPF

Communikations

Page 25: Composite WPF

25

Loosely coupled communication

Requirements: No direct dependencies

Shared Services

Event Aggregator

Weak Multicast Events

Commands

Page 26: Composite WPF

26

Commands

public interface ICommand{

event EventHandler CanExecuteChanged;bool CanExecute(object parameter);void Execute(object parameter);

}

<Button Command="..." />

MyCommand

Execute() CanExecute()

Page 27: Composite WPF

demo

CommandsEventAggregator

Page 28: Composite WPF

28

Shell Extensibility

Modules can extend the shell with customtoolbars

ShellWindow implements Interface

IExtendableShell

Access via UnityContainer

Modules are only aware of IExtendableShell

Breaking the dependency to shell implementation

Page 29: Composite WPF

29

Shell Extensibility

public ShellWindow : Window, IExtendableShell { .. }ShellWindow.cs:

this.Container.RegisterType<IExtendableShell, ShellWindow();MyBootstrapper.cs:

var shell = this.Container.Resolve<IExtendableShell>();

shell.ShowStatusBarText("bla"); //nur IExtendableShell sichtbar

Im Modul:

IExtendableShell.cs:public interface IExtendableShell

{

void AddToolBarItem(object identifier, string text,

ICommand command);

void RemoveToolBarItems(object identifier);

void ShowStatusBarText(string text);

}

Page 30: Composite WPF

30

Composition Commands

Submit

OrderDetails

Submit

OrderDetails

Submit

OrderDetail

s

Delegate Commands

Composite

Command

Submit

OrderDetails

Submit All

Page 31: Composite WPF

31

Summary

Composite Smart Clients

Extensible Architecture

Build in a modular way

Independent modules enable

Parallel Development of modules

Flexible deployment

Composite Application Guidance

Library providing Infrastruktur

Tips

Interfaces reduce dependencies (z.B.: UI Testing)

Event Aggregator can get evil (event loops)

Page 32: Composite WPF

32

Contact

Max KnorDeveloper Evangelist

Microsoft Austria

http://www.knor.net/

Page 33: Composite WPF