`
`4: Modular Application Development
`
`21out of 27 rated th s helpful
`
`Microsoftu
`patterns & practices
`proven practices for predictable rsults
`
`On this page:
`
`Download:
`
`Benefits of Building Modular Applications Prism's Support for Modular Appli(ation
`Development lCore Concepts IModule:The Building Block of Modu lar Applications
`I Module Lifetime Module Catalog Controlling When to Load a Module llntegrate
`Modules with the Application lCommunicate Between Modules Dependency
`Injection and Modular Applications I Key De(isions Partition Your Appli(ation into
`Modules Determire Ratio of Projects to l"lodules, XAP ond Module Factoting lUse
`Dependency Injection for Loose Coupling Core Scenarios ] Defining a Module
`Registering and Discovering Modules Registeing Modules in Cade, Registenng l'lodules
`Using a XAML File, Registetit)g lvlodules Using o Configurotian File, Discovering l"lodules rn o
`Doectaty Loading Modules llnitializing Modules Spe(ifying Module Dependencies
`Specifying Dependencier r/r Code lSpecify Dependencies in XAML Specify
`Dependenctes in Canfigurotrcn Loading Modules on Demand Specifying On-Demond
`Loading h Code, Specifyng On-Demond Laoding in XAI'11, Specifying An-Demoncl Laading
`in Configurction, Requesting An Demond Looding of o Module I Downloading Remote
`Modufes in the Backglound - Prcparing o lvlodule for Remote Dawnlood, Ttacking
`Don/nload Proqress I Detecting When a Module Has Been Loaded Modules in MEF
`Reg6tenng Modules n Cade Usng MEF, Discaverhg 14odules in o Diectaty Using MEF,
`Specifying Dependencies o Code Usuig l"1EF, Specifyng On-Demond Loading Using MEF,
`Pre0orino o Module fot Remate DawnLoad U5iro MFF More Information
`
`A mod!lar application ls an application that is div ded nto a set of functiona units {named modules) that can be integrated
`into a larger application. A client module encapsulates a port on of the application s overall functionalrty and typlcall,
`represents a set of related concerns. It can rncl!de a collection of related components, such as application features, including
`!ser interface and business logic, or pieces of applicatron nfrastructufe, such as applcation level services for logging or
`authenticating users. Modules are independent of one another but can commun cate with each other in a oosely .oupled
`fashron. Modular applications can make it easier for you to deve op, test, deploy, and extend your applrcation.
`
`For example, consrder a personal banking application. The user can access a vanety of functions, such as transferring money
`between accounts, paying bllls, and updating personal information from a single user lnterface (UI). However, behind th€
`scenes, each of these functions is encapsulated w thin a dis.rete module. These modules communicate with each other and
`with back end systems such as database se|vers and ureb services. Applicat on se|v ces integrate the various components
`wrthrn each of the different mod!les and handle the communrcatron with the user The !ser sees an lnteqrated view that looks
`like a srngle app ication.
`
`The follow ng illustration shows a design of a modu ar application with multiple modules.
`
`Module composition
`
`Tuesday, February 25, 2014
`
`Page I of 19
`
`MPHJ 2006
`
`
`
`Hosts all the visual
`comPonents.
`
`Provides the glue
`between the
`modules and the
`shell.
`
`Provides views,
`services, and other
`fu nctiona lity to the
`application.
`
`Modular Application
`
`Shell
`
`Prism
`
`ModuleManager
`
`Application
`Infrastructu re
`
`Modu le
`
`Modu le
`ls.."*1
`
`Module
`
`Benefits of Building Modular Applications
`You are probably already building a well archrtected applrcaton using assemblies, nterfaces, and classes, and employrng good
`oblect oriented design prlnciples. Even so, unlessgreatcare s taken, yourappllcation design may still be monolithic'(where
`all the functionality rs implemented n a t ghtly coupled way w thln the applicat on), which can make the appllcation difficult to
`develop, test, extend, and marntain.
`
`The modular applrcation approach, on the other hand. can he p you to ldenti{y the large scale functlonal areas of your
`application and allow you to deve op and test that f!nctional ty independ€nt y. Th s .an make development and testing easier,
`but lt can a so make your application more flexible and easler to extend in the future. The benefrt of the modular approach is
`that it can make your overall applicatron arch tecture more flexlble and marntarnab e because t aliows you to break yoLrr
`appllcation into manageable pieces. Each p ece encapsu ates specrfic funct onality, and each piece rs Integrated through clear
`but ooselv couoled communication channels.
`
`Prism's Support for Modular Application Development
`Prism prov des support for modu ar app ication development and for rLrn tinre module management within your appllcatlon
`Using Pr sm s modular deve opment funct onality can save you time because you don t have to imp ement and test your own
`modularity framework. Prism supports the fo low ng modular app icatron deve opment features:
`
`. A module catalog for registering named modules and each module's location;you can create the module catalog in the
`following ways:
`" By defining modules in code or Extensible Application lvarkup Language (XA[.41)
`. For Windows Presentation Foundation (WPF): By discovering modules in a directory so you can load all your
`modules without explicitly defining in a centralized catalog
`" For WPF: By defining modules in a €onfiguration
`file
`. Declarative metadata attributes for modules to support initialization mode and dependencres
`. Integration with dependency injection containers to support loose coupling between modules
`. For module loading:
`. Dependency management, including duplicate and cycle detection to ensure modules are loaded in the correct
`order and only loaded and initialized once
`" On demand and background downloading of modules to minimize application start up time; the rest of the
`modules can be loaded and initialized in the background or when they are required
`
`Core Concepts
`
`Tuesday. February 25. 2014
`
`Page 2 of 19
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com/en-u...
`
`This section introduces the core concepts related to modularity in Pflsm, includrng the IModule interface, the module loading
`process, the module catalog, communicating between modules, and dependency injection containers.
`
`IModule: The Building Block of Modular Applications
`A module is a logical collection of functionality and resources that is packaged in a way that can be separately developed
`tested, deployed, and integrated into an application. A package can be one or more assembhes, either as a loose collection or
`bundled together in a XAP file. Each module has a central class that is responsible for initializing the module and integrating its
`functionality into the application. That class rmplements the IModule interJace. The presence of a class that implements the
`IModule intedace is enough to identily the package as a module. The IModule interface has a single method, named
`lnitialize, within which you can implement whatever logrc rs required to initialize and integrate tl^re module's functionality into
`the application. Depending on the purpose of the module, lt can register views into composite user interfaces, make additional
`services available to the application, or extend the application's functionality. The following code shows the minimum
`implementation tor a module.
`
`public class l"lyModuIe : IModuIe
`{
`
`public void Initialize( )
`t
`
`// Do something here.
`
`)
`
`E ruote:
`
`Instead of using the initialization mechanism provided by the IModule interface, the Stock Trader RI uses a declarative,
`attribute-based approach for registering views, services, and types.
`
`Module Lifetime
`The module loading process in Prism includes the followlng;
`
`1. Registering/discovering modules. The modules to be loaded at run-time for a particular application are defined in a
`Module catalog. The catalog contains information about the modules to be loaded, their location, and the order in
`which they are to be loaded.
`2. Loading modules. The assemblies that contain the modules are loaded into memory. This phase may require the
`module to be downloaded from the web or otherwise retrieved from some remote location or local directory.
`3. Initializing modules. The modules are then initialized. This means creating instances of the module class and calling
`the Initialize method on them via the IModule interface.
`
`The following figure shows the module loading process.
`
`l',y'odule loading process
`
`Tuesday, February 25, 2014
`
`Page 3 of 19
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com./en-u...
`
`Module Catalog
`The Modul€Catalog holds information about the modules that can be used by the application. The catalog is essentially a
`collection of Modulelnfo classes. Each module is described in a Modulelnfo class that records the name, type, and location,
`among other attributes of the module. There are several typical approaches to filling the Modulecatalog with Modulelnfo
`instances:
`
`. Registering modules in code
`. Registerjng modules in XAML
`. Registering modules in a configuration file (WPF only)
`. Discovering modules in a local directory on disk (WPF only)
`
`The registration and discovery mechanism you should use depends on what your application needs. Using a configuration file
`or XAlvlL file allows your application to not require references to the modules. tJsing a directory can allow an application to
`discover modules without having to specify them in a file.
`
`Controlling When to Load a Module
`Prism applications can initialize modules as soon as possible, known as "when available," or when the application needs them,
`known as "on-demand." For Silverlight applications, modules can be downloaded with the application or in the background
`after the application starts. Consider the following guidelines for loading modules:
`
`. Modules required for the application to run must be downloaded with the application and initialized when the
`application runs.
`' lModules containing features that are almost always used in typical usage of the application can be downloaded inthe
`background and initialized when they become available.
`. Modules containing features that are rarely used (or are support modules that other modules optionally depend upon)
`can be downloaded in the background and initialized on-demand.
`
`Consider how you are partitioning your application, common usage scenarios, application start up time, and the number and
`size of downloads to determine how to configure your module for downloading and initialization.
`
`Tuesday, February 25, 2014
`
`Page 4 of l9
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com/en-u...
`
`Integrate Modules with the Application
`Prism provides the follor!rng classe5lo bootstrap youI app !aatrof: the UnityBootstiapper or the MefBootstrapper. These
`ciasses can be used to create aad conirglir'e lhe r]iodule nranager to discovea and load od!les. Yo! can ovei'lde a
`confrguration nrethod to registef nodules specrlied ;rr a XAML file, a confrgufation Iile, or a directory location In a few lines of
`code.
`
`Use the nrodule In:t:alize metl'rod to rntegrate lhe Inodule !v lh lhe rest ol the app|cation. The way you do lhls vafies,
`depending on the st.u.ture of your app icatior'r ard the content of the module. fhe iollowing are common things to do to
`Inteqrate youi modu e rnto your applcatron:
`
`. Add the module's views to the application's navigation structure. This is common when building composite UI
`applications using view discovery or view inJection.
`. Subscribe to aoDlication level events or services.
`. Register shared services with the application's dependency injection container.
`
`Communicate Between Modules
`lven tholrgh modules should have o\! coupirng between eaca olher, rt s aommon tor modules to communrcate wrth each
`other. There are several ioosely coupled communrcatlcn patterns, eaah wrth thelr o\r'rn stfer1gths. Typically, combrnatrons of
`these patterns are used to.reate the resulting solution. The io iow og are sonre of these pattems:
`
`. Loosely coupled €vents. A module can broadcast that a certain event has occurred. Other modules can subscribe to
`those events so they will be notified when the event occurs. Loosely coupled events are a lightweight manner oI setting
`up communication betlveen two modules;therelore, they are easily implemented. Howevet a design that relies too
`heavily on events can become hard to maintain, especially if many events have to be orchestrated together to fulfrll a
`single task. In that case, it might be better to consider a shared service.
`. Shared services. A shared service is a class that can be accessed through a common interface. Typically, shared services
`are found in a shared assembly and provide system wide services, such as authentication, logging, or configuration.
`. Shared resources.lf you do not want modules to directly communicate with each other, you can also have them
`communicate indirectly through a shared resource, such as a database or a set of web services.
`
`Dependency Injection and Modular Applications
`Containers like the Unrty Applrcat on B ock lUn tyl and Managed [xteis brl ty Framework (MEF) al]ow you to easlly use
`lnversron of Control (loc) and Dependency Injectron, whlch are po!/er1!l desrgn patterns that help to cornpose components In
`a loosely coupled fashion. It allows conrponents to obta n retefences to the other components that they depend on without
`having to hard code those references, thereby pfomotrng better code Te use and rinproved flexibr ity. Dependency Inject on is
`very useful when buildng a loosely coLrpied, modular applrcation Pnsnr rs desiqned to be agnostic aboutthe dependency
`rn]ectron conlalner used to compose conrponents withrn an appl(ation The choice ot contarnef rs up to you and wi largely
`depend on your appllcation requirements and preferences. llorn/ever, there afe two pr r'rc pal dependency inlectlon frameworks
`from Microsoft to considef Unity and M:F.
`
`The pattern! & pfa.tices Un ty Applicat on B ock provrdes a fully featured dependency inJection container. It supporls
`p.ope y based and constructor based Inject on and polcy lnjectro''1, whrch a lows yo! to t.ansparently Inject behav or and
`po clr between ccnrponents; it also supports a host of other features that are typ cal oi dependency Injection containers.
`
`Mtt (!arhi.h rs now pait of .NET tramework 4 and Si ver-lrght 4) provides support for building extensib e .NET appl catrons by
`sLrpporting dependency inJection based component coirpos t o.r and pfovides other ieatures that support modular
`eppllcation developnrent.It a lov,,s an applcatron to d scover components at [in trme and then to integfate those components
`Lntctheappicationinalooselycoupedway.f'lIF
`s a gfeat extensrbr ity and compos tion f.amework. ]t inclLrdes assembly and
`type drs.overy, type dependency resolution, d-.pendency rnJection and sorne nrce assembly and XAP down oad capabilties.
`P.ism s!ppofts takrng advantag€ of MEF features, as \,\re I as the tolou/rng:
`
`. Associating module types to their XAP location
`. Module registration through XAML and code attributes for both WPF and Silverlight
`. Module registration through configuration files and directory scans for WPF
`
`Tuesday, February 25, 201 4
`
`Page 5 of 19
`
`
`
`4: Modular Application Develo... http://msdn.microsofi.com/en-u...
`
`. State tracking as the module is loaded
`. Custom declarative metadata for modules when using l'y'EF
`
`Soth the Unrty afd MEF dependency nje.tof.ontainers \"/ork seamlessly \'! th Pfisnr.
`
`Key Decisions
`Thef stdecsionyouv,'lll rnake is ',rhether you !/arrt to deve op a nodLi ar solutron There are numerous benefits of bu ld ng
`modular app icatrons as drscLrssed In the prevrous sectron but the,e rs a cornfrtnrent rn terms of time and effort that yo! need
`to ]nake tc reap these beneil15. lf you de. de to clev.lop d rfcdrilaf solltion. thefe are seve.nl rnofe things to conslder:
`
`framework you will use. You can create your own modularity framework, use Prism, MEF, or another
`
`. Determine th€
`framework-
`' Determine how to organize your solution. Approach a modular architecture by defining the boundaries of each
`module, including what assemblies are part of each module. You can decide to use modularity to ease the development,
`as well as to have control over how the application will be deployed or if it will support a plug in or extensible
`archrtecture.
`. Determine how to partition your modules. Modules can be partitioned differently based on requirements, for
`example, by functional areas, provider modules, development teams and deployment requirements.
`. Determine the core services that the application will provide to all modulesAn example is that core services could
`be an error reporting service or an authentication and authorization service.
`. If you are using Prism, determine what approach you are using to register modules in the module catalog. For
`WPF, you can register modules in code, XANilL, in a configuration file, or discovering modules in a local directory on disk.
`For Silverlight, you can register modules in code or XAML.
`. Determine your module communication and dependency strat€gy. Ny'od ules will need to communicate with each
`other, and you will need to deal with dependencies between modules.
`. Determine your dependency injection container. Typically, modular systems require dependency injection, inversion
`of control, or service locator to allow the loose coupling and dynamic loading and creating of modules. Prism allows a
`choice between using the Unity, MEF, or another container and provides libraries for Unity or MEF-based applications.
`. Minimize application startup time. Think about on-demand and background downloading of modules to minimize
`application startup time.
`. Determine deployment requirements. You will need to thinkabout howyou intend to deploy you r application. This
`may affect the number of assemblies you put in a XAP. You might also pa(ition shared libraries such as the Prism
`Library to take advantage of assembly caching in Silverlight.
`
`The fext seat oi'rs Drov de deta s about some ol these aieari ofs.
`
`Partition Your Application into Modules
`Wheri you deveiop yo!r appl cat on In i nrodLr ar red fashrof. yorl struature the appi cation Into separate cllent rnodules thal
`.af be ndrvrd!al !i developed tested and deployed ta.h nrodule '\rll encaps!late a port on ot your app catron s overall
`lirnationalrty. One of th-. first d-"s gn dears ons yo! u/ havo to Innke rs to dca de ho[- to partrtron your app iaatron s
`faifctronalty rnto discrete modLr es
`
`A |noa]|ile should ercapsulate a set ol fe nted concern5 and have a d stinct set of respons brltres. A module aao represeft a
`vertca s r.e of the app icatron or a hor zoftal servrc-. layer Laigeapprcatons!!, llrkely have both types o{ modules
`
`An application with modules organized around vertical slices.
`
`Tuesday. February 25, 2014
`
`Page 6 of l9
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com/en-u...
`
`Applicetion
`
`CustomerModule
`
`ProductModule
`
`OrderModu le
`
`CustomersView
`
`ProductView
`
`Orde rView
`
`CustomerView
`
`ProductCatalog
`
`ShoppingCa rtView
`
`Custome rRepos itory
`
`Productlnventory
`
`ShoppingCart
`
`nrzed around honzontal
`
`SharedServicesModu le
`
`Auth entication
`
`Cach ing
`
`Logging
`
`U lModu le
`
`CustomerView
`
`ProductView
`
`ShoppingView
`
`A larger applicatron may have modu es organ zed with v€dical slices and horizontal layers. Some exanrples of modules include
`the following:
`
`. A module that contains a specific application feature, such as the News module in the Stock Trader Reference
`Implementation (Stock Trader RI)
`. A module that contains a specific sub-system or functionality for a set of related use cases, such as purchasing,
`invoicing, or general ledger
`. A module that contains infrastructure services, such as logging, caching, and authorization services, or web servrces
`. A module that contains services that invoke line of business (LOB) systems, such as Siebel CRM and SAP, in addition to
`other internal systems
`
`A module should have a minrmal set of dependenc es on other modu es. When a nrodu e has a dependency on another
`module, it sho!ld be loosely coupled by using interfaces defined ln a shared llbrary instead of concrete types, or by using the
`EventAggregator to communicate wlth other modules vra EventAggregator event types.
`
`The goal of modulaflty is to partitron the appllcation in sLrch a way that it remains flexible, rnaintainable, and stab e even as
`features and technologies are added and removed. The best way to accomp ish thls is to design your application so that
`modules are as ndependent as possib e, have well defined lnterfaces, and are as iso ated as possible.
`
`Determine Ratio of Proiects to Modules
`There afe several ways to create and package
`modules. The recommended and most common way is to create a s ngle
`modules separate and promotes proper encapsulation. It also makes it easier to
`assembly per module. This helps keep logical
`
`Tuesday. February 25, 201 4
`
`Page 7 of l9
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com/en-u...
`
`talk about the assenbly as the module bor.rndary as well as the packaglnq oi how yc! deploy the modLrle. However, nothing
`prevents a s ng e as5embly Jfom containlrg inultrple nrodLrles, and !r s{role.ases this may be prefeffed to minimize the
`|]armber of prolects n your solutron. For a large app|catron, it is !rot !ncommorr to have 10 50 modules. Sepa.ating each
`niod!le Into rts own pfolect adds a lot oi coorplexity to the solutro]l and can 5lo\'^,' dov,'n V sua Studlo pertororance. Sometirnes
`ri rnakes sense to break a module or set of modules into their own 5() ut on to manaoe thrs if vou choose to strck to one
`rrcdLrle par assefib y/Vis!al St!dro project.
`
`XAP and Module Factoring
`Fof Sllvef ght applications, modu es are typicaly pa.kaqed In separale XAP iiles thouqh In some cases, you may have more
`than one nlodu e per XAP You should consrder how many XAP frles you need ln ofder to m n Inrze the number and size of
`download requests requ||ed for the app rcatron to start up and enabie a ne\,! feature. If you choose to separate each module
`nto its o$rn prolect/assembly, you then need to declde whether to put each assembly rn l1s o\,1,n XAP for deployrnent or
`whethe! to lnc ude multiple assenrb les In a srngle XAP
`
`Some fa.tors that influence yo!r chorce of ri hether to nc ude nrultlple modu es rn a s ngle XAP or to separate them include the
`torowing:
`
`. Download size and shared dependencies. Each XAPfile has a small amount of additional size overhead in its manifest
`and .zip file packaging. Also, if there are common dependencies between the modules and they are not separated out
`to a dependent module or cached library, each XAP will include those dependent libraries, which could significantly
`increase the download size
`. The timing of when multiple modules are needed by the application.lf multiple modules are loaded and used at
`the same time, such as presenting views at the startup of the application, packaging them in a single XAP file might
`make the download slightly faster and helps ensure that both modules become physically available to the client at the
`same time. The modularity feature of Prism ensures that modules that indicate dependencies are loaded in the right
`ordet so that can be used to make sure there is no improper load order when the modules are split across multiple
`XAPS. But there is a small amount of performance overhead involved in making two downloads instead of one, even if it
`is the same total download size.
`. Module versioning. If different modu les will be developed on independent timelines and potentially deployed
`separately, you will want to put them into separate XAP5 so they can be marked with different versions more cleanly and
`updated independently.
`
`To avoid doy,nload ng the sanre assemblies more tlran once n separate XAPS thefe are t',lro approaches that can be used:
`
`. Factor shared dependencies into a separate infrastructure module and have the consuming modules take a dependency
`on that shared module.
`. Use Assembly Library Caching in Silverlight to put the shared types into a shared library that is downloaded once and
`cached bv Silverlioht instead of the Prism module downloader.
`
`Use Dependency Injection for Loose Coupling
`A mod!le may depend or components and servrces provlded by the host applcatron or by other modules- Pfisor supports the
`abrltyto reqrster dependencres between modules sc lhat they afe load-"d and nrtiallzed if the flghtorder Pnsm also supports
`the nltralzation of mod!les rnrhen they are oaded rrto lhe apqlrcation Dur|ng modu e Initialrzation, the module can fetrieve
`references to the addit onal aomponenls and seryiaes lt req!rres, and/or reg ster any aonlponents and senr'lces that I conta ns
`in order to make thern avai able to otlei lrodules.
`
`A rrodule shouid use an Indepe denl mecha]li5nl lo gel rNstan.es of exter na nterfa.es Instead ofdlreciy instanliatrng a
`concfete type, for exampie by usrng a dependena!, njectrol] contalner or tactory service. Dependency lnJeation contarners suah
`as Unity or MEF allow a type to automntically a(qulre instances of the inteffaces and types it needs throlgh dependency
`njectron Pfisnr lntegfates with both lJnrly a|d MEF to alor! a rrodule to eas ly use depefden.y injection.
`
`The fo lowr'rg dlagram shows the lyplcal sequence of operat ons r,vhen modules are loaded that need to acqulre or reg ste
`retererces to the components aod setu (e5.
`
`Example of dependency injection
`
`Tuesday, February 25, 2014
`
`Page 8 of 19
`
`
`
`4: Modular Application Develo... http://msdn.microsoft .com/en-u...
`
`ln this example, the Orde6Module assembly defines an OrdersRepository class (along with other views and classes that
`implement order functionality). The CustomerModule assembly defrnes a CustomersviewMod€l class which depends on the
`OrdersRepository, typically based on an interface exposed by the service. The application startup and bootstrapping process
`contains the followino steps:
`
`2.
`
`3.
`
`4.
`
`The bootstraooer starts the module initialization orocess, and the module loader loads and initializes the
`OrdersModule.
`ln the initialization ot the OrdersModule, it registers the OrdersRcpository with the container.
`The module loader then loads the CustomersModule. The order of module loading can be specified by the
`dependencres In the module metadata.
`The CustomersModule constructs an instance of the CustomerviewModel by resolving it through the containet The
`CustomerviewModel has a dependency on the OrdersRepository (typically based on its interface) and indicates it
`through constructor or property injection. The container injects that dependency in the construction of the view model
`based on the type registered by the OrdersModule. The net result is an interface reference from the
`customerviewModel to the OrderRepository without tight coupling betlveen those classes.
`E ruote:
`
`The interface used to expose the OrderRespository (lorderRepository) could reside in a separate "shared services"
`assembly or an "orders seryices assembly that only contains the service interfaces and types required to expose
`those services. This way, there is no hard dependency between the CustomersModule and the OrdersModule.
`
`Note that both modules have an implicit dependency on the dependency inJectron container. This dependency is
`iniected durinq module construction in the module loader.
`
`Core Scenarios
`This section describes the common scenarios you will encgunter when working with modules in your application. These
`scenarios include defining a module, registering and discovering modules, loading modules, initializing modules, specifying
`module dependencies, loading modules on demand, downloading remote mgdules in the background, and detecting when a
`
`Tuesday. February 25, 2014
`
`Page 9 of 19
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com./en-u...
`
`module has already been loaded. You can register and discover modules In code, in a XAML or application configuration file, or
`by scanning a local directory.
`
`Defining a Module
`A module is a logical collection of functionality and resources that is packaged in a way that can be separately developed,
`tested, deployed, and integrated into an application. Each module has a central class that is responsible for initiaIzrng the
`module and integrating its functionality into the application. That class lmplements the IModule interface, as shown here-
`
`,ct
`r{
`
`public class lvlyl1odu1e : Il4odule
`
`public void Initializeo
`{
`
`// Initialize modu 1e
`
`)
`
`)
`
`The way you implement the Initialize method will depend on the requirements of your application. The module class type
`initialization mode, and any module dependencies are defined in the module catalog. For each module in the catalog, the
`module loader creates an instance of the module class, and then rt calls the lnitialize method. Modules are processed in the
`order specified in the module catalog. The runtime initialization order is based on when the modules are downloaded,
`available, and the dependencies are satistied.
`
`Depending on the type of module catalog that your application is using, module dependencies can be set either by declarative
`attributes on the module class itself or within the module catalog file. The following sections provide more details.
`
`Registering and Discovering Modules
`The modules that an application can load are defined in a module catalog. The Prism l\4odule Loader uses the module catalog
`to determine which modules are available to be loaded into the applicatlon, when to load them, and in which order they are to
`be loaded.
`
`The module catalog is represented by a class that implements the IModulecatalog interface. The module catalog class rs
`created by the application bootstrapper class during application initialization. Prism provldes different implementations of
`module catalog for you to choose from. You can also populate a module catalog from another data source by calling the
`AddModule method or by deriving from Modulecatalog to create a module catalog with customized behavior-
`
`E Note,
`
`Typically, modules in Prism use a dependency injection container and the Common Service Locator to retrieve instances of
`types that are required for module initialization. Both the Unity and the l\,4EF containers are supported by Prism. Although
`the overall process of registering, discovering, downloading, and initializing modules is the same, the details can vary based
`on whether Unity or t\.,lEF is being used. The container specific differences between approaches are explained throughout
`th is topic.
`
`Registering Modules in Code
`class. Yo! can use this module catalog to programmatically
`The most basic module catalog is provided by the Modul€Catalog
`register modules by specifying the module class type. You can also programmatically specily the module name and
`initialization mode. To register the module directly with the Modulecatalog class, call the AddModule method ln your
`application's Bootstrapper class. An example is shown in the following code.
`
`lc#
`
`l l
`
`Tuesdav. Februarv 25. 20 | 4
`
`Page l0 of I 9
`
`
`
`4: Modular Application Develo... http://msdn.microsoft.com/en-u...
`
`protected override void ConfigureModulecatalogo
`i
`
`Type modulecType = typeof (Hodulec ) ;
`l,lodulecatalog. AddModuIe(
`new l4odulelnfo( )
`{
`
`l,loduleName = nodulecType , Name,
`l4oduleType = nodulecType . As semblyQualifiedNai€,
`
`In the preceding example, the modules are directly referenced by the shell, so the module class types are defined and can be
`used in the call to AddModule. That is why this example uses typeof(Module) to add modules to the catalog.
`
`E Notet
`
`If your application has a direct reference to the module type, you can add it by type as shown above; otherwise you need to
`provide the fully qualified type name and the location of the assembly.
`
`To see another example of defining the module catalog in code, see sto€kTraderRlBootstrapper.cs
`Reference Implementation (Stock Trader RI).
`
`in the Stock Trader
`
`E Note,
`
`The Bootstrapper base class provides the CreateModulecatalog method to assist in the creation of the Modulecatalog.
`By default this method creates a ModuleCatalog instance, but this method can be overridden in a derived class in order to
`create different tVpes of module cataloq.
`
`Registering Modules Using a )GML File
`You can define a module catalog declaratively by speciting it in a XATML file. The XAML file specifies what kind oI module
`catalog class to create and which modules to add to it. Usually, the .xaml file is added as a resource to your shell project. The
`module catalog is cre