
Lab2: Dependency injection
March 12, 2008The Dependency Injection pattern promotes loose coupling between a class and the objects it depends on. With the Dependency Injection pattern, you do not instantiate the dependencies explicitly in your class. Instead, a class gets the objects it requires from an outside source. With the Web Client Software Factory, you express dependencies declaratively in your class definition. In the declaration, you specify an interface; you do not specify a concrete implementation. This means you can easily replace the implementation of an object that a class depends on, without changing the source code of the class. This is especially useful if you want to be able to test your classes in isolation. The Composite Web Application Block includes a dependency injection framework based on ObjectBuilder that you can use to implement the Dependency Injection pattern in your application; this framework is used frequently throughout the software factory.
For more information about the Dependency Injection pattern, see “Dependency Injection” in Web Client Software Factory Help.
Module services are services that can be consumed only by the module that registers it. Global services are services that are available to all the modules in the solution.
To register module services, you implement the AddModuleServices method of the module initialization class of your business module.
A module initialization class is a class that implements the IModuleInitializer interface. This class contains initialization code that runs when the Composite Web Application Block loads the module. This interface defines two methods, Load and Configure. You implement these methods to perform module initialization tasks, such as registering services or adding site map nodes to the site map. For more information about module loading and the IModuleInitializer interface, see “Modules” in Web Client Software Factory Help.
Note: moduleServices is a services collection. In this particular case, it is the collection of services of the EFT module. You use the AddNew method to have ObjectBuilder create a new instance of the service and add it to the services collection. By adding a service to the services collection of the EFT module, you register it as a module service, and it will always be available at run time to be injected by the ObjectBuilder in the current module.
(EFT = general module. JUST IN THIS CASE!!!)
The AddNew method allows you to specify the type that is used to register the service (this is the second generic parameter in the line of code you added in step 3). This is the type developers use to obtain a reference to the service when using the Dependency Injection pattern. By specifying the service’s interface as the registration type, you enable developers to write generic code that is not aware of the concrete service implementation being used. This is especially useful when testing your classes because you can easily replace dependencies with mock implementations to test your classes in isolation.