Archive for the ‘Design’ Category

Testing framework is not just for writing… tests

Wednesday, January 18th, 2012

Quick ques­tion – from the top of your head, with­out run­ning the code, what is the result of:

var foo = -00.053200000m;
var result = foo.ToString("##.##");

Or a dif­fer­ent one:

var foo = "foo";
var bar = "bar";
var foobar = "foo" + "bar";
var concaternated = new StringBuilder(foo).Append(bar).ToString(); 

var result1 = AreEqual(foobar, concaternated);
var result2 = Equals(foobar, concaternated);

public static bool AreEqual(object one, object two)
{
    return one == two;
}

How about this one from NHibernate?

var parent = session.Get<Parent>(1); 

DoSomething(parent.Child.Id); 

var result = NHibernateUtil.IsInitialized(parent.Child);

The point being?

Well, if you can answer all of the above with­out run­ning the code, we’re hir­ing. I don’t, and I sus­pect most peo­ple don’t either. That’s fine. Ques­tion is – what are you going to do about it? What do you do when some 3rd party library, or part of stan­dard library exhibits unex­pected behav­iour? How do you go about learn­ing if what you think should hap­pen, is really what does happen?

Scratch­pad

I’ve seen peo­ple open up Visual Stu­dio, cre­ate ConsoleApplication38, write some code using the API in ques­tion includ­ing plenty of Console.WriteLine along the way (curse who­ever decided Client Pro­file should be the default for Con­sole appli­ca­tions, switch to full .NET pro­file) com­pile, run and dis­card the code. And then repeat the process with ConsoleApplication39 next time.

 

The solu­tion I’m using feels a bit more light­weight, and has worked for me well over the years. It is very sim­ple – I lever­age my exist­ing test frame­work and test run­ner. I cre­ate an empty test fix­ture called Scratchpad.

scratchpad

scratchpad_fixture

This class gets com­mit­ted to the VCS repos­i­tory. That way every mem­ber of the team gets their own scratch­pad to play with and val­i­date their the­o­ries, ideas and assump­tions. How­ever, as the name implies, this all is a one-off throw­away code. After all, you don’t really need to test the BCL. One would hope Microsoft already did a good job at that.

If you’re using git, you can eas­ily tell it not to track changes to the file, by run­ning the fol­low­ing com­mand (after you com­mit the file):

git update-index –assume-unchanged Scratchpad.cs

scratchpad_git

With this sim­ple set up you will have quick place to val­i­date your assump­tions (and answer ques­tions about API behav­iour) with lit­tle friction.

scratchpad_test

So there you have it, a new, use­ful tech­nique in your toolbelt.

Testing conventions

Wednesday, March 9th, 2011

I already blogged about the topic of val­i­dat­ing con­ven­tions in the past (here and here). Doing this has been a fan­tas­tic way of keep­ing con­sis­tency across code­bases I’ve worked on, and sev­eral of my col­leagues at Read­ify adopted this approach with great success.

Recently I found myself using this approach even more often and in sce­nar­ios I didn’t think about ini­tially. Take this two small tests I wrote today for Windsor.

[TestFixture]
public class ConventionVerification
{
	[Test]
	public void Obsolete_members_of_kernel_are_in_sync()
	{
		var message = new StringBuilder();
		var kernelMap = typeof(DefaultKernel).GetInterfaceMap(typeof(IKernel));
		for (var i = 0; i < kernelMap.TargetMethods.Length; i++)
		{
			var interfaceMethod = kernelMap.InterfaceMethods[i];
			var classMethod = kernelMap.TargetMethods[i];
			Scan(interfaceMethod, classMethod, message);
		}

		Assert.IsEmpty(message.ToString(), message.ToString());
	}

	[Test]
	public void Obsolete_members_of_windsor_are_in_sync()
	{
		var message = new StringBuilder();
		var kernelMap = typeof(WindsorContainer).GetInterfaceMap(typeof(IWindsorContainer));
		for (var i = 0; i < kernelMap.TargetMethods.Length; i++)
		{
			var interfaceMethod = kernelMap.InterfaceMethods[i];
			var classMethod = kernelMap.TargetMethods[i];
			Scan(interfaceMethod, classMethod, message);
		}

		Assert.IsEmpty(message.ToString(), message.ToString());
	}

	private void Scan(MethodInfo interfaceMethod, MethodInfo classMethod, StringBuilder message)
	{
		var obsolete = EnsureBothHave<ObsoleteAttribute>(interfaceMethod, classMethod, message);
		if (obsolete.Item3)
		{
			if (obsolete.Item1.IsError != obsolete.Item2.IsError)
			{
				message.AppendLine(string.Format("Different error levels for {0}", interfaceMethod));
			}
			if (obsolete.Item1.Message != obsolete.Item2.Message)
			{
				message.AppendLine(string.Format("Different message for {0}", interfaceMethod));
				message.AppendLine(string.Format("\t interface: {0}", obsolete.Item1.Message));
				message.AppendLine(string.Format("\t class    : {0}", obsolete.Item2.Message));
			}
		}
		else
		{
			return;
		}
		var browsable = EnsureBothHave<EditorBrowsableAttribute>(interfaceMethod, classMethod, message);
		{
			if (browsable.Item3 == false)
			{
				message.AppendLine(string.Format("EditorBrowsable not applied to {0}", interfaceMethod));
				return;
			}
			if (browsable.Item1.State != browsable.Item2.State || browsable.Item2.State != EditorBrowsableState.Never)
			{
				message.AppendLine(string.Format("Different/wrong browsable states for {0}", interfaceMethod));
			}
		}
	}

