Unity: how to resolve class through base interface

Here is my repository class:

public interface IMyRepository : IRepository<IMyEntity>{}

public class MyRepository : IMyRepository

Here is way to register it:

container.Register<IMyRepository, MyRepository >();

Here is the way how I want to get repository resolved:

IRepository<IMyEntity> repository = container.Resolve<IRepository<IMyEntity>>();

Attempt to resolve repository in this ways give an error:

Resolution of the dependency failed, type = "CMCore.Repository.IRepository`1[CMCore.Data.ICmCoreLog]", name = "(none)". Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, IRepository`1[IMyEntity], is an interface and cannot be constructed. Are you missing a type mapping?

At the time of the exception, the container was:

Resolving IRepository`1[IMyEntity],(none)

What is wrong in my approach? What is a proper way to achieve mentioned functionality?

Thanks a lot!

P.S. Sometime I want to resolve my class through the IMyRepository, sometime through IRepository. Should I register class twice?



container.RegisterType<IRepository<IMyEntity>, MyRepository>();

instead. Unity, by design, only does one level of type mapping. It's only going to look for a mapping from the type you ask, it doesn't chase down inheritance trees.

If you want it to be available both a IRepository or as IMyRepository, then just register it twice:

container.RegisterType<IRepository<IMyEntity>, MyRepository>()
    .RegisterType<IMyRepository, MyRepository>();

Need Your Help

Enum Conversion to String

c# asp.net data-binding dataset

I have created Enum of Leave Reason and now i want to convert it into string but error comes up.

Infinite Loop? Can't see why

c# asp.net

I have a function that takes in a roleID(as an int), and compares it to a multidimensional array to see if that array contains that roleID. if it does, it sets a bool to true and breaks. if it does...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.