Using Parameters

When you register service you can specify the IInjectionParameter, available parameter types:
  • ConstructorParameter - represents the value of a constructor argument.
  • FieldParameter - represents the value of a field parameter.
  • PropertyParameter - represents the value of a property parameter.
  • MethodParameter - represents the value of a method parameter.
You can easily extend this list by implementing the interface IInjectionParameter.
This code shows how to add parameters to binding:
injector.Bind<A>().ToSelf()
                .WithConstructorArgument("arg", 1)
                .WithFieldValue("field", "field")
                .WithPropertyValue("property", new C())
                .WithMethodArgument("methodName", "arg", "st")
                .WithParameter(new CustomParameter());

Using Named bindings

Services can be further identified using a service name. Names are not necessarily unique; multiple bindings for a given service may be registered with the same name:
injector.Bind<Alfa>().ToSelf();
injector.Bind<Alfa>().To<Beta>().NamedBinding("Beta");
injector.Bind<Alfa>().To<Bravo>().NamedBinding("Bravo");

var alfa = injector.Get<Alfa>();//Instance of Alfa.
var beta = injector.Get<Alfa>("Beta");//Instance of Beta.
var bravo = injector.Get<Alfa>("Bravo");//Instance of Bravo.

Using a condition for bindings

When you register service you can specify the condition for bindings, available conditions:
  • WhenInto - Indicates that the binding should be used only for injections on the specified type. Types that derive from the specified type will not be considered as valid target.
  • WhenIntoIsAssignable - Indicates that the binding should be used only for injections on the specified type. Types that derive from the specified type are considered as valid targets.
  • WhenNamespaceEqual - Indicates that the binding should be used only for injections on the specified type when namespace equals.
  • WhenClassHasAttribute - Indicates that the binding should be used only when the class being injected has an attribute of the specified type.
  • When - Indicates that the binding should be used only for requests that support the specified condition.
injector.Bind<Alfa>().ToSelf()
                .When(context => context.TypeInto != null && context.TypeInto.FullName.StartsWith("S"));
injector.Bind<Alfa>().ToSelf()
                .WhenInto<Beta>();
injector.Bind<Alfa>().ToSelf()
                .WhenIntoIsAssignable<Alfa>();
injector.Bind<Alfa>().ToSelf()
                .WhenClassHasAttribute<SerializableAttribute>();
injector.Bind<Alfa>().ToSelf()
                .WhenNamespaceEqual("Test");

Set an IActivator

By default, the IActivator for binding is used from configuration of the IInjector, but you can change this. When you register service you can specify the IActivator that will use for create service, available IActivator:
  • ReflectionActivator - for creating objects using reflection (Available on all platforms).
  • EmitActivator - for creating objects using emit (Available on all platforms except Windows phone 7).
  • ExpressionActivator - for creating objects using expression trees (Available on all platforms except Windows phone 7 and NET 2).
You can easily extend this list by implementing the interface IActivator.
injector.Bind<Alfa>().ToSelf().UseEmitActivator();
injector.Bind<Alfa>().ToSelf().UseReflectionActivator();
injector.Bind<Alfa>().ToSelf().UseExpressionActivator();
injector.Bind<Alfa>().ToSelf().UseCustomActivator(new YourCustomActivator());

Set an IConstructorResolver

By default, the IConstructorResolver for binding is used from configuration of the IInjector, but you can change this. When you register service you can specify the IConstructorResolver
injector.Bind<Alfa>().ToSelf().UseCustomConstructorResolver(new CustomConstructorResolver());

Select a special constructor

When you register service you can specify the constructor that will use for create service:
injector.Bind<Alfa>().ToSelf().TakeConstructor<Bravo>();
injector.Bind<Alfa>().ToSelf().TakeConstructor(new Type[] { typeof(Bravo) });
If the constructor is not found you will get an error InvalidBindingException:
The constructor with 'MugenInjectionDocumentation.Bravo' type(s) not found in service MugenInjectionDocumentation.Alfa.

Using a binding priority

When you register service you can specify the binding priority:
injector.Bind<Alfa>().ToSelf().SetStandardPriority();
injector.Bind<Alfa>().To<Beta>().SetLowPriority();
injector.Bind<Alfa>().To<Bravo>().SetNormalPriority();

var bravo = injector.Get<Alfa>();//Instance of Bravo.

Other settings

When you register service you can specify behavior after dispose binding, you can try dispose the object after binding disposed.
injector.Bind<Alfa>().ToSelf().TryDisposeObjects();

Last edited Aug 12, 2012 at 12:16 PM by VVS0205, version 1

Comments

No comments yet.