Don't Pass the Container Around

667 views
Skip to first unread message

janu...@gmail.com

unread,
Mar 3, 2009, 7:31:31 PM3/3/09
to Autofac
Hi there

Trying to learn something about best practice :)

It's written "Don't Pass the Container Around", please take a look at
my first prove of concepts. You can see that I actually created a
static version... (not singleton for this poc)
How do I wire u all my dependencies then? I would like to do the
wiring from within a single place and not using config files too
much :)

Kind regards
Janus007

class Program
{
static void Main(string[] args)
{
Console.Write(string.Format("Shaking hands with Mr.
Message"));
Console.WriteLine();

var im = ModuleBootStrapper.Container.Resolve<IMessage>();

Console.WriteLine(im.HandShake("Main"));


Console.ReadLine();


}

....

public class ModuleBootStrapper
{
public static Autofac.IContainer Container
{
get
{
var builder = new Autofac.Builder.ContainerBuilder();
builder.Register<Message>().As<IMessage>();

return builder.Build();
}

}
}

Nicholas Blumhardt

unread,
Mar 3, 2009, 11:10:07 PM3/3/09
to aut...@googlegroups.com
Hi Janus,

The best idea is to factor all of your registrations into modules. See: http://code.google.com/p/autofac/wiki/StructuringWithModules

Once you've registered all of your application's modules (and perhaps some standalone components too) you can resolve one of them and call whatever methods are required to invoke your application functionality.

HTH

Nick

janu...@gmail.com

unread,
Mar 4, 2009, 3:56:27 AM3/4/09
to Autofac
Hi

To get this right..., I need to create a module and then load the
module from XmlConfiguration ? And then create the more advanced
wiring inside the module?


Could you help me on track with this? Best thing would be if you could
take my above code sample and show me the light :)


//
public class Message : IMessage
{
string ctor;

public int MyProperty { get; set; }

public Message() { }

public Message(string ctor)
{
this.ctor = ctor;
}

public string HandShake(string caller)
{
if(string.IsNullOrEmpty(this.ctor))
return "Hello " + caller + ", I'm a message";
else
return "Hello " + caller + ", I'm a message, now with
a constructor paramvalue: " + ctor;
}
}

//
public interface IMessage
{
string HandShake(string caller);
int MyProperty { get; set; }

}

//


Nicholas Blumhardt

unread,
Mar 4, 2009, 10:40:04 AM3/4/09
to aut...@googlegroups.com
Sure thing.

Create modules for app features:

class MessagingModule : Module {

  override Load(ContainerBuilder builder) {

      builder.Register<Message>().As<IMessage>().FactoryScoped();

      // ... register other messaging-related components
  }
}

Then, in 'main':

var builder = new ContainerBuilder();
builder.RegisterModule(new MessagingModule());
// ... register other modules
var container = builder.Build();
var message = container.Resolve<IMessage>();
message.HandShake()....

Cheers,
Nick

Rinat Abdullin

unread,
Mar 5, 2009, 12:18:07 AM3/5/09
to Autofac
Janus,

you may also get some ideas from this article on configuring with
autofac modules:
http://abdullin.com/journal/2009/2/12/structuring-net-applications-with-autofac-ioc.html

Best regards,
Rinat Abdullin
Reply all
Reply to author
Forward
0 new messages