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

Mugen Injection [Method, property injection]

Property and method injection uses writeable properties and methods rather than constructor parameters to perform injection.

Using attribute to inject

To enable injection you need to mark your property or method attribute that you set to IInjectorSetting.AttributeForInject property, default is the MugenInjection.Attributes.InjectAttribute.
public sealed class MyInjectAttribute : Attribute { }

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

    [Inject]
    public void Method(string method)
    {

    }

    [MyInject]
    public string PropertyCustomAttr { get; set; }

    [MyInject]
    public void MethodCustomAttr(string method)
    {

    }
}

// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<string>().ToConstant("test");

var injectAttributeExample = _injector.Get<InjectAttributeExample>();

_injector.Settings.AttributeForInject = typeof (MyInjectAttribute);
injectAttributeExample = _injector.Get<InjectAttributeExample>();

Using activator to inject

If the attribute does not suit you, you can use its own implementation of IActivator interface. This example of IActivator from NServiceBus.MugenInjection integration library.
public class ObjectBuilderActivator : ExpressionActivator
{
    #region Fields

    private Type _serviceType;
    private List<PropertyInfo> _cachedProperty;

    #endregion

    #region Method

    private IEnumerable<PropertyInfo> GetProperties(Type service)
    {
        if (_serviceType != service)
        {
            _serviceType = service;
            _cachedProperty = service.GetProperties(BindingFlags).ToList();
            return _cachedProperty;
        }
        return _cachedProperty;
    }

    #endregion

    #region Overrides of ActivatorBase

    protected override bool IsHasValuesToInject(Type service, Type attributeType)
    {
        return true;
    }

    protected override IList<PropertyInfo> GetPropertiesToInject(Type service, Type attributeType)
    {
        IList<PropertyInfo> propertiesToInject = base.GetPropertiesToInject(service, attributeType);
        foreach (PropertyInfo propertyInfo in GetProperties(service))
        {
            if (propertiesToInject.Contains(propertyInfo)) continue;
            if (CurrentContext.Parameters.Any(parameter => parameter.CanResolve(propertyInfo, null)))
            {
                propertiesToInject.Add(propertyInfo);
                continue;
            }
            if (!CurrentContext.CallInjector.CanResolve(propertyInfo.PropertyType, true, false)) continue;
            propertiesToInject.Add(propertyInfo);
        }
        return propertiesToInject;
    }

    #endregion
}
And then you can use it in your bindings:
// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<Bravo>().ToSelf().UseCustomActivator(new ObjectBuilderActivator());
More about the activators look here.

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]