tags:

views:

71

answers:

1

I have a LoggedTextWriter that I'd like to inject in to the Log property of the LinqToSql DataContext class.

My custom LoggedTextWriter has a constructor that takes ICustomWriter, but I don't know how to inject it in to the Log property.

Bind<DataContext>()
                .ToSelf()
                .InTransientScope()
                .WithConstructorArgument("connection", @"Data Source=localhost\sqlexpress2008;Initial Catalog=MyDB;Integrated Security=True")
                .WithPropertyValue("ObjectTrackingEnabled", true)
                .WithPropertyValue("Log", **<HowDoIGetAnInstanceOfLoggedTextWriter>**);

Bind<LoggedTextWriter>().ToSelf().InTransientScope();

Bind<ICustomWriter>().To<MyCustomWriter>().InTransientScope();
+2  A: 

Like this! Bind using ToMethod, the context (x below) is passed to your lambda. You can use it to lookup the Kernel and find your log. This is very similar to how AutoFac and Funq work. Also, transient is the default scope, so you can remove it from your bindings if you'd like.

Bind<LoggedTextWriter>().ToSelf();

Bind<ICustomWriter>().To<MyCustomWriter>();

Bind<DataContext>().ToMethod(x => 
    new DataContext(@"Data Source=localhost\sqlexpress2008;Initial Catalog=MyDB;Integrated Security=True")
    {
        ObjectTrackingEnabled = true,
        Log = x.Kernel.Get<LoggedTextWriter>()
    });
Andy S