	private static Tuple<TAttribute, TAttribute, bool> EnsureBothHave<TAttribute>(MethodInfo interfaceMethod, MethodInfo classMethod, StringBuilder message)
		where TAttribute : Attribute
	{
		var fromInterface = interfaceMethod.GetAttributes<TAttribute>().SingleOrDefault();
		var fromClass = classMethod.GetAttributes<TAttribute>().SingleOrDefault();
		var bothHaveTheAttribute = true;
		if (fromInterface != null)
		{
			if (fromClass == null)
			{
				message.AppendLine(string.Format("Method {0} has {1} on the interface, but not on the class.", interfaceMethod, typeof(TAttribute)));
				bothHaveTheAttribute = false;
			}
		}
		else
		{
			if (fromClass != null)
			{
				message.AppendLine(string.Format("Method {0} has {1}  on the class, but not on the interface.", interfaceMethod, typeof(TAttribute)));
			}
			bothHaveTheAttribute = false;
		}
		return Tuple.Create(fromInterface, fromClass, bothHaveTheAttribute);
	}
}

All they do is ensure that when­ever I obso­lete a method on the con­tainer, I do that con­sis­tently between the inter­face and the class that imple­ments it (set­ting the same warn­ing mes­sage, and the same warning/error flag state). It also val­i­dates that I hide the obso­lete method from Intel­lisense for peo­ple who have the option enabled in their Visual Studio.

Those are kinds of things, that are impor­tant, but they nei­ther cause a com­piler error, or com­piler warn­ing, nor do they fail at run­time. Those are kinds of things you can val­i­date in a test. Those are small things that make a big dif­fer­ence, and hav­ing a com­pre­hen­sive bat­tery of tests for con­ven­tions in your appli­ca­tion, can greatly improve con­fi­dence and morale of the team.

Unit tests are overrated

Monday, February 28th, 2011

Some­thing is rot­ten in the state of Devel­op­ment. It seems to me we (devel­op­ers) either ignore test­ing alto­gether, leav­ing it to the Q&A team when we throw them the app over the wall, or we con­cen­trate on unit tests only. Let me make one thing clear before we go any fur­ther – unit tests are a fan­tas­tic and extremely valu­able tool in many cases, and I by no means am try­ing to dis­cour­age any­one from writ­ing unit tests. They have their place in the devel­op­ment pipeline (espe­cially if you’re doing TDD).

While I still usu­ally write a fair amount of unit tests I find the per­cent­age of unit tests in the code I write is get­ting smaller. I just find unit tests are not the best value for the buck in many cases where pre­vi­ously I would write unit tests uncon­di­tion­ally, not really giv­ing it any thought.

Where would that be?

Let’s stop and think for a sec­ond – what unit tests are good at. Unit tests exer­cise sin­gle units of func­tion­al­ity, and they’re the most fine grained tests you write. They tests a method pro­duces the right out­put. They test a class responds in expected man­ner to results of invo­ca­tion of another method, poten­tially on another object. And they really shine if those algo­rithms and small scale inter­ac­tions are com­plex and/or part of API that is going to be used exten­sively where they do a really great job at being an exe­cutable documentation.

How­ever, for quite a few sce­nar­ios unit tests just aren’t the best approach and that’s my goal with this blog post – to make you stop and think – should I write a unit test, or per­haps an inte­gra­tion test, with real, not stubbed out dependencies?

To illus­trate the point let me tell you a (real) story. I was called to a client recently to have a look at the issue they were hav­ing in their sys­tem. The sys­tem was com­posed of an admin­is­tra­tion web­site, and a client appli­ca­tion that was com­mu­ni­cat­ing with the server side via a WCF ser­vice. The issue they were hav­ing is that some infor­ma­tion that was entered on the admin­is­tra­tive web­site, wasn’t dis­played prop­erly in the client appli­ca­tion. How­ever all the unit tests were pass­ing, for all three ele­ments of the sys­tem. As I dug into the code­base I noticed that some trans­for­ma­tion was being done to the infor­ma­tion com­ing from the web UI on the web-app end before it was being saved, and them what was sup­posed to be the same process in reverse was hap­pen­ing on the WCF Ser­vice side. Except it wasn’t. The trans­for­ma­tion being done changed, and was updated every­where except for the web­site (this also rein­forces the point I made in my pre­vi­ous blog­post, that changes in appli­ca­tion code should almost always be accom­pa­nied by changes to tests). So the unit tests between two parts of the sys­tem were out of sync, as were their imple­men­ta­tions yet unit tests weren’t able to detect that. Only a grander scale, inte­gra­tion test, that would cover both ends of the spec­trum, the sav­ing of the infor­ma­tion on the web­site site, and read­ing it from the same repos­i­tory on the ser­vice side would have caught this kind of change.

