This article is mainly used to introduce the core services based on the Prism library and how to configure the container, there is an important part of how to manage the dependencies between the various components, the following is a one by one introduction to this content.
1 Core Services in Prism
Imodulemanager: Defines the interface for the service that can retrieve and initialize the various modules of the application.
Imodulecatalog: Contains metadata for each module of an application, and this prism Libray several different catalog.
Imoduleinitializer: Initializes each module of the application.
Iregionmanager: Register and retrieve regions (as a view area), which is a visual container for the entire layout.
Ieventaggregator: It is a collection of events that are loosely coupled between the publisher of an event and the Subscriber.
Iloggerfacade: It's a wrapper for a recording mechanism, so you can choose your own logging mechanism, and when the container uses the bootstrapper run method, the logging service is registered, and using container to register another logger (logger) is not valid.
Iservicelocator: Allows you to get containers through the prism library, which is useful if you want to customize or extend the current prism Libray.
2 Creating and configuring container in Unitybootstrapper
The Unitybootstrapper class provides a CreateContainer method that creates and returns an instance of UnityContainer, and in most cases you don't need to change that. This CreateContainer () method is a virtual method, so it is quite flexible.
Once this container is created, you need to configure your application, and this configurecontainer () defaults to registering a series of prism services, so let's look at the default implementation of this method.
protected virtual void Configurecontainer () {... if (usedefaultconfiguration) {registertypeifmissing (typeof (Iservicel Ocator), typeof (Unityservicelocatoradapter), true); Registertypeifmissing (typeof (Imoduleinitializer), typeof (Moduleinitializer), true); Registertypeifmissing (typeof (Imodulemanager), typeof (Modulemanager), true); Registertypeifmissing (typeof (Regionadaptermappings), typeof (Regionadaptermappings), true); Registertypeifmissing (typeof (Iregionmanager), typeof (Regionmanager), true); Registertypeifmissing (typeof (Ieventaggregator), typeof (Eventaggregator), true); Registertypeifmissing (typeof (Iregionviewregistry), typeof (Regionviewregistry), true); Registertypeifmissing (typeof (Iregionbehaviorfactory), typeof (Regionbehaviorfactory), true); }}
This bootstrapper registertypeifmissing method to determine whether a service has been registered, and does not continue to register, by overloading this method, you can also close the registered services, and register their services, in the following code, for example, Explains how to sign up for your service.
protected override void Configurecontainer () { base. Configurecontainer (); This. Registertypeifmissing (typeof (Imoduletracker), typeof (Moduletracker), true); This. Container.registerinstance<callbacklogger> (This.callbacklogger);}
Below we will analyze Moduletracker and Imoduletracker by code.
This class and interface is our own definition for managing our own classes, first of all to introduce Imoduletracker.
Public interface Imoduletracker { void recordmoduleconstructed (string modulename); void Recordmoduledownloading (String modulename, long bytesreceived, long totalbytestoreceive); void recordmoduleinitialized (String modulename); void recordmoduleloaded (String modulename); }
According to the meaning of the nomenclature, it is not difficult to see the meaning of each of the specific methods, recordmoduleconstructed (string modulename) is used to record the current module is constructed by the constructor, Recordmoduledownloading (String modulename, long bytesreceived, long totalbytestoreceive) records that the current module is downloaded over the network, Recordmoduleinitialized (String modulename) indicates that the current moudule has gone through the initialization phase, in addition to recordmoduleloaded (string modulename) Used to record that the current module has been loaded. Moduletracker is inherited from the concrete implementation of Imoduletracker, here too many code is not explained, is only a sketchy way to tell the basic concepts and principles.
Study on Prism frame (III.)