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
Use Autofac as an IoC container API Web API project with the types you will want to inject. Autofac also has a feature to scan assemblies and register types by name conventions.
StructureMap is a feature rich IoC tool with support for interception, object lifecycles and intelligent disposal patterns, open generic types, modular registrations, conventional registration, custom policies, and all the injection pattern support you would expect in a modern . Net IoC container.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With