<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Comment Feed for Channel 9 - Extreme ASP.NET Makeover: Mr. Escher, Your Software is Ready - Circular Dependency Cycle</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Blogs/howarddierking/Extreme-ASPNET-Makeover-Mr-Escher-Your-Software-is-Ready-Circular-Dependency-Cycle/RSS"></atom:link>
	<image>
		<url>http://ecn.channel9.msdn.com/o9/previewImages/100/491009_100x75.jpg</url>
		<title>Channel 9 - Extreme ASP.NET Makeover: Mr. Escher, Your Software is Ready - Circular Dependency Cycle</title>
		<link></link>
	</image>
	<description>
Extreme ASP.NET Makeover: Mr. Escher, Your Software is Ready –Circular Dependency Cycle
The only reason for Users to reference Host is to raise events on behalf of the Users class. We can quickly and easily remove this dependency by allowing Users to raise its own events, as shown in
Figure 4. 
Figure 4 Users Raises Its Own Events 
public class Users : IUsers {&amp;nbsp; public event     EventHandler&amp;lt;UserAccountActivityEventArgs&amp;gt; UserAccountChanged;&amp;nbsp; private void OnUserAccountChanged(UserInfo user,    UserAccountActivity activity) {&amp;nbsp; &amp;nbsp;   if(UserAccountChanged != null) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;   UserAccountChanged(this,           new UserAccountActivityEventArgs(user, activity));&amp;nbsp;&amp;nbsp;&amp;nbsp;   }  }&amp;nbsp; public event     EventHandler&amp;lt;UserGroupActivityEventArgs&amp;gt; UserGroupChanged;&amp;nbsp; private void OnUserGroupChanged(UserGroup group,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UserGroupActivity activity) {&amp;nbsp;&amp;nbsp;&amp;nbsp; if(UserGroupChanged != null) {&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; UserGroupChanged(this,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new UserGroupActivityEventArgs(group, activity));&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; }}

Host can now subscribe to the Users.UserAccountChanged and Users.UserGroupChanged events and re-broadcast those events to interested listeners without breaking the facade by exposing the Users class directly, as
Figure 5 shows. 
Figure 5 Host Can Subscribe to the Users.UserAccountChanged and Users.UserGroupChanged Events 
public class Host : IHostV30 {&amp;nbsp;&amp;nbsp;&amp;nbsp; public Host(ISettings settings, IUsers users) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customSpecialTags = new Dictionary&amp;lt;string, CustomToolbarItem&amp;gt;(5);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.settings = settings;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.users = users;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; users.UserAccountChanged &amp;#43;= OnUserAccountActivity;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; users.UserGroupChanged &amp;#43;= OnUserGroupActivity;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; public event EventHandler&amp;lt;UserAccountActivityEventArgs&amp;gt; UserAccountActivity;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void OnUserAccountActivity(object sender,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UserAccountActivityEventArgs args) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(UserAccountActivity != null) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; UserAccountActivity(this, args);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; public event EventHandler&amp;lt;UserGroupActivityEventArgs&amp;gt; UserGroupActivity;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void OnUserGroupActivity(object sender, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UserGroupActivityEventArgs args) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(UserGroupActivity != null) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; UserGroupActivity(this, args);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; }}

Users no longer needs a reference to Host and it can be safely removed from Users’ constructor. We have broken the circular dependency between Host and Users. Running our tests, we see that everything is green again. 
Before we move on, notice how the circular dependency problem wasn’t immediately noticeable when the classes were linked together using Singletons. Singletons, due to their global nature, make it easy for dependencies to spill between classes. Using the
 dependency injection principle, it is much easier to see a class’s dependencies because most of them are in the constructor and the remainder are method parameters. 
Don’t You Forget about IoC
With apologies to Billy Idol, we cannot forget about the container. It is responsible for creating and wiring together our dependencies. Looking at ScrewTurnWikiInitializationTask, we see the following: 
Users.Instance = new Users();

Classes that take a dependency on IUsers are going to get an instance of Users supplied through their constructor, but classes that access Users.Instance are going to get a different Users instance. We can solve this minor problem by allowing the IoC container
 to supply ScrewTurnWikiInitializationTask with an IUsers instance through its constructor. We now have: 
public ScrewTurnWikiInitializationTask(IHostV30 host, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ISettingsStorageProviderV30 settingsStorageProvider, IUsers users) {&amp;nbsp;&amp;nbsp;&amp;nbsp; this.host = host;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.settingsStorageProvider = settingsStorageProvider;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.users = users;}public void Execute() {&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&amp;nbsp;&amp;nbsp;&amp;nbsp; Users.Instance = users;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....}

While we’re talking about the container, let’s look at HostAnProviderRegistration: 
public class HostAndProviderRegistration : IContainerStartupTask {&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Execute(IWindsorContainer container) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Component.For&amp;lt;IHostV30&amp;gt;().ImplementedBy&amp;lt;Host&amp;gt;(),&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Component.For&amp;lt;ISettings&amp;gt;().ImplementedBy&amp;lt;Settings&amp;gt;(),&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Component.For&amp;lt;IUsers&amp;gt;().ImplementedBy&amp;lt;Users&amp;gt;());&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&amp;nbsp;&amp;nbsp;&amp;nbsp; }}

You’ll notice that we’re starting to get a lot of repetitive code. This is a great place to establish a convention rather than manually configuring each dependency. Let’s create a ScrewTurn.Wiki.Hosting namespace and register each dependency in that namespace
 against its first interface: 
public class HostAndProviderRegistration : IContainerStartupTask {&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Execute(IWindsorContainer container) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; AllTypes.FromAssembly(Assembly.GetExecutingAssembly())&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Where(x =&amp;gt; x.Namespace.StartsWith(&amp;quot;ScrewTurn.Wiki.Hosting&amp;quot;))&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .WithService.FirstInterface()&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; );&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&amp;nbsp;&amp;nbsp;&amp;nbsp; }}

As we refactor dependencies, we need only place them in this namespace (achieved by moving them to the Hosting folder in ScrewTurn.Wiki.Core) and they will automatically be registered correctly in the IoC container. 
The Journey Continues
Users depends on the Settings.Instance and Pages.Instance Singletons. These are quickly extracted into constructor dependencies as before. Pages gets the same treatment as Users—moving to ScrewTurn.Wiki.Hosting namespace (for automatic IoC registration through
 our convention), interface extraction, and pushing dependent Singletons to constructor parameters. (Additional tests were added to ensure that IPages can be resolved from the IoC container and that Pages.Instance is properly initialized.) 
Pages has the same problem as Users in that it uses Host to raise events on its behalf. The same procedure is applied whereby Pages is refactored to raise its own events, which Host then registers for and re-raises to interested listeners. Once again, we’ve
 broken an unnecessary coupling between two classes, Pages and Host this time. For those interested in the details, you can check out the code download associated with the article. 
Pages makes use of many other Singletons and the refactoring continues in much the same vein until we get to Users. Once we move Users into a constructor parameter, our tests break. Pages depends on Users and Users depends on Pages. We’ve uncovered another
 circular dependency issue. Let’s take a closer look. 
Other videos from this article

Overview
Dependency
Host Users Dependency Cycle
Circular Dependency Cycle
Refactoring Notification

Read the full article at 
http://msdn.microsoft.com/magazine/ee470637.aspx 
</description>
	<link></link>
	<language>en</language>
	<pubDate>Wed, 22 May 2013 19:27:51 GMT</pubDate>
	<lastBuildDate>Wed, 22 May 2013 19:27:51 GMT</lastBuildDate>
	<generator>Rev9</generator>
</channel>
</rss>