Showing posts with label PRISM. Show all posts
Showing posts with label PRISM. Show all posts

Monday, 30 July 2012

PRISM : Explained

Using Prism you can create a WPF application , the client-side of a silverlight application and a Windows Phone applications.

The client side application of PRISM generally has a Shell, a few modules and the infrastructure to connect these:
  • Shell
  • Modules
  • Infrastructure 

Namespace

Namespace used in prism are
  • Microsoft.Practices.Composite.dll
  • Microsoft.Practices.Composite.Presentation.dll
  • Microsoft.Practices.Composite.UnityExtensions.dll
  • Microsoft.Practices.Unity.dll
  • Microsoft.Practices.ServiceLocation.dll

Shell

The foundation of the application is “The Shell”.  The Shell is the top-level window of an application based on the Prism Composite Application Library. This window is a place to host different user interface (UI) components that exposes a way for itself to be dynamically populated by others, and it may also contain common UI elements, such as menus and toolbars. The Shell window sets the overall appearance of the application.

The shell that is responsible to load the Modules.

Module 

A module represents a set of related concerns. It can include components such as views, business logic, and pieces of infrastructure, such as services for logging or authenticating users. Modules are independent of one another but can communicate with each other in a loosely coupled fashion.

Infrastructure 

The Infrastructure Assembly is a shared library referenced by both the shell project and the module projects, and holds shared types such as constants, event types, entity definitions and interfaces.

Looking inside Shell

Shell contains BootStrapper.cs

The Bootstrapper component is used by the application to initialize the various Prism components and services. It is used to initialize the dependency injection container to register any application-level components and services with it. It is also used to configure and initialize the module catalog and the shell's view and view model or presenter.

The Bootstrapper inherits from 
  • Microsoft.Practices.Composite.UnityExtensions.UnityBootstrapper 
To be able to run the bootstrapper at least one module should be registered in our application. 


Shell contains Shell.xaml on to which modules are loaded.

Modules can be loaded in the regions defined in Shell.xaml.

Regions


Regions are the placeholders for the controls defined in Modules.  Modules can be added into the shell one by one , generally we add one module at begining and then continue adding more and more.
Modules can be added to regions in shell.xaml and can be added/removed at runtime.Because our application is build with modularity in mind it’s the Module and not the Shell that should define which views need to be added to the regions.

Region Manager Service

Views are created inside modules.To add views to our regions we use the prism region manager service. The region manager service is responsible for maintaining a collection of regions and creating new regions for controls.  Typically, we interact directly with region manager service to locate regions in a decoupled way through their name and add views to those regions. By default, the UnityBootstrapper base class registers an instance of this service in the application container. This means that we can obtain a reference to the region manager service in our application by using dependency injection.

So, this is how my Shell and Module talk to each other.





and following shows the basic terms in PRISM



Bootstrapper – let’s get this party started!


  • Kicks off the application
  • Starts the main UI container (the Shell)
  • Registers Modules and loads, if needed
  • Registers any global singletons (optional)

Shell – the main view (might be a master page)


  • The main UI container
  • Houses all of the Views that will be loaded
  • Can be split into Regions
  • Knows nothing of what will be loaded into it

Regions – content areas


  • Area(s) in the Shell where Views can be placed
  • Are given a name
  • Can contain context, if needed
  • RegionManager exists to help maintain Regions

Modularity – self contained modules


  • To the user this is seamless
  • Can be developed separately
  • Does not reference other modules
  • Solution is split into Modules
  • Modules share infrastructure and Models

Inversion of Control (IoC)


  • Unity or other Dependency Injection (DI) Tools 
  • Helps for test ability and mocking
  • Abstraction
  • Container object allows classes to be registered against their interfaces
  • When an interface is requested, the container creates the class registered with the interface
  • Supports singletons

Infrastructure – common tools


  • A Silverlight class library project
  • Contains shareable items for the modules
  • Classes
  • Assets
  • Resources
  • Makes no references
  • A pure library

Commanding – Action & Reactions


  • Allows events between a View and a ViewModel through Data Binding
  • ViewModel declares the Command receiver
  • Command is declarative in XAML
  • Button – Click,ListBox (Selector) – Selected
  • Command is data bound to the ViewModel’s command receiver
  • Can be disabled/enabled based on rules

