This project is read-only.

Autofac suggestions

Jun 27, 2009 at 8:17 PM

Hi Chip,

I had a look through the examples - this project will be a great cheat sheet for people learning or switching containers, great idea!

As far as the Autofac examples go, there are some minor things you might consider tweaking.

 

It is unnecesary to specify the return type of the lambda passed to Register(). Instead of:

builder.Register<Door>(c => new Door());

You can just say:

builder.Register(c => new Door());

 

In the Autofac XML configuration, specifying a default assembly lets you omit the assembly name from type names, e.g. instead of:

 

      <component

type="ContainerVariations.Common.Knob, ContainerVariations.Common"

service="ContainerVariations.Common.IKnob, ContainerVariations.Common"

        />

I think your config could read:

 

 

      <component

type="ContainerVariations.Common.Knob"

service="ContainerVariations.Common.IKnob"

        />

 

Also, the return type of a factory delegate is used as the default product, so instead of:

 

            builder.RegisterGeneratedFactory<NameAnalysis.Factory>(new TypedService(typeof (NameAnalysis)));

you can write:
            builder.RegisterGeneratedFactory<NameAnalysis.Factory>();
(It's worth noting that Castle has an interface-based version of this feature, too.)

 

I thought of another few examples you might include, off the top of my head:

  • Passing parameters by name, type or ordinal (explicitly, i.e. without factory delegates)
  • Hooking activation/post-activation events
  • Property and method wiring
  • Using a factory method

 

 

Hope this helps!

Nick

Jun 30, 2009 at 4:17 AM

Nick,

Thanks for looking the project over. A cheat sheet is exactly what I was going for.  That and a more disciplined examination of the various containers for my own education.

I've already committed the first few changes you suggested.

I had noticed that the return type on the Regiser call was redundant (Resharper makes it very clear) but decided to leave it explicit without much thought. After a little reflection I've decided to remove the redundancies.  I wouldn't leave them in in any other project, so they probably don't belong in this one either.

I like your suggestions for additional examples and the news about Castle's version of delegate factory.  They'll be fun to chase down.

Thanks again for your help and your great work on Autofac.

Chip