At the end, let me share a secret with you – Wind­sor, has very few unit tests. Most tests there are (and we have close to 1000 of them) exer­cise entire con­tainer with no part stubbed out in sim­u­la­tions of real life sce­nar­ios. And that works excep­tion­ally well for us, and lets us cover broad range of func­tion­al­ity from the per­spec­tive of some­one who is actu­ally using the con­tainer in their real application.

So that’s it – do write tests, and do remem­ber to have more than just a sin­gle ham­mer in your tool belt.

Tests are your airbag

Sunday, February 27th, 2011

Good bat­tery of tests is like good wine. You will not under­stand its value until you’ve expe­ri­enced it. There’s few things as lib­er­at­ing in soft­ware devel­op­ment as know­ing you can refac­tor fear­lessly hav­ing con­fi­dence your tests will catch any break­ing changes.

I don’t have the time not to write tests

I can’t imag­ine work­ing on Wind­sor if it wasn’t thor­oughly cov­ered by tests. On the other hand, most of the prob­lems I’ve faced on var­i­ous other projects, could have been avoided had they been bet­ter tested. Prob­lem is – they were. Most of the projects I’ve been work­ing on had tests and if you’d inspect code cov­er­age of those tests it would usu­ally yield a rea­son­ably high num­ber. Except code cov­er­age doesn’t tell the full story. What’s impor­tant is not just that the code is tested. And that’s because not all code is equal.

public class Event : EntityBase
{
	public Event(string title, string @where, DateTime when, string description, UserInfo generatedBy)
	{
		Title = title;
		Where = @where;
		When = when;
		Description = description;
		GeneratedBy = generatedBy;
	}

	public virtual string Title { get; set; }

	public virtual string Where { get; set; }

	public virtual DateTime When { get; set; }

	public virtual string Description { get; set; }

	public virtual UserInfo GeneratedBy { get; set; }
}

The code as above will have dif­fer­ent test­ing require­ments from your price cal­cu­lat­ing logic if you’re writ­ing an e-commerce appli­ca­tion. The higher cyclo­matic com­plex­ity and the more crit­i­cal the code is the more atten­tion it requires when test­ing. That’s pretty easy to com­pre­hend and agree with.

There’s another angle to it, which is often over­looked. Indeed, like in the tale of boiled frog, that’s the rea­son why the qual­ity of our tests dete­ri­o­rates – the why of a test. Why and when to write a test is equally impor­tant as what to test. And the answer here is quite sim­ple. So sim­ple in fact, that often over­looked or neglected. When­ever some­thing in the code changes the change ought to be doc­u­mented in test. When­ever your code changes it is most vul­ner­a­ble. And isn’t it he premise of Agile? To embrace the change?

It is not enough to test your new code. If a change to some code is made and no test was changed or added that should raise a red flag for you – I cer­tainly am going to make that a rule for myself – any change to a non-trivial code must be accom­pa­nied by a change in exist­ing tests, and (almost always) by addi­tion of new tests.

Must Windsor track my components?

Thursday, August 19th, 2010

Prob­a­bly the sin­gle most mis­un­der­stood fea­ture of Cas­tle Wind­sor is regard­ing its life­time man­age­ment of com­po­nents. Hope­fully in this post (and the next one) I’ll be able to clear all the misconceptions.

Why is Wind­sor track­ing com­po­nents in the first place?

lifecycle_simplified

One of the core respon­si­bil­i­ties of a con­tainer is to man­age life­cy­cle of com­po­nents.  At a very high level it looks like in the pic­ture on the right. The con­tainer cre­ates the object, sets it up, then when it’s ready to go, it gives it to you, so that you can use it, and when it’s no longer needed it care­fully tears it apart and sends it to happy hunt­ing ground. Many peo­ple for­get about that last part, and indeed some con­tain­ers lack sup­port for this cru­cial ele­ment at all.

In order to be able to iden­tify objects it cre­ated and destroy them when their time has come, Wind­sor obvi­ously needs to have access to them and that’s why it keeps ref­er­ence to the objects it needs to destroy.

Destroy objects. What does that even mean?

I’ve been pretty abstract until now, let’s look at an actual (triv­i­al­ized) example .

public class UnitOfWork
{
   public void Init()
   {
      // some initialization logic...
   }
   public void Commit()
   {
      // commit or rollback the UoW
   }
}

We want to cre­ate the instance of our UnitOf­Work, then before we use it, we want to ini­tial­ize it, then use it for a while to do the work, and then when we’re done, com­mit all the work that we’ve done. Usu­ally in a web-app the life­time of the unit or work would be bound to a sin­gle web request, in a client app it could be bound to a screen.

It is container’s work to cre­ate the object. It’s container’s work to ini­tial­ize it. So that I don’t need to remem­ber to call the Init method myself. Espe­cially that within a web request (let’s stick to the web exam­ple) I can (and likely will) have mul­ti­ple com­po­nents depend­ing on my unit of work. Which one of them should call Init?

None. It’s not their job. Their job is to make use of it. Be lazy and out­source this to the con­tainer.  Which of my com­po­nents should Com­mit the unit of work? Also none.

