-
Notifications
You must be signed in to change notification settings - Fork 0
Factory
This feature converts interfaces into factory implementations. The proxy is weaved during compilation, the interpretation of the factory interface takes place during runtime.
Bind any interface (marked with [StaticProxy]
-attribute) as follows:
using Ninject.Extensions.StaticProxy.Factory;
Bind<IFactory>().ToFactory();
[StaticProxy]
public interface IFactory
{
IFoo Create(string parameter);
}
returns the result of the following resolution:
kernel.Get<IFoo>(new TypeMatchingConstructorArgument(
typeof(string), (ctx, target) => parameter, false));
[StaticProxy]
public interface IFactory
{
IFoo Create([TypeMatchingConstructorArgument(inherited:true)] string parameter);
}
returns the result of the following resolution:
kernel.Get<IFoo>(new TypeMatchingConstructorArgument(
typeof(string), (ctx, target) => parameter, true));
[StaticProxy]
public interface IFactory
{
IFoo Create([NamedConstructorArgument(inherited:true)] string parameter);
}
factory.Create("Hallo");
returns the result of the following resolution:
kernel.Get<IFoo>(new ConstructorARgument("parameter", "Hallo", true));
[StaticProxy]
public interface ITransportFactory
{
[ResolveTo(typeof(TcpTransport))]
ITransport CreateTcpTransport();
}
returns the result of the following resolution:
kernel.Get<TcpTransport>();
Hint: If a factory-method has multiple constraints (imagine custom constraints), the binding matching all the constraints is chosen.
[StaticProxy]
public interface IFactory
{
[NamedConstraint("Bar")]
IFoo Create();
}
returns the result of the following resolution:
kernel.Get<IFoo>("Bar");
[StaticProxy]
public interface IFactory
{
IFoo Create([NamedConstraintByParameter]string name);
}
factory.Create("Foo");
returns the result of the following resolution:
kernel.Get<IFoo>("Foo");
You can create and apply custom attributes to customize the resolution. Inherit from any of the following:
-
ReturnTypeAttribute
- apply on method -
ConstraintAttribute
- apply on method -
ParameterisedConstraintAttribute
- apply on parameter -
ParameterAttribute
- apply on parameter