views:

1056

answers:

3

Hi,

I've got an interface:

IRepository<T> where T : IEntity

while im knocking up my UI im using some fake repository implementations that just return any old data.

They look like this:

public class FakeClientRepository : IRepository<Client>

At the moment im doing this:

ForRequestedType<IRepository<Client>>()
   .TheDefaultIsConcreteType<FakeRepositories.FakeClientRepository>();

but loads of times for all my IEntities. Is it possible to use Scan to auto register all my fake repositories for its respective IRepository?

Edit: this is as far as I got, but i get errors saying the requested type isnt registered :(

Scan(x =>
{
    x.TheCallingAssembly();
    x.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
    x.AddAllTypesOf(typeof(IRepository<>));
    x.WithDefaultConventions();
});

Thanks

Andrew

+2  A: 

Take a look at this discussion from the StructureMap users group: http://groups.google.com/group/structuremap-users/browse_thread/thread/649f5324c570347d

Chris Marisic
cool, looks like its the right thing, ill check it out tomorrow
Andrew Bullock
+3  A: 

Thanks Chris, thats exactly what I needed. For clarity, heres what I did from your link:

Scan(x =>
{
 x.TheCallingAssembly();
        x.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
 x.With<FakeRepositoryScanner>(); 
});


private class FakeRepositoryScanner : ITypeScanner
{
 public void Process(Type type, PluginGraph graph)
 {
  Type interfaceType = type.FindInterfaceThatCloses(typeof(IRepository<>));
  if (interfaceType != null)
  {
   graph.AddType(interfaceType, type);
  }
 }
}
Andrew Bullock
+1  A: 

There is an easier way to do this. Please see this blog posting for details: Advanced StructureMap: connecting implementations to open generic types

public class HandlerRegistry : Registry
{
    public HandlerRegistry()
    {
        Scan(cfg =>
        {
            cfg.TheCallingAssembly();
            cfg.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
            cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
        });
    }
}

Doing it this way avoids having to create your own ITypeScanner or conventions.

JD Courtoy