None of my com­po­nents is respon­si­ble for the unit of work, since none of them cre­ated it. The con­tainer did, so it’s container’s job to clean up after the object when I’m done using it.

The destroy part is just it – do all the things with the com­po­nents that need to be done after the com­po­nent is no longer being used by the appli­ca­tion code, but before the object can be let go. The most com­mon exam­ple of that is the IDis­pos­able inter­face and Dis­pose method.

The rule in .NET frame­work is very sim­ple when it comes to Dis­pos­ing objects.

Dis­pose what you’ve cre­ated, when you’re done using it.

Hence clearly since the con­tainer is cre­at­ing the object it’s its respon­si­bil­ity to dis­pose of them. In Wind­sor lingo, you’d call the Init method on the UnitOf­Work a com­mis­sion con­cern and the Com­mit method a decom­mis­sion con­cern.

What if my object requires no clean up? Will Wind­sor still track it?

This is a very impor­tant ques­tion. The answer is also impor­tant. In short.

It depends.

Wind­sor by default will track objects that them­selves have any decom­mis­sion con­cerns, are pooled, or any of their depen­den­cies is tracked.

We will dis­cuss this in more details in a future post, since it is a big and impor­tant topic. In short though – you as a user of a com­po­nent should not know all these things (espe­cially that they can change), so you should treat all com­po­nents as being tracked..

What do you mean by default?

Wind­sor is very mod­u­lar and unsur­pris­ingly track­ing of com­po­nents is con­tained in a sin­gle object, called release pol­icy. The behav­ior described above is the behav­ior of default, Life­cy­cled­Com­po­nentsRe­lease­Pol­icy. Wind­sor comes also with alter­na­tive imple­men­ta­tion called NoTrack­ingRe­lease­Pol­icy which as its name implies will never ever track your com­po­nents, hence you never want to use it.

Now seri­ously – often peo­ple see that Wind­sor holds on to the com­po­nents it cre­ates as a mem­ory leak (it often is, when used inap­pro­pri­ately which I’ll talk about in the next post), and they go – Wind­sor hold­ing on to the objects causes mem­ory leaks, so lets use the NoTrack­ingRe­lease­Pol­icy and the prob­lem is solved.

This rea­son­ing is flawed, because the actual rea­son is you not using this fea­ture cor­rectly, not the fea­ture itself. By switch­ing to no track­ing, you end up out of the fry­ing pan and into the fire, since by not destroy­ing your objects prop­erly you’ll be fac­ing much more sub­tle, harder to find and poten­tially more severe in results bugs (like units of works not being com­mit­ted, hence los­ing work of your users). So in clos­ing – it’s there when you need it, but even when you thing you need it, you really don’t.

IoC patterns – partitioning registration

Tuesday, August 10th, 2010

I’ve blogged a bit in the past, more or less explic­itly, about pat­terns and antipat­terns of Inver­sion of Con­trol usage. This is yet another post that will (pos­si­bly) spawn a series. We’ll see about that. Note that this post is not talk­ing about any par­tic­u­lar IoC con­tainer and what I’m talk­ing about is generic and uni­ver­sally applic­a­ble to all of them.

His­tor­i­cally we started to reg­is­ter and con­fig­ure our com­po­nents in XML (exam­ple is in pseudo syn­tax, not spe­cific to any par­tic­u­lar framework).

<components>
   <register name="foo" type="Acme.Crm.Foo, Acme.Crm" as="Acme.Crm.IFoo, Acme.Crm" />
   <register name="bar" type="Acme.Crm.Bar, Acme.Crm" as="Acme.Crm.IBar, Acme.Crm">
	  <parameter name="bla" value="42" />
   </register>
   <!--... many, many more components like this...-->
</components>

Later we learned that it’s mas­sive pain in the neck to write and man­age, and we moved to code:

container.Register<Foo>().As<IFoo>().WithName("foo");
container.Register<Bar>().As<IBar>().WithName("bar")
   .WithParameter("bla", 42);
/* ...many, many more components like this... */

This had the advan­tage over XML of being strongly typed, refac­tor­ing friendly, but still shared the biggest draw­back of the pre­vi­ous approach. For each and every new com­po­nent you added to your appli­ca­tion you would have to go back and explic­itly reg­is­ter it.

To alle­vi­ate that, many con­tain­ers sup­port con­ven­tions – where nam­ing the type in the “right” way or putting it in the “right” place would be enough to make that type avail­able as a component.

container.RegisterAllTypesInNamespace("Acme.Crm")
   .AsInterfaceTheyImplement()
   .NamedAsLowercaseTypeName()
   .WithParameterFor<Bar>("bla", 42);

You would still need to do some fine­tun­ing with some of the com­po­nents, but for bulk of it, it would just work.

So where’s the problem?

While the last approach is evo­lu­tion­ally the most advanced it too comes with its own set of draw­backs, that can bite you if you don’t pay atten­tion to them.