Event Aggregation: Publish – Subscribe pattern


  • Allows events of any kind to be published and subscribed to
  • Can be cross module
  • Can be filtered by subscribers
  • For example:
  • Click on a menu item in the Shell
  • Event is invoked by the publisher
  • Event is received by the subscriber
  • The subscriber then loads a View in a Region in the Shell


Conclusion

Thinking of a Prism as a set of options is probably the best way.One pick only the features one really needs, and simply skip the rest. Architecture of Prism provides flexibility, and that is the first advantage of adapting it to project.

You can also read this article at dotnetspider.

Friday, 27 July 2012

PRISM : An Introduction

What is PRISM?


Prism (or Composite WPF) is a framework provided by Microsoft to help build composite WPF ,Silverlight and Window Phone 7 applications.
It has mechanisms for

  • UI composition,
  •  Module management and 
  • Dependency injection.
Prism uses the design concepts of 
  • Separation of concern
  • Loose Coupling

 Prism helps you to design and build applications using loosely coupled components that can evolve independently but which can be easily and seamlessly integrated into the overall application. These types of applications are known as composite applications.

Advantages of PRISM

  • Create an application from modules that can be built, assembled, and, optionally, deployed by independent teams using WPF or Silverlight.
  • Minimize cross-team dependencies and allow teams to specialize in different areas, such as user interface (UI) design, business logic implementation, and infrastructure code development.
  • Use an architecture that promotes reusability across independent teams.
  • Increase the quality of applications by abstracting common services that are available to all the teams.
  • Incrementally integrate new capabilities.

Important terms in PRISM

Shell:The main window of a WPF application or the top-level UserControl of a Silverlight application where the primary UI content is contained.

Region :A named location that you can use to define where a view will appear. Modules can locate and add content to a region in the layout without exact knowledge of how and where the region is visually displayed. This allows the appearance and layout to change without affecting the modules that add the content to the layout.

Region Manager:The class responsible for maintaining a collection of regions and creating new regions for controls. The RegionManager finds an adapter mapped to a WPF or Silverlight control and associates a new region to that control.

Regions allow you to define named placeholders in your main view (the “Shell”). When you initialize your module, you can inject views (e.g. user controls) into these placeholders by specifying their name. This helps separate the application layout from specific views, and allows injecting other views at run time (such as add-ins).

Region Context is a technique that can be used to share context between a parent view and child views that are hosted in a region. The RegionContext can be set through code or by using data binding XAML.


Bootstrapper:The class responsible for the initialization of an application built using the Prism Library.

Composite application: A composite application is composed of a number of discrete and independent modules. These components are integrated together in a host environment to form a single, seamless application.

Event Aggregator:A service that is primarily a container for events that allows publishers and subscribers to be decoupled so they can evolve independently. This decoupling is useful in modularized applications because new modules can be added that respond to events defined by the shell or other modules.

Module:A logical unit of separation in the application

Module Catalog:Defines the modules that the end user needs to run the application. The module catalog knows where the modules are located and the module's dependencies.

Module Manager:The main class that manages the process of validating the module catalog, retrieving modules if they are remote, loading the modules into the application domain, and invoking the module's Initialize method

View Discovery:A way to add, show, or remove views automatically in a region by associating the type of a view with a region name. Whenever a region with that name displays, the registered views will be automatically created and added to the region.

View Injection: A way to add, show, or remove views programmatically  in a region by adding or removing instances of a view to a region. The code interacting with the region does not have direct knowledge of how the region will handle displaying the view.


PRISM Architecture



When to Use View Discovery vs. View Injection

View discovery is a automatic and simple approach to composing views and getting them displayed in a region. In general, View discovery is used , but you can use view injection if you need one of the following:

  • Explicit or programmatic control over when a view is created and displayed, or when you need to remove a view from a region, for example, as a result of application logic.
  • To display multiple instances of the same views into a region, where each view instance is bound to different data.
  • To control which instance of a region a view is added (for example, if you want to add customer detail view to a specific customer detail region). Note that this scenario requires scoped regions described later in this topic.

What PRISM cannot do

  • Occasional connectivity and data synchronization
  • Service and messaging infrastructure design
  • Authentication and authorization
  • Application performance
  • Application versioning
  • Error handling and fault tolerance