Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created January 2, 2013 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/4437787 to your computer and use it in GitHub Desktop.
Save akimboyko/4437787 to your computer and use it in GitHub Desktop.
Ninject, static constructor and named bindings
void Main()
{
using(var kernel = new StandardKernel())
{
kernel
.Bind<IDocumentService>()
.ToMethod(x => CoDocumentService.Create(x.Kernel.Get<IMessage>(),x.Kernel.Get<IClientChannel>("IClientChannel")))
.InTransientScope();
kernel
.Bind<IAccountService>()
.ToMethod(x => CoAccountService.Create(x.Kernel.Get<IMessage>(),x.Kernel.Get<IClientChannel>("IAccountService")))
.InTransientScope();
kernel
.Bind<IClientChannel>()
.To<UniversalClientChannel>()
.Named("IClientChannel")
.WithPropertyValue("Number",x => 42);
kernel
.Bind<IClientChannel>()
.To<UniversalClientChannel>()
.Named("IAccountService")
.WithPropertyValue("Number",x => 5);
kernel
.Bind<IMessage>()
.To<Message>();
var docService = kernel.Get<IDocumentService>();
docService.Dump();
var accService = kernel.Get<IAccountService>();
accService.Dump();
}
}
public interface IDocumentService { }
public interface IAccountService { }
public interface IMessage { }
public interface IClientChannel
{
int Number { get; set; }
}
public class CoDocumentService : IDocumentService
{
public IMessage Message { get; private set; }
public IClientChannel ClientChannel { get; private set; }
private CoDocumentService() { }
public static IDocumentService Create(IMessage message, IClientChannel clientChannel)
{
return new CoDocumentService
{
Message = message,
ClientChannel = clientChannel
};
}
}
public class CoAccountService : IAccountService
{
public IMessage Message { get; private set; }
public IClientChannel ClientChannel { get; private set; }
private CoAccountService() { }
public static IAccountService Create(IMessage message, IClientChannel clientChannel)
{
return new CoAccountService
{
Message = message,
ClientChannel = clientChannel
};
}
}
public class Message : IMessage { }
public class UniversalClientChannel : IClientChannel
{
public int Number { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment