Last active
December 11, 2015 02:19
-
-
Save akimboyko/4530019 to your computer and use it in GitHub Desktop.
Ninject.Extensions.Conventions unable to resolve which of two implementation to use
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() | |
{ | |
// create kernel with conventions | |
using(var kernel = InitializeKernel()) | |
{ | |
// ninject.extensions.conventions would not automaticaly select wich of | |
// two implementations of IDependency to resolve | |
var dependency = kernel.Get<IDependency>(); | |
Assert.That(dependency, | |
Is.Not.Null | |
.And.InstanceOfType(typeof(DependencyFirstImplementation))); | |
var service = kernel.Get<IService>(); | |
Assert.That(service, | |
Is.Not.Null | |
.And.InstanceOfType(typeof(ServiceImplementation)) | |
.And.Property("Dependency").InstanceOfType(typeof(DependencyFirstImplementation))); | |
} | |
} | |
public static IKernel InitializeKernel() | |
{ | |
var kernel = new StandardKernel(); | |
// ninject.extensions.conventions | |
kernel.Bind(x => x | |
.FromThisAssembly() | |
.SelectAllTypes() | |
.BindAllInterfaces() | |
.Configure((b, c) => | |
b.InTransientScope().Named(c.Name))); | |
return kernel; | |
} | |
public interface IService | |
{ | |
IDependency Dependency { get; } | |
} | |
public interface IDependency { } | |
public class ServiceImplementation : IService | |
{ | |
public IDependency Dependency { get; private set; } | |
public ServiceImplementation(IDependency dependency) | |
{ | |
Dependency = dependency; | |
} | |
} | |
public class DependencyFirstImplementation : IDependency { } | |
public class DependencySecondImplementation : IDependency { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment