concept Dependency cycle in category 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
SqlUserRepositoryclass breaks the Dependency cycle, because the newSqlUserByNameRetrieverdoesn’t depend onIAuditTrailAppender. Figure 6.14 shows how the Dependency cycle was broken.Figure 6.14 The separation of
IUserRepositoryinto two interfaces breaks the Dependency cycle.![]()
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)
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%@!