Prism Document Chapter II initializing the PRISM application

Source: Internet
Author: User

This chapter discusses what is necessary to make a prism application start and run. The prism application needs to be registered and configured during the boot process, which is known as the bootstrap application.

What is bootstrapper?

Bootstrapper is a class that is responsible for initializing an application based on the Prism class library. By using bootstrapper, you will have more control over how to connect the Prism library component to your application.

The Prism class library includes a default abstract bootstrapper base class that can be used exclusively for any container. Many of the methods for bootstrapper classes are virtual methods. You can override these methods to make them suitable for your own custom bootstrapper.

Basic stages of the bootstrapping process

The Prism Library provides some additional base classes, derived from bootstrapper, and has a default implementation that is suitable for most applications. For the bootstrapper of your program, the only thing left for you to do is create and initialize the shell.

Dependency Injection

Applications built with prism depend on the dependency injection provided by the container. This library provides containers that work in unity (Unity application Block) or MEF (Managed Extensibility Framework), which allows you to use other dependency injection containers. Part of starting the boot process is configuring this container and registering the type with the container.

The Prism library includes the unitybootstrapper and mefbootstrapper two classes, which implements most of the functionality, These features are necessary for using unity or MEF as a dependency injection container in your application. In addition to the boot process shown in, each bootstrapper adds some container-specific steps.

Create Shell

In traditional Windows Presentation Foundation (WPF) applications, the URL of the launcher is defined in the App.xaml file. In a Silverlight application, it is specified by the RootVisual property in the App.xaml code.

In applications created with the Prism Library, the responsibility of bootstrapper is to create a shell or main window. This is because the shell relies on some services, such as region Manager, and needs to be registered before the shell is displayed.

Key decisions

When you decide to use prism in your application, there are some additional decisions to make:

    1. For your Dependency injection container, you need to decide whether to use mef,unity, or other containers. This will determine who you should use to provide the bootstrapper class and whether you need to create a bootstrapper for another container.
    2. You should consider some of the specific app services in your application, which will need to be registered in the container.
    3. Determine whether the logging service is built to meet your needs or, if necessary, create another logging service.
    4. Determine how the module will be discovered by the application: through explicit code declarations, through directory scanning module code properties, configuration, or XAML ...

More detailed references are provided in the remainder of the chapters.

Core programmes

Creating a startup sequence is an important part of building your prism application. This section describes how to create a bootstrapper and customize it to create a Shell, configure Dependency Injection containers, register application-level services, and how to load and initialize modules.

Create a startup program for your app (Bootstrapper)

If you choose to use Unity or MEF as your dependency injection container, it's easy to create a simple bootloader for your app. You need to create a new derived class, whether derived from mefbootstrapper or unitybootstrapper. Then, execute the Createshell method. Alternatively, you can override the Initializeshell method for a specific initialization of the shell.

Implement Createshell Method

The Createshell method allows the developer to specify a top-level window for the prism application. The shell is usually the main window or home page. This method is implemented by returning the Shell class of the application. In a prism application, you can create a Shell object, or specify it from a container, depending on the needs of your application.

In the following code example, you specify a Shell object by using servicelocator .

Override DependencyObject Createshell (){return servicelocator.current.getinstance<shell>() ; 4}    

Note : you will often see that servicelocator is used to instantiate instead of a specific dependency injection container. The Servicelocator is implemented by invoking a container, which is a good choice for decoupling the container from other code. You can also refer directly to using containers instead of servicelocator.

Implement Initializeshell Method

After you create the shell, you may need to run the initialization steps to ensure that the shell can be displayed. Depending on whether you are writing a WPF or Silverlight application, the implementation of the Initializeshell method will be different. For Silverlight apps, you'll use the shell as the visual Root of your application, as shown below.

void Initializeshell () {    Application.Current.RootVisual = Shell;} 

For WPF applications, you will create a shell Application object and set it as the application's main window (from the modularity QuickStarts for WPF).

void Initializeshell () {    Application.Current.MainWindow = Shell;    Application.Current.MainWindow.Show ();} 

