About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

Mugen Injection [Binding parameters]

The MugenInjector supports register binding with parameters. When you register service you can specify the IInjectionParameter, available parameter types:

  • ConstructorParameter - represents the value of a constructor argument.
  • 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:
public class Alpha
{
    [Inject]
    public string PublicField;

    [Inject]
    private string _field;

    [Inject]
    public string Property { get; set; }

    public Alpha(string st)
    {

    }

    [Inject]
    public void Method(string param)
    {

    }
}

// Create your MugenInjector.
_injector = new MugenInjector();

_injector.Bind<Alpha>()
            .ToSelf()
            .WithConstructorArguments(context => new Alpha("constructorParam"))
            .WithPropertyValue(alpha => alpha.Property, "propertyParam")
            .WithMethodArguments((context, alpha) => alpha.Method("methodParam"))
            .WithParameter(new FieldParameter("_field", "privateFieldParam"));

var alpha1 = _injector.Get<Alpha>();
When you add method or constructor parameter, you can specify that a parameter was taken from IInjector:
// Create your MugenInjector.
_injector = new MugenInjector();

_injector.Bind<Alpha>()
            .ToSelf()
            .WithConstructorArguments(context => new Alpha(context.Resolve<string>()))
            .WithMethodArguments((context, alpha) => alpha.Method(context.Resolve<string>()));
_injector.Get<Alpha>();

Comments
Leave a Reply
*bold*
_italics_
+underline+
* Bullet List
** Bullet List 2
# Number List
## Number List 2
{"Do not apply formatting"}
{code:language} code here {code:language}.
Supports: aspx c#, c#, c++, html, sql, xml
[url:http://www.example.com]