Register Assembly
    using Grace.DependencyInjection;

    DependencyInjectionContainer container = new DependencyInjectionContainer();

    container.Configure(c => c.ExportAssembly(YourAssembly));

    ISomeInterface someInstance = container.Locate<ISomeInterface>();

Above is an example of how to register an assembly to be scanned for classes using attributes.

Export Type
    using Grace.DependencyInjection.Attributes;

    [Export(typeof(ISomeInterface))]
    public class SomeClass : ISomeInterface
    {
       public SomeClass(ISomeOtherClass otherClass)
       {

       }
    }
    
   [Export(typeof(ISomeOtherClass))]
   public class SomeOtherClass : ISomeOtherClass
   {

   }

Above is an example of how to export two classes. By default constructor parameters will be resolved using the Dependency Injection container


ExportAttribute

The export attribute can be used on a class to export a class under a particular name or type. If no name or type is provided the classes full name is used to export the class.

ImportAttribute

The import attribute can be used in multiple different contexts. Below is a list of the different uses and what it means
  • On Property - When used on a property the property will be injected upon activation
  • On Constructor - When a class has multiple constructors one constructor needs to be attributed as the importing one.
  • On Method - Methods that are attributed with Import will be called during activation. Parameters will be resolved using the current scope
  • On Parameter - When the Import attribute is used on a Parameter (method or constructor) it will control how the value is imported (Required flag, required name, etc).

ActivationCompleteAttribute

Methods that are attributed with this attribute will be called at the end of activating a type. The method signature needs to either be Action or Action<IInjectionContext>.

SingletonAttribute

Classes that are attributed with the Singleton Attribute will be shared for the life time of the container for all scopes.

WeakSingletonAttribute

Classes that are attributed with the WeakSingletonAttribute will be shared while the export hasn't be released and garbage collected.

Custom Attributes

Because sometimes you want to create your own attributes Grace offers a set of interfaces that attributes can implement to be included for export. The interfaces exist in the Grace.DependencyInjection.Attributes.Interfaces namespace.

Last edited Nov 11, 2013 at 4:51 PM by ipjohnson123, version 6