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;


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

Leave a Reply
* 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