Last active
August 29, 2015 14:20
-
-
Save a-h/437d5e7846126be91768 to your computer and use it in GitHub Desktop.
Migrating from Ninject to Simple Injector (WCF)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ninject | |
private static IKernel CreateKernel() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); | |
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); | |
// Singleton | |
kernel.Bind<ILog>().ToMethod(c => LogManager.GetLogger(typeof(ProjectService))).InSingletonScope(); | |
// WCF Scope | |
// Done by default in Ninject when you use the WCF package. | |
kernel.Bind<IEntities>().To<Entities>(); | |
kernel.Bind<IAssetManager>().To<AssetManager>(); | |
return kernel; | |
} | |
// Simple Injector | |
private static void Start() | |
{ | |
// Create the container as usual. | |
var container = new Container(); | |
// Register your types. | |
// Singleton. | |
container.RegisterSingle<ILog>(() => LogManager.GetLogger(typeof(ProjectService))); | |
// WCF Service Scope. This is different to Ninject, because using this Lifecycle in | |
// Simple Injector will result in only a single concrete object being created per WCF | |
// operation whereas Ninject can create multiple objects, but scoped to the WCF operation. | |
container.RegisterPerWcfOperation<IEntities, Entities>(); | |
// Transient Scope. | |
container.Register<IRepository, Repository>(); | |
// Register the container to the SimpleInjectorServiceHostFactory. | |
SimpleInjectorServiceHostFactory.SetContainer(container); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment