Factory Pattern

When developing an application using inversion of control paradigm one can face the necessity of creating a factory object for crating instances of some class hierarchy. In traditional environment, factory method is responsible only for choosing which implementation to create in particular case, based on arguments provided by caller.
In the inversion of control environment, though, there are two additional tasks for a factory class:
  • Injecting dependencies to factory-produced objects
  • Enabling dependency injection infrastructure to inject interceptors into factory-produced objects
Classic solutions of this problems include:
  • Using main application container to create instances in the factory method. Main disadvantage of this method is requirement that factory-produced classes must be known and configured in the app-level container.
  • Creating instances using simple new operator and then passing constructed instance to the container for injecting interceptors. Disadvantages of this approach include resolving dependencies of produced objects at the level of factory and necessity of providing abstraction over injecting interceptors functionality.
The MetaContainer enables us to develop a much better solution. It is based on the ability of factory to create child container of main application container (represented as a IMetaContainer interface) and store it privately. This factory-level container can be filled with mappings specific for the class hierarchy the factory is created for. These mappings are unavailable to any external entities ensuring that no one can depend on particular implementation classes.

Last edited Mar 7, 2009 at 5:30 PM by Leszcz, version 2


No comments yet.