Cannot resolve simple interface

May 30, 2008 at 12:09 PM
I'm trying to resolve simple interface with unity.
The configuration is:
      <typeAlias alias="IEcoSpace" type="Project.Config.IEcoSpace, Project" />
      <typeAlias alias="memoryEcoSpace" type="Project.Config.EcoSpaces.MemoryEcoSpace, Project" />
      <container name="model">
          <type type="IEcoSpace" mapTo="memoryEcoSpace" />
      <container name="services">
          <type type="IEcoSpace" mapTo="memoryEcoSpace" />
          <type type="Project.Services.AuthenticationService, Project" />

When I call contaier.Resolve<Project.Services.AuthenticationService>("services") it succeeds.

But when I call contaier.Resolve<IEcoSpace>("model") it fails with following error:
The current type, Project.Config.IEcoSpace, is an interface and cannot be constructed. Are you missing a type mapping?

The AuthenticationService has a constructor that accepts IEcoSpace. MemoryEcoSpace is inherited from AbstractEcoSpace which implements IEcoSpace.

I'm not sure what I'm doing wrong and how it happens that services container can resolve the dependency implicitly, but model container cannot resolve more explicit dependency.
Even more interesting, if I remove type registration for IEcoSpace (mapTo="memoryEcoSpace") from services container it still can resolve it.

Could you please explain me what I'm doing wrong and what's happening here?

Thanks in advance.
May 30, 2008 at 9:58 PM
You're trying to resolve using the name "model", but the type mapping doesn't include a name. They need to match. So either add "name="model"" to your type mapping, or do "container.Resolve<IEcoSpace>()" without the name.

Jun 4, 2008 at 12:55 AM
I see now.
I thought the name parameter is the container to try to resolve type in but not the alias to type.
I'd better read documentation :)