If you par­ti­tion your appli­ca­tion abstract­ing imple­men­ta­tion detains with inter­faces and reg­is­ter­ing classes via con­ven­tions you will end up with highly decou­pled appli­ca­tion. So decou­pled in fact, that you will have no explicit ref­er­ence to your classes any­where out­side your tests. This can com­pli­cate things when a new per­son comes to the project and tries to fig­ure out how stuff works. Sim­ply fol­low­ing ReSharper’s “Go to definition”/”Go to usage” won’t cut it. Often you can infer from the inter­face or usage which class is used under which inter­face, but if you have generic abstrac­tion (like ICom­mand­Han­dler) and mul­ti­ple imple­men­ta­tions that get wired in a non-trivial way it may quickly become much more com­pli­cated to find out how the stuff gets wired and why it works the way it does.

Par­ti­tion your registration

Installers

To min­i­mize prob­lems like this its cru­cial that you par­ti­tion your reg­is­tra­tion cor­rectly. What do I mean by that?

First of all make it gran­u­lar. Most con­tain­ers have some means of divid­ing your reg­is­tra­tion code into pieces. Wind­sor has installers, Aut­o­fac and Nin­ject have mod­ules, Struc­tureMap has reg­istries… Take advan­tage of them. Don’t just cre­ate one and put all your reg­is­tra­tion in one. You will regret it when later on you try to change some­thing and you find your­self scrolling through this mon­strous class. Make it gran­u­lar. Par­ti­tion is apply­ing Sin­gle Respon­si­bil­ity Prin­ci­ple. Even if your installer (I will stick to Wind­sor lingo – replace with module/registry if you’re using any other con­tainer) ends up reg­is­ter­ing just a sin­gle type – that’s fine.

Remem­ber to name them cor­rectly so that it’s very quick and obvi­ous hav­ing a type, to find which installer is respon­si­ble for its registration/configuration.

To do that – also pay atten­tion to the name you give your installers. If the next devel­oper has to stop and think, or search sev­eral to find the right one for a par­tic­u­lar ser­vice you’ve failed.

Last but not least – keep them all in one vis­i­ble place. I gen­er­ally cre­ate folder/namespace ded­i­cated to hold­ing my installers, so that I can find them quickly and scan the entire list at once. It’s really very frus­trat­ing hav­ing to chase down installers all across the solution.

New Castle Windsor feature – debugger diagnostics

Friday, July 2nd, 2010

What you’re see­ing here, is a fea­ture in very early stages of devel­op­ment. It’s very likely to change in the very near future, hope­fully based on your feed­back which I’m look­ing for­ward to.

It is often the case with IoC con­tain­ers, espe­cially when reg­is­ter­ing com­po­nents by con­ven­tion, that you end up with mis­con­fig­ured com­po­nents, or with an excep­tion say­ing that your com­po­nent can’t be resolved. To aid work­ing in these sit­u­a­tions Struc­tureMap pro­vides meth­ods like What­DoI­Have and Assert­Con­fig­u­ra­tionIs­Valid. That’s the only con­tainer I know of that pro­vides this kind of diagnostics.

Wind­sor also has sim­i­lar abil­ity to StructureMap’s What­DoI­Have method. It’s very pow­er­ful as well, since Wind­sor tracks inter­nally the entire graph of objects and lets you access it you can for exam­ple visu­al­ize it.

The Assert­Con­fig­u­ra­tionIs­Valid is a tougher nut to crack. Thing is – you can’t really say when the con­fig­u­ra­tion is valid in any non triv­ial sit­u­a­tion. You can’t say when it’s non-valid either. The rea­son for that is that there are mul­ti­ple dynamic mov­ing parts that you can’t really sta­t­i­cally analyse to out­put a yes/no value.

What Wind­sor does

To help with these sit­u­a­tions when debug­ging, in Wind­sor 2.5 I added debug­ging prox­ies to the most impor­tant classes in Wind­sor, so that when look­ing at them in the debug­ger you will be pre­sented with much more help­ful view of what’s in the con­tainer and what’s poten­tially not right.

debugger_visualizer

You will see a very min­i­mal­is­tic view of what’s going on in the container:

  • All com­po­nents – will give you a list of all exist­ing com­po­nents in the con­tainer, kind of like WhatDoIHave
  • Facil­i­ties will give you the list of the installed facilities
  • Poten­tially mis­con­fig­ured com­po­nents – this is a list of com­po­nents that don’t look to good to Wind­sor and may have some depen­den­cies miss­ing. It also is a very sim­pli­fied view. At the first glance it will only tell you the key of the com­po­nent (in this case fully qual­i­fied name), slash service/implementation. In this case both ser­vice and imple­men­ta­tion are the same, so it won’t repeat the infor­ma­tion.
    When you drill down, you will also see the lifestyle of the com­po­nent and most impor­tantly its sta­tus, which will tell you why Wind­sor thinks there may be some­thing wrong with the component.

debugger_visualizer2

This pretty nicely tells you what might be wrong. If you are sure you pro­vid­ing these val­ues dynam­i­cally, be it from the call site or via dynamic para­me­ters, you can move on, oth­er­wise it can remind you of the miss­ing dependencies.

I want your feedback

How do you like this fea­ture? How would you change it? What other infor­ma­tion you think would be use­ful in this view? Leave a com­ment, or go to user­voice site to share your ideas.

Thanks

How I use Inversion of Control containers – pulling from the container

Tuesday, June 22nd, 2010