Don't do anything about the basic implementation of Initializeshell . It is safe to do so.

Create and configure a list of modules

If you are creating a module application, you need to create and configure a list of modules. Prism uses An instance of Imodulecatalog to track which modules are available to the application, which modules may need to be downloaded, and where to put them.

Bootstrapper provides a modulecatalog protected property to represent the base implementation of this directory and the virtual Createmodulecatalog method. Returns a new Modulecatalogin the implementation of the base class; However, you can override this method to provide a different Imodulecatalog instance, as in the following code, which is in modularity wiyh MEF for The quickstartbootstrapperin the Silverlight QuickStart code.

Override Imodulecatalog Createmodulecatalog () {    //when using MEF, the existing Prism Modulecatalog is STI ll    //return Modulecatalog.createfromxaml (new"/modularitywithmef.silverlight; Component/modulescatalog.xaml", urikind.relative));}         

In this unitybootstrapper and mefbootstrapper two classes, the Run method calls the Createmodulecatalog method, and then sets the class's The modulecatalog property. If you override this method, it is not necessary to call the implementation of the base class, because you will replace the provided functionality. For more information on modules, see Chapter 4th, "Development of modular applications." ”

Creating and configuring Containers

Containers play a key role in the applications created by the Prism library. Either the Prism library or the application that is used to build it relies on a container for property and service injection. During the container configuration phase, some core services are registered. In addition to these core services, you may need specific application services to provide additional functionality because of the organization involved.

Core Services

The following table lists the core non-application specific services in the Prism library

Service Interface

Describe

Imodulemanager

Defines the interface used for the service to retrieve and initialize the application module.

Imodulecatalog

Contains metadata about the module in the application. The Prism library provides several different lists.

Imoduleinitializer

Initializing the module

Iregionmanager

Register and recycle regions, which is the layout of the visual container.

Ieventaggregator

A series of loosely coupled events between the publisher and the Subscriber.

Iloggerfacade

A log mechanism is encapsulated, so you can choose your own log mechanism. The Stock Trader Reference implementation (stock Trader RI) through the Enterpriselibraryloggeradapter class, using Enterprise Library Logging Application Block, as an example of how you use your own logs. The logging service is registered in the container through the boot program's Run method, which takes advantage of the value returned by the Createlogger method. Registering another logger in a container will not work; instead, the Createlogger method of the bootstrapper should be rewritten.

Iservicelocator

Allows the Prism library to access the container. This may be useful if you want to customize or extend the library.

Application-specific Services

The following table lists the application-specific services that are used in the stock Trader RI, which can be used as an example to understand the types of services that may be available in your application

Services in the stock Trader RI

Describe

Imarketfeedservice

Provide real-time market data. PositionSummaryPresentationModel Updates the location screen based on notifications received from the service.

Imarkethistoryservice

Provides market historical data that is used to display trend lines for selected funds.

Iaccountpositionservice

Provide a list of funds in the portfolio.

Iordersservice

Continue to submit buy/sell orders.

Inewsfeedservice

Provide a list of news items for the selected fund.

Iwatchlistservice

Processed when new observation items are added to the watch list.

Derived classes with two bootstrapper in Prism are valid,unitybootstrapper and mefbootstrapper. creating and configuring different containers involves similar concepts but with different implementations.

Create and configure the Unitybootstrapper the container in

The CreateContainer method of the Unitybootstrapper class simply creates and returns a unitycontainer new instance. In most cases, you do not need to change this functionality; However, the method is a virtual method and thus has flexibility.

After the container is created, it may need to configure your application. The Configurecontainer method in unitybootstrapper Registers a series of core prism services by default, as shown below

Note Example: When a module registers modules and services in its initialization method

UnityBootstrapper.cs

ProtectedVirtualvoidConfigurecontainer () {...If(usedefaultconfiguration) {registertypeifmissing (typeof (Iservicelocator),typeof (Unityservicelocatoradapter),True); Registertypeifmissing (typeof (Imoduleinitializer),typeof (Moduleinitializer),True); Registertypeifmissing (typeof (Imodulemanager),typeof (Modulemanager),True); Registertypeifmissing (typeof (Regionadaptermappings),typeof (Regionadaptermappings),True); Registertypeifmissing (typeof (Iregionmanager), true); Registertypeifmissing (typeof (Ieventaggregator), true); Registertypeifmissing (typeof (Iregionviewregistry), true); Registertypeifmissing (typeof (Iregionbehaviorfactory), typeof (Regionbehaviorfactory), true); }} 

The registertypeifmissing method of the bootstrapper determines whether the service has been registered---cannot be registered two times. Through configuration, allows you to override the default registration. You can also turn off default registration for any service; To do this, pass falseusing the overloaded bootstrapper.run method. You can also override the Configurecontainer method and disable services that you do not want to use, such as event aggregation.

Note: If you close the default registration, you must manually register the necessary services.

To extend the default behavior of Configurecontainer , simply overload the bootstrapper of your application and have a selective call to the base class implementation, as follows from the Quickstartbootstrapper from the Modularity for WPF (with Unity) is shown in the code for QuickStart. This implementation invokes the implementation of the base class, registers the Moduletracker type as a concrete implementation of the Imoduletracker , and registers the Callbacklogger as a single instance of Callbacklogger.

void Configurecontainer () {    base this. Registertypeifmissing (true this. Container.registerinstance<callbacklogger> (this. callbacklogger);}      

Create and configure the Mefbootstrapper the container in

The CreateContainer method of the Mefbootstrapper class has done the following several things. First, it creates a assemblycatalog and a catalogexportprovider. Catalogexportprovider allows the mefextensions component to provide a default egress for some prism types and also allows you to override the default type registration. CreateContainer then creates and returns a new instance of the Compositioncontainer that uses catalogexportprovider . In most cases, you do not need to change this functionality; However, the method is a virtual method and thus has more flexibility.

Note: In Silverlight, it is not possible to recycle a type of component due to security restrictions. However, Prism uses an alternative approach, which is to use the Assembly.getcallingassembly method.

After the container is created, you need to configure it for your application. The Configurecontainer implementation in Mefbootstrapper registers a series of core prism services by default, as shown in the following code example. If you override this method, carefully consider whether you should call the implementation of the base class to register the core prism service, or you might provide these services in your implementation.

ProtectedVirtualvoidConfigurecontainer () {This. Registerbootstrapperprovidedtypes ();}Protectedvirtual voidthis. Container.composeexportedvalue<iloggerfacade> (this this. Container.composeexportedvalue<imodulecatalog> (this this. Container.composeexportedvalue<iservicelocator> (new Mefservicelocatoradapter (thisthis. Container.composeexportedvalue<aggregatecatalog> (this               

Note : in Mefbootstrapper, the core services of Prism are added to the container as a singleton, so they can be placed throughout the application through the container.

In addition to providing the CreateContainer and configurecontainer methods,Mefbootstrapper also provides two ways to create and configure a MEF- used Aggregatecatalog. The Createaggregatecatalog method simply creates and returns a Aggregatecatalog object. Like other methods in mefbootstrapper ,createaggregatecatalog is a virtual method that can be rewritten if necessary.

The Configureaggregatecatalog method allows you to register with the add-on type of the Aggregatecatalog command. For example, Quickstartbootstrapper from the modularity with MEF for Silverlight QuickStart explicitly added Modulea and Modulec to Aggregatecatalog , as shown below.

ProtectedOverridevoidConfigureaggregatecatalog () {Base. Configureaggregatecatalog ();//ADD this assembly to export ModuletrackerThis. AGGREGATECATALOG.CATALOGS.ADD (New Assemblycatalog (typeof (Quickstartbootstrapper). Assembly)); // Module A is referenced in in the project and directly in CO De. thisnew Assemblycatalog ( Typeof (Modulea.modulea). Assembly)); // Module C is referenced in in the project and directly in CO De. thisnew Assemblycatalog ( Typeof (Modulec.modulec). Assembly))}                

Prism Document Chapter II initializing the PRISM application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.