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

Mugen Injection [Circular dependencies]

This is when you have one class (Alpha) that takes a dependency of a second type (Beta) and the second type (Beta) has a dependency of the first type (Alpha). In this case you can write behavior to resolve circular dependencies.
This code shows how it works:

public class Alpha
{
    public Bravo Bravo { get; set; }

    public Alpha(Bravo bravo)
    {
        Bravo = bravo;
    }
}

public class Bravo
{
    public Alpha Alpha { get; set; }

    public Bravo(Alpha alpha)
    {
        Alpha = alpha;
    }
}

public class CycleDependencyResolver : ICycleDependencyBehavior
{
    #region Implementation of ICycleDependencyBehavior

    public bool Resolve(IBindingContext bindingContext, out object result)
    {
        if (bindingContext.Service == typeof(Bravo) && bindingContext.TypeInto == typeof(Alpha))
        {
            result = new Bravo(null);
            return true;
        }
        result = null;
        return false;
    }

    #endregion
}

// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Components
    .Get<IBehaviorManagerComponent>()
    .Add<ICycleDependencyBehavior>(new CycleDependencyResolver());
var bravo = _injector.Get<Bravo>();

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]