Wednesday, November 24, 2010

Pds Training In Philippines

PRISM 4 - Part 2 Overview of PRISM

Agenda
• DI container container
• The role of DI in Prism
• Use container DI
• ServiceLocator

DI Box DI Box
( called Dependency Injection) is a design pattern aimed at removed from the system depending on the class and inject the objects implementing the interface. Verbal and musically: the container is a kind of map, to which record links between the interface and class on a key value. If you need an object that implements the interface that the container indicating the polls the most appropriate key (interfaces), which in return we get the value (class). This approach makes them independent
our application on the specific implementation of which will be very easy to replace the mechanism of operation, eg

 ICalendarDataService 
public interface {void
GetAllCalendars (Action \u0026lt;Calendar> result, object state);}


interface has declaration of the method that returns all klendarze.

 public class AzureCalendarDataService: ICalendarDataService 
{public void
GetAllCalendars (Action \u0026lt;Calendar> result, object state)

{/ / get calendars with SQL Azure

}} public class

XMLCalendarDataService: ICalendarDataService
{public void
GetAllCalendars (Action \u0026lt;Calendar> result, object state) {

/ / get calendars from the XML file

}} public class

EFCalendarDataService: ICalendarDataService
{public void
GetAllCalendars (Action \u0026lt;Calendar> result, object state) {

/ / get calendars from the DB using the Entity Framework

}}

These classes implement our interface downloading calendars from different data sources.
Because the application is based on interfaces, does not need to know where data is retrieved. Substitution
data source is registered in the container of the appropriate class for your interface ICalendarDataService .
Benefits are interesting (testable, simple configuration, independence, etc.).

role in the DI container
Prism Prisma Concept is the ability to create applications composed of modules. Not really interested in how the functionality will be implemented. Important module indicated that implemented the interface.

implementation of Prism provides two containers:
- MEF,
- Unity.

To use them, just to make our Boostraper podziedziczył the appropriate class. And just for the MEF will MefBootstraper, and it will be for Unity UnityBootstraper. Of course, if you like StructureMap, AutoFac or another DI container, there is nothing in the way to write their own implementation of recording your favorite Boostrapera Konter.

use DI container
Frequently used commands when working with a container of DI are:
- registration type
- Collection of instances of the type of container,
- registration set instance (Singleton),
- injecting objects to the constructor

registration type container Unity
 this.Container.RegisterType \u0026lt;ICalendarDataService, XMLCalendarDataService> (); 

type registration in the container MEF
 [Export (typeof (ICalendarDataService))] public class 
XMLCalendarDataService: ICalendarDataService
} {


Downloading instance type of container Unity
 var inch = this.Container.Resolve \u0026lt;ICalendarDataService> (); 

Downloads instance of the type of container MEF
 var inch = this.Container.GetExportedValue \u0026lt;ICalendarDataService> (); 

registration set instance (Singleton) in the Unity container
 container.RegisterInstance \u0026lt;ICalendarDataService> (new AzureCalendarDataService); 

registration set instance (Singleton) in the container MEF
 this.Container.ComposeExportedValue \u0026lt;ICalendarDataService> (this.serwisKalendarza); 

Injecting objects to the constructor. In Unity
this operation happens automatically or, for example public class
 CalendarViewModel 
{
private IEventAggregator _eventAggregator;

public CalendarViewModel (IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;

}}

will close _eventAggregator initializing the object registered in the appropriate container, Unity.

The MEF does this by analogy with the fact that we have put the class attribute

 [ImportingConstructor] public class 
CalendarViewModel

These examples are just quick downloading. For details, refer to the addresses:
http://unity.codeplex.com
http://mef.codeplex.com

ServiceLocator
ServiceLocator is simply an overlay of our containers to facilitate you try to change the container during the application development process.
only option offered is the ability to download ServiceLocator object type and is in addition wrapped object by which we are still impinge on an interface.
 ServiceLocator.Current.GetInstance \u0026lt;IRegionNavigationService> (); 

ServiceLocatorem Cool thing of is that it is a static class available in the entire application so useful in situations where you need to download something from the container, and we do not have him Access our example, we create style ViewModel'ew Blendability, where the instance is created by the View (View First strategy).

0 comments:

Post a Comment