1

I would like to be able to resolve an enumerable collection of IRepository<T> where T : IDocument

In my registry class I've added the following registration code:

this.For<IRepository<IDocument>>().Add<Repository<Document>>();
this.For<IRepository<IDocument>>().Add<Repository<AnotherDocumentType>>();

Both the classes Document and AnotherDocumentType implement the interface IDocument, and Repository<T> implements IRepository<T>. I'm new to structure map, and I don't really understand the error message. All classes involved have default constructors.

Structuremap is throwing the following error Test Name: can_resolve_a_collection_of_document_repositories Test FullName: my.test.IoCTest.can_resolve_a_collection_of_document_repositories Test Source: d:\Projects\level\my\mtrunk\src\my.test\IoCTest.cs : line 46 Test Outcome: Failed Test Duration: 0:00:00.138309

Result Message: Test method my.test.IoCTest.can_resolve_a_collection_of_document_repositories threw exception: StructureMap.Exceptions.StructureMapConfigurationException: StructureMap configuration failures: Error: 104 Source: Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223 Type Instance '613169b9-b8d9-4c80-868a-d6aa47e3d95c' (Configured Instance of my.data.Impl.Repositories.Repository1[[my.domain.Document, my.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], my.data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null) cannot be plugged into type my.data.IRepository1[[my.domain.IDocument, my.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], my.data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Error: 104 Source: Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223 Type Instance '2196350f-0c64-4bd2-92af-9946f1e11862' (Configured Instance of my.data.Impl.Repositories.Repository1[[my.domain.AnotherDocumentType, my.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], my.data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null) cannot be plugged into type my.data.IRepository1[[my.domain.IDocument, my.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], my.data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Result StackTrace: at StructureMap.Diagnostics.GraphLog.AssertFailures() in c:\BuildAgent\work\767273992e840853\src\StructureMap\Diagnostics\GraphLog.cs:line 68 at StructureMap.Container.construct(PluginGraph pluginGraph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Container.cs:line 576 at StructureMap.Container..ctor(PluginGraph pluginGraph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Container.cs:line 55 at StructureMap.ObjectFactory.Initialize(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\ObjectFactory.cs:line 65 at my.test.IoCTest.can_resolve_a_collection_of_document_repositories() in d:\Projects\level\my\mtrunk\src\my.test\IoCTest.cs:line 51

Jason
  • 15,915
  • 3
  • 48
  • 72
  • I haven't used StructureMap, but I would guess that this doesn't work, because you are trying to register two implementations for the same interface. How should the container know, which to return when you want to resolve `IRepository`? – EagleBeak Oct 04 '13 at 16:39
  • Nope, most containers will allow this. StructureMap specifically has an `Add` method that is used when you want to resolve a collection of instances (as I do) – Jason Oct 04 '13 at 19:09
  • 1
    I use this.For(typeof(IRepository<>)).Use(typeof(Repository<>)); to map all Repository implementations in one shot. Maybe this will be helpful. – Vinod Kumar Y S Oct 06 '13 at 20:39

2 Answers2

2

The problem isn't StructureMap but the way you're using generics.
Does IRepository<IDocument> foo = new Repository<Document>(); build?
Make IRepository covariant (i.e. public interface IRepository<out T> where T : IDocument, the out part is important) and try again.

David
  • 3,736
  • 8
  • 33
  • 52
  • Yep, thats it. Too bad I can't change the signature of the IRepository interface. – Jason Oct 05 '13 at 14:56
  • Would wrapping the repo help? `public interface IRepository { T GetFoo(); } public interface IMyRepository { T GetFoo(); } public class MyRepository : IMyRepository { private readonly IRepository _repo; public MyRepository(IRepository repo) { _repo = repo; } public T GetFoo() { return _repo.GetFoo(); } }` – David Oct 06 '13 at 10:36
-1

I had the same error code.

I made the dumb mistake of not adding the Interface to the Class.

BEFORE

public class MyClass //No Interface

AFTER

public class MyClass : IMyClass

"That's like so me dude!"

christo8989
  • 6,442
  • 5
  • 37
  • 43