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

Mugen Injection [Settings(metadata)]

Metadata is information about a component, stored with that component, accessible without necessarily creating a component instance.
When you register service you can specify any custom setting(metadata) which you can use in IBinding, this code shows, how it work:

// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<Beta>().ToSelf().WithSetting("test", "test");
_injector.Get<Beta>();

WithSetting of T

Sometimes we need to get a service with some settings, the MugenInjection has a system of relationship types that can be used to provide the features of the container in a declarative way. Other components can consume metadata using the WithSetting<T> type or your custom type(see below).
To get setting from binding you can write this code:
// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<Beta>().ToSelf().WithSetting("test", "test");
var withSettings = _injector.Get<WithSettings<Beta>>();
//do something
var o = withSettings.Settings["test"];
var value = withSettings.Value;

public class Bravo
{
    public Bravo(IEnumerable<WithSettings<Lazy<Beta>>> values)
    {
        var withSettings = values.First(lazy => lazy.Settings.ContainsKey("test"));
    }
}

Custom implementation of WithSetting

Sometimes we need to get a service with some settings, but we don't want to include MugenInjection library. In this case you can write a custom implementation of WithSetting. This code shows how to do it:
public class WithCustomSettings<T>
{
    #region Constructors

    public WithCustomSettings(T value, IDictionary<string, object> settings, 
                                                     IDictionary<string, object> specialParameters)
    {
        Settings = settings;
        SpecialParameters = specialParameters;
        Value = value;
    }

    #endregion

    #region Properties

    public T Value { get; private set; }

    public IDictionary<string, object> Settings { get; private set; }

    public IDictionary<string, object> SpecialParameters { get; private set; }

    #endregion
}


// Create your MugenInjector.
_injector = new MugenInjector();
//Register the WithCustomSettings<> type
_injector.Components
            .BindingActivatorComponent
            .GetConverter<SettingsWrapperConverter>()
            .SettingsTypes
            .Add(typeof (WithCustomSettings<>));

_injector.Bind<IAlpha>().ToConstant(new Alpha()).WithSetting("test", "test");

//Getting service with custom setting wrapper.
var alpha = _injector.Get<WithCustomSettings<IAlpha>>();

Try to dispose object setting

Also when you register service you can specify behavior after dispose binding, you can set setting to try dispose an object after binding disposed, this code shows, how it work:
// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<Beta>().ToSelf().TryDisposeObjects();

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]