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

Mugen Injection [Decorators]

The decorator pattern is somewhat similar to the adapter pattern, where one service "wraps" another. However, in contrast to adapters, decorators expose the same service as what they're decorating. The point of using decorators is to add functionality to an object without changing the object's signature.
The MugenInjection provides built-in decorator registration so you can register services and have them automatically wrapped with decorator classes.

Decorator without condition

This code shows how to register the decorator for all IAlpha type, and then unregister it:
public interface IAlpha { }

public class Alpha : IAlpha { }

public class AlphaDecorator : IAlpha
{
    public AlphaDecorator(IAlpha alpha)
    {
        Alpha = alpha;
    }

    public IAlpha Alpha { get; private set; }
}


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

var value = new Alpha();
_injector.Bind<IAlpha>().ToConstant(value);
//Registering the decorator for IAlpha
_injector.BindAsDecoratorForAll<IAlpha>((context, alpha) => new AlphaDecorator(alpha));

var decorator = (AlphaDecorator)_injector.Get<IAlpha>();
//decorator.Alpha -  Alpha equals to value

//Unregistering the decorator for IAlpha
_injector.UnbindAllDecoratorsFor<IService>();

//alpha1 equals to value
var alpha1 = _injector.Get<IAlpha>();

Decorator with condition

This code shows how to register the decorator only for types with specific condition, and then unregister it:
// Create your MugenInjector.
_injector = new MugenInjector();
const string named = "named";
Type typeInto = typeof(Program);

var value1 = new Alpha();
var value2 = new Alpha();
_injector.Bind<IAlpha>().ToConstant(value1).NamedBinding(named);
_injector.Bind<IAlpha>().ToConstant(value2).WhenInto(typeInto);

//Registering the decorator for IAlpha
_injector.BindAsDecoratorFor<IAlpha>(builder => builder.Named(named),
                                    (context, alpha) => new AlphaDecorator(alpha));
_injector.BindAsDecoratorFor<IAlpha>(builder => builder.WhenInto(typeInto),
                        (context, alpha) => new AlphaDecorator(alpha));

//We get exception, because we don't have binding without condition
//var alpha1 = _injector.Get<IAlpha>();

var decorator = (AlphaDecorator) _injector.Get<IAlpha>(named);
//decorator.Alpha -  Alpha equals to value1

decorator = (AlphaDecorator)_injector.GetInto<IAlpha>(typeInto);
//decorator.Alpha -  Alpha equals to value2

//Unregistering the decorator for IAlpha
var dict = new Dictionary<string, object>();
SpecialParameterKeys.SetKeyedBindingParameter(dict, named);
_injector.UnbindDecoratorsFor<IAlpha>(null, null, dict, null);
_injector.UnbindDecoratorsFor<IAlpha>(typeInto, null, null, null);

//alpha1 equals to value1
var alpha1 = _injector.Get<IAlpha>(named);
//alpha1 equals to value2
alpha1 = _injector.GetInto<IAlpha>(typeInto);

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]