Dependency Injection Container

StyleMVVM comes with its own built in dependency injection container (A.K.A IOC Container). This container is slightly different from other containers in a couple respects.
  • You can export multiples types under the same Export name or Type. At Location time the best export will be chosen based on what Environment you are in (Run Time, Design Time, Unit Test time) as well as the priority of the export. (You can change sort order by setting CustomSortExportMethod on the container)
  • You are allowed to pass constructor parameters to exports at creation time as long as they aren't marked as shared.
  • Some types are auto registered for you specifically Pages, UserControls and BaseClient<T> (WCF proxies). This is a convenience and can be turned off.

Configuration can be done in one of two ways.
  • Attributes - StyleMVVM provides a large set of attributes that can be used to control how classes are exported (Attribute configuration is not available to C++/CX and C# WinRT components).
  • Fluent Interface - There is a set of fluent interfaces that allow you to export assemblies, types and Func<T>

Attribute Configuration

Export Attributes

  • ExportAttribute - This attribute allows you to mark an object for export. You have the chose to export by type or by name (string).
  • ExportByInterfaceAttribute - Marks a class to be exported by the interfaces it implements.
  • ExportEnvironmentAttribute - This attribute specifies what environment the class should be exported in. There are current 3 export environment RunTime, DesignTime, UnitTestTime. When there are multiple export of the same type or the same name they are sorted by Environment first then Priority
  • ExportPriorityAttribute - Allows you to specify a priority for your export within the context of the ExportEnvironment
  • SharedAttribute - Classes marked with this attribute will share an instance between consumers (including if the class is exported under multiple names or types). You can optionally set the permanent property that will hold the instance for the lifetime of the container.

Import Attributes

  • ImportAttribute - This attribute is used to mark a property for import at the time of activation. By default the property type is used for dependency injection resolution (it's equavilent to x.Property = container.Locate<PropertyType>()). If an export cannot be found an error is logged and the construction of the object stops there.
  • ImportConstructorAttribute - This attribute is to be used on one constructor to mark it to be called on activation. The parameters for the call will be resolved by type against the current container. Note: you can use the ImportAttribute on individual parameters of the constructor to control resolution.
  • ImportMethodAttribute - With this attribute you can mark a method to be called at activation time. The parameters for the method will be resolved against the DI container. Note: you can use the ImportAttribute on individual parameters of the method to control resolution.

Other Attributes

  • CoreExportAttribute - This attribute allows you to mark a class to be instantiated at the time of Container startup. The startup order can be controlled if your export should be started in a particular order.
  • DesignTimeOnlyAttribute - Classes Marked with this attribute will only be exported when the container is configured for DesignTime.
  • NonRunTimeExportAttribute - Classes marked with this attribute will be exported during DesignTime and UnitTestTime but not during runtime (usually a Faux class).

Last edited Oct 31, 2012 at 1:30 PM by ipjohnson123, version 6

Comments

No comments yet.