As I expected my pre­vi­ous post prompted a few ques­tions regard­ing the Three Con­tainer Calls pat­tern I out­lined. Major one of them is how to han­dle the fol­low­ing scenario:

Container_pulling

We cre­ate our con­tainer, install all the com­po­nents in it.  Moments later we resolve the root com­po­nent, con­tainer goes and cre­ates its entire tree of depen­den­cies for us, does all the con­fig­u­ra­tion and other book­keep­ing, injects all the depen­den­cies and gives us back the new fresh ready to use objects graph that we then use to start off the application.

More often than not, this will not be enough though. At some point later in the life­cy­cle of the appli­ca­tion, a user comes in and wants some­thing from the appli­ca­tion. Lets say the user clicks a but­ton say­ing “Open a file” and at this point a “Open­File” view should be loaded along with all of its depen­den­cies and pre­sented to the user. But wait – it gets worse – Because now if a user selects a .txt file a “Dis­play­Text” view should be loaded, but when a .png file gets selected, we should load a “Dis­play­Im­age” view.

Gen­er­al­iz­ing – how to han­dle sit­u­a­tions where you need to request some depen­den­cies unavail­able at reg­is­tra­tion or root res­o­lu­tion time, poten­tially pass­ing some para­me­ters to the com­po­nent to be resolved, or some con­tex­tual infor­ma­tion affect­ing which com­po­nent will be resolved (or both).

Fac­tory alive and kicking

The way to tackle this is to use a very old design pat­terns (from the orig­i­nal GOF pat­terns book) called Abstract fac­tory and Fac­tory method.

Nit­pick­ers' cor­ners – as I know I will be called out by some purists that what I’m describ­ing here is not purely this or that pat­tern accord­ing to this or that def­i­n­i­tion  – I don’t care, so don’t bother. What’s impor­tant is the concept.

It gives us exactly what we need – pro­vides us with some com­po­nents on demand, allow­ing us to pass some inputs in, and on the same time encap­su­lat­ing and abstract­ing how the object is con­structed. The encap­su­lat­ing part means that I totally don’t need to care about how, and where the com­po­nents get cre­ated – it’s com­pletely offloaded to the con­tainer. The abstract­ing part means that I can still adhere to the Matrix Prin­ci­ple – the spoon con­tainer does not exist.

How does this work?

Using Cas­tle Wind­sor, this is very sim­ple – all you have to do is to declare the fac­tory inter­face, and Wind­sor itself will pro­vide the imple­men­ta­tion. It is very impor­tant. Your inter­face lives in your domain assem­bly, and knows noth­ing about Wind­sor. The imple­men­ta­tion is a detail, which depends on the abstrac­tion you pro­vided, not the other way around. This also means that most of the time all of the work you have to do is sim­ply declar­ing the inter­face, and leave all the heavy lift­ing to Wind­sor, which has some pretty smart defaults that it will use to fig­ure this stuff out by itself. For non-standard cases, like decid­ing which com­po­nent to load based on the exten­sion of the file pro­vided, you can eas­ily extend the way fac­tory works by using cus­tom selec­tor. With well thought out nam­ing con­ven­tion, cus­tomiz­ing Wind­sor to sup­port this sce­nario is lit­er­ally one line of mean­ing­ful code.

In addi­tion to that (although I pre­fer the interface-driven approach) Wind­sor (upcom­ing ver­sion 2.5), and some other con­tain­ers, like Aut­o­fac (which first imple­mented the con­cept), and Struc­tureMap (don’t really know about Nin­ject, per­haps some­one can clar­ify this in the com­ments) sup­port del­e­gate based fac­to­ries, where just the fact of hav­ing a depen­dency on Func<IFoo> is enough for the con­tainer to fig­ure out that it should inject for that depen­dency a del­e­gate that when invoked will call back to the con­tainer to resolve the IFoo ser­vice. The con­tainer will even imple­ment the del­e­gate for you, and it also obvi­ously means that the code you have has no idea (nor does it care) that the actual depen­dency was cre­ated by a container.

 

This is the beauty of this approach – it has all the pros and none of the cons of Ser­vice Loca­tor, and shift towards sup­port­ing it was one of the major changes in the .NET IoC back­yard in the last cou­ple of months, so if you are still using Ser­vice Loca­tor you’re doing it wrong.

How I use Inversion of Control containers

Sunday, June 20th, 2010

Quite reg­u­larly I get asked by peo­ple how they should use IoC con­tainer in their appli­ca­tion. I don’t think I can answer this ques­tion once and uni­ver­sally because every appli­ca­tion is dif­fer­ent, every appli­ca­tion has dif­fer­ent needs and every appli­ca­tion has dif­fer­ent oppor­tu­ni­ties for lever­ag­ing an Inver­sion of Con­trol container.

How­ever there are some gen­eral rules and pat­terns that I use and I thought I will blog about this instead.

While I use a con­crete exam­ple of Cas­tle Wind­sor, the dis­cus­sion here is universal.It applies to all containers.

Inver­sion of Con­trol means con­tainer does not exist

Container_does_not_exist

