Extreme ASP.NET Makeover: Mr. Escher, Your Software is Ready - Host Users Dependency Cycle

Sign in to queue


Extreme ASP.NET Makeover: Mr. Escher, Your Software is Ready –Host Users Dependency Cycle

Going Around in Circles

We successfully refactored away Host’s dependency on Settings.Instance by allowing the IoC container to pass Host an instance of ISettings via its constructor. It seems like a simple matter to continue refactoring away Host’s dependencies on the other Singletons, but trouble is brewing just around the corner. The next Singleton we extract is Users.Instance. Following exactly the same procedure as Settings.Instance, we discover a horrible secret - a CircularDependencyException.

Let’s take a look at the constructors for Host and Users:

public Host(ISettings settings, IUsers users) {
    customSpecialTags = 
        new Dictionary<string, CustomToolbarItem>(5);
    this.settings = settings;
    this.users = users;

public Users(IHostV30 host) {
    this.host = host;

So Host depends on Users and Users depends on Host. Before the refactoring, this dependency cycle wasn’t obvious. The two components are in fact much more tightly coupled to one another than expected. Let’s take a deeper look at Users to find out where this coupling is coming from.

Other videos from this article

Read the full article at https://msdn.microsoft.com/magazine/ee470637.aspx



Download this episode

The Discussion

Add Your 2 Cents