Created
January 2, 2013 20:41
-
-
Save akimboyko/4437787 to your computer and use it in GitHub Desktop.
Ninject, static constructor and named bindings
This file contains hidden or 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
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