Basic dif­fer­ence between an Inver­sion of Con­trol frame­work and any other kind of frame­work is that the con­trol gets inverted. Your appli­ca­tion does not call to the frame­work. Instead the frame­work is aware of your appli­ca­tion and it goes and does its stuff with your application’s objects.

Since Inver­sion of Con­trol Con­tain­ers are Inver­sion of Con­trol frame­works that par­a­digm applies to them as well. They con­trol objects in your appli­ca­tion. They instan­ti­ate them, man­age their life­cy­cle, invoke meth­ods on them, mod­ify them, con­fig­ure them, dec­o­rate them, do all sorts of stuff with them. The main point here is – the appli­ca­tion is com­pletely unaware of that. I feel tempted to put another Matrix anal­ogy here, but hope­fully you get the point with­out it.

The most vis­i­ble man­i­fes­ta­tion of this fact, which clearly illus­trates the lack of any knowl­edge about the con­tainer is that I tend not to ref­er­ence the con­tainer in the appli­ca­tion at all. The only place where the ref­er­ence to the con­tainer does appear is the root project which only runs the appli­ca­tion. It how­ever con­tains no appli­ca­tion logic and serves merely as appli­ca­tion entry point and con­tainer bootstrapper.

Not ref­er­enc­ing the con­tainer at all serves a few pur­poses. Most impor­tantly it helps to enforce good OOP design, and blocks the temp­ta­tion to take short­cuts and use the con­tainer as Ser­vice Loca­tor.

Three calls pat­tern of usage

Now, prob­a­bly the most inter­est­ing part is this sim­ple boot­strap­per. How do I inter­act with the container?

I tend to use pat­tern of three calls to the con­tainer. Yes you heard it right – I only call the con­tainer is three places in the entire appli­ca­tion* (con­di­tions apply, but I’ll dis­cuss this below in just a moment).

My entire inter­ac­tion with the con­tainer usu­ally looks like this:

   1: var container = BootstrapContainer();

   2:  

   3: var finder = container.Resolve<IDuplicateFinder>();

   4: var processor = container.Resolve<IArgumentsParser>();

   5: Execute( args, processor, finder );

   6:  

   7: container.Dispose();

The three steps are:

  1. Boot­strap the container
  2. Resolve root components
  3. Dis­pose the container

Let’s go over them in turn:

One Install to rule them all…

Boot­strap­ping is incred­i­bly simple:

   1: private IWindsorContainer BootstrapContainer()

   2: {

   3:     return new WindsorContainer()

   4:         .Install( FromAssembly.This() );

   5: }

The most impor­tant rule here (which is impor­tant in Wind­sor, but good to fol­low with other con­tain­ers that enable this)  is – to call Install just once and reg­is­ter and con­fig­ure all the com­po­nents dur­ing this sin­gle call. Also impor­tant stuff is to use the Install method and Installers to encap­su­late and par­ti­tion reg­is­tra­tion and con­fig­u­ra­tion logic. Most other con­tain­ers have that capa­bil­ity as well. Aut­o­fac and Nin­ject call it mod­ules, Struc­tureMap calls it registries.

As you can see on the screen­shot above I usu­ally have a ded­i­cated folder called Installers in my boot­strap assem­bly where I keep all my installers. I tend to par­ti­tion the installers so that each one of them installs some cohe­sive and small set of ser­vices. So I might have View­Mod­elsIn­staller, Con­trollers Installer, Back­ground­Ser­vicesIn­staller, Log­gin­gIn­staller etc. Each installer is sim­ple and most of them look sim­i­lar to this:

   1: public class ArgumentInterpretersInstaller : IWindsorInstaller

   2: {

   3:     public void Install(IWindsorContainer container, IConfigurationStore store)

   4:     {

   5:         container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));

   6:         container.Register(

   7:             Component.For<IArgumentsParser>()

   8:                 .ImplementedBy<ArgumentsParser>().LifeStyle.Transient,

   9:             Component.For<IParseHelper>().ImplementedBy<ParseHelper>(),

  10:             AllTypes.FromAssemblyContaining<IArgumentInterpreter>()

  11:                 .BasedOn<IArgumentInterpreter>()

  12:                 .WithService.Base());

  13:     }

  14: }

 

This par­ti­tion­ing helps keep things tidy, and by lever­ag­ing Installers you end up writ­ing less code, as now Wind­sor will autodis­cover them and reg­is­ter in just a sin­gle call to FromAssembly.This(). Also another note­wor­thy fact about this, is that you lever­age Inver­sion of Con­trol prin­ci­ple to con­fig­ure the con­tainer itself. Instead of pass­ing the con­tainer around, which should always raise a read flag, you’re telling Wind­sor – con­fig­ure your­self. Nice and tidy.

Another impor­tant thing to notice, is that I tend to lever­age con­ven­tion based reg­is­tra­tion, rather than reg­is­ter­ing all the com­po­nents one by one. This greatly cuts down the size of your reg­is­tra­tion code. It takes the bur­den of reg­is­ter­ing each newly added com­po­nents man­u­ally off of your shoul­ders. It also enforces con­sis­tency in your code, because if you’re not con­sis­tent your com­po­nents won’t get registered.

