Entering an object when creating an object

I have a LoggedTextWriter that I would like to add to the Log property of the LinqToSql DataContext class.

My custom LoggedTextWriter has a constructor that accepts ICustomWriter, but I don't know how to insert it into 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(); 
+4
source share
1 answer

Like this! A binding using ToMethod, the context (x below) is passed to your lambda. You can use it to search for the kernel and search for your log. This is very similar to AutoFac and Funq. In addition, the transition mode is visible by default, so you can remove it from your bindings if you want.

 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>() }); 
+1
source

All Articles