concept Dependency cycle in category dependency injection

appears as: Dependency cycle, Dependency cycles, A Dependency cycle
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

Mary gained a couple of things from this division. First of all, the new classes are smaller and easier to comprehend. Next, it lowers the chance of getting into the situation where Mary will be constantly updating existing code. And last, but not least, splitting the SqlUserRepository class breaks the Dependency cycle, because the new SqlUserByNameRetriever doesn’t depend on IAuditTrailAppender. Figure 6.14 shows how the Dependency cycle was broken.

Figure 6.14 The separation of IUserRepository into two interfaces breaks the Dependency cycle.

06-14.eps

6.3.4 Common strategies for breaking Dependency cycles

When we encounter a Dependency cycle, our first question is, “Where did I fail?” A Dependency cycle should immediately trigger a thorough evaluation of the root cause. Any cycle is a design smell, so your first reaction should be to redesign the involved part to prevent the cycle from happening in the first place. Table 6.4 shows some general directions you can take.

Table 6.4 Some redesign strategies for breaking Dependency cycles, ordered from most to least preferable strategy (view table figure)
Strategy Description
Split classes As you saw with the audit trail example, in most cases, you can split classes with too many methods into smaller classes to break the cycle.
.NET events You can often break a cycle by changing one of the Abstractions to raise events instead of having to explicitly invoke a Dependency to inform the Dependency that something happened. Events are particularly appropriate if one side only invokes void methods on its Dependency.
Property Injection If all else fails, you can break the cycle by refactoring one class from Constructor Injection to Property Injection. This should be a last-ditch effort, because it only treats the symptoms.

Make no mistake: a Dependency cycle is a design smell. Your first priority should be to analyze the code to understand why the cycle appears. Still, sometimes you can’t change the design, even if you understand the root cause of the cycle.

Listing 6.30 Breaking a Dependency cycle with a Virtual Proxy
var lazyAppender = new LazyAuditTrailAppender();    #1  

var repository =
    new SqlUserRepository(context, lazyAppender);

var userContext = new 
AspNetUserContextAdapter(
    new SqlUserByNameRetriever(context));

lazyAppender.Appender =    #2  
    new SqlAuditTrailAppender(
        userContext, context, timeProvider);

#1   Creates a Virtual Proxy
#2   Injects the real appender into the property of the Virtual Proxy


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":48}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":16}],[{\"line\":3,\"ch\":35},{\"line\":3,\"ch\":47}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":12}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":23}]]"}
!@%STYLE%@!