* yes – I do inter­act with the con­tainer in the installers, so I clearly break the three calls rule, right? No – I inter­act with the con­tainer in objects that extend, or mod­ify the con­tainer itself – in this case it’s obvi­ously not a bad thing.

…and in one Resolve bind them

Sim­i­lar to unit tests prin­ci­ple – one log­i­cal assert per tests, I fol­low the rule of allow­ing explicit call to resolve in just one place in the entire appli­ca­tion. Usu­ally this will be just one call, that pulls the root com­po­nent (Con­troller in MVC appli­ca­tion, Shell in WPF appli­ca­tion, or what­ever the root object in your app is). In the exam­ple above I have two root objects so I have two calls to Resolve. That’s usu­ally OK. How­ever if you have more than three, you might want to take a closer look at rea­sons for that, as it’s quite unlikely you really need this.

The impor­tant thing is to have the Resolve calls in just this one sin­gle place and nowhere else in your appli­ca­tion. Why that’s impor­tant? To fully lever­age container’s poten­tial, instead of telling it at every step what it should do. Let it spread its wings.

Clean up

It is impor­tant to let the con­tainer clean up after itself, when its done doing its job. In this case I can not only say that this is some­thing I do. You also always should dis­pose your con­tainer at the end of your appli­ca­tion. Always, no excep­tions. This will let the con­tainer to shut­down grace­fully, decom­mis­sion all the com­po­nents, give them chance to clean up after them­selves, and free all the resources they may occupy.

 

What about you? How do you use your container?

Castle Windsor and child containers: Revolutions

Thursday, June 3rd, 2010

Con­tin­u­ing the topic from the pre­vi­ous posts.

What would happen?

Cur­rent behav­ior of Wind­sor is some­what flawed. What it will do is it will resolve foo, and pro­vide it with bar. The flaw of this behav­ior is that now when we resolve foo via any of the tree con­tain­ers we’ll  get the same instance (since it’s a sin­gle­ton). This intro­duced two issues:

  • com­po­nent from childA (bar) will now be avail­able via childB and par­ent while log­i­cally think­ing – it should not.
  • we have tem­po­ral cou­pling in our code. Depend­ing on the order of con­tain­ers we’ll use to resolve foo we’ll get dif­fer­ent results

So what should happen?

Ok, so now that we agreed that cur­rent behav­ior is indeed flawed (we did, didn’t we?) what are our options for fix­ing it? We basi­cally have two options (both of which were men­tioned in com­ments to pre­vi­ous post).

It all boils down to scop­ing. If we have a per-web-request object – should sin­gle instance be shared among mul­ti­ple con­tain­ers or should it be per-web-request-and-per-container? If we have sin­gle­ton should it be sin­gle instance per con­tainer, per con­tainer hier­ar­chy or per process?

Let’s con­sider slightly more com­pli­cated picture.

containers_2

Now we have two com­po­nents for bar, one in childA and one in par­ent. Also we have one more com­po­nent; baz, which is reg­is­tered in childB.

classes_2

Baz has depen­dency on foo, foo still has depen­dency on bar. All of these depen­den­cies are optional, and all com­po­nents are singletons.

There can only be one

We want to scope instances strictly per their con­tainer. So that foo is scoped in par­ent (thus vis­i­ble from its child con­tain­ers as well), and bar is scoped per childA. This appears to be the sim­plest setup, and the most in line with def­i­n­i­tion of sin­gle­ton, but then we run into prob­lems out­lined above.

We then could add another con­straint – depen­den­cies can only face upwards the con­tainer hier­ar­chy. We would then get foo with its depen­dency on bar pulled from par­ent con­tainer, con­sis­tently, regard­less of the con­tainer we’d resolve it from. More­over, we could resolve baz from childB and its depen­dency would be prop­erly pop­u­lated from par­ent since it comes from a con­tainer that is higher in the hier­ar­chy, so we’re safe to pull that dependency.

On the other hand this robs us of one of nice (and often used) side effect of child con­tain­ers, that is con­tex­tual over­rid­ing of com­po­nents from con­tain­ers higher up the hierarchy.

If we have bar in childA, we’d expect to get foo pulled via childA to have that bar, not parent’s bar.

Or per­haps not?

We can approach the prob­lem from another angle alto­gether. We can nar­row down the scope of a com­po­nent instance, to the scope of its out­er­most depen­dency. What do I mean by that?

When resolv­ing foo from childA the out­er­most depen­dency of foo would be the bar liv­ing in childA. There­fore the instance of foo would have bar pulled from par­ent, but scoped in childA. There­fore when we’d request foo again, but this time from childB we’d get another instance of foo, with depen­dency on bar pulled from par­ent. This could poten­tially lead to prob­lems as well, since if you’re depend­ing on the fact that sin­gle instance of your com­po­nent will ever live in the appli­ca­tion at given point in time you may end up with problems.

So what do you think? Which approach would you pre­fer, or is there some­thing I’m miss­ing here?

To those who though I’m seri­ously going to remove sup­port for nested con­tain­ers rest assured this is not going to hap­pen. I still think this could be a viable option and I wanted to throw it into the air, but its become such a core fea­ture of each IoC con­tainer, that Wind­sor would look crip­pled if it didn’t have it, regard­less of whether or not it’d be actu­ally needed.