Click here to Skip to main content
15,848,930 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Consider the two classes below.

public abstract class BaseService<tmodel> : IService<tmodel> where TModel : IModel

public class AccountCodeService : BaseService<accountcode> ,IAccountCodeService
BaseService implements IService<tmodel>.

Since AccountCodeSevice inherits from BaseService, it implements the IService<tmodel> interface as well as IAccountCodeService

I now register AccountCodeService in Unity:


Then resolve:
_accountCodeService = container.Resolve<iaccountcodeservice>();

The Problem:
_accountCodeService does not expose the elements in IService<tmodel>.

I tried:


Then resolve:
_accountCodeService = container.Resolve<iaccountcodeservice>();

Same issue:
_accountCodeService does not expose the elements in IService<tmodel>.

For sanity sake I tried:
var o = container.Resolve<iservice><accountcode>>();

Verified that o exposes the the elements in IService<tmodel>.

How do I get Unity to Resolve the class in a way that it exposes the elements from IService<tmodel> and IAccountCodeService?

Note: I found that if I duplicated the IService<tmodel> properties in IAccountCodeService, the app would compile and then the IService<tmodel> elements were exposed and were able to be called. I do not want to implement this way though.
Updated 29-May-12 5:50am

1 solution

In short, Unity container resolution resolves interfaces. So if you want a resolved reference which implements both IService<T> and IAccountCodeService, you need an interface which implements both. In this case it appears that IAccountCodeService should be that interface, so try interface IAccountCodeService extends IService<AccountCode> (and, obviously, have AccountCodeService implement it).

If your naming is bad, and you're actually after the more general case which can be summarised:
interface IService<T> { ... }
interface ISomethingElse { ... }
class Example implements ISomethingElse, IService<DataClass> { ... }

container.RegisterType<???, Example>();
var example = container.Resolve<???>(); // What can I put here?

... then you need to introduce a new interface which you can use to register:
interface IExample extends IService<DataClass>, ISomethingElse {}
class Example implements IExample { ... }

... and then you can use that interface for registration and resolution:
container.RegisterType<IExample, Example>();
var example = container.Resolve<IExample>();

Alternatively, if you are simply using Unity for service singleton reasons, it's possible to register concrete types directly, or just use a simpler singleton pattern and not bother with Unity at all. Dependency injection is greatly over-rated in my opinion.
Share this answer
Wbehning 29-May-12 22:28pm    
Thanks. Exactly what I needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900