Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hotgazpacho/840575 to your computer and use it in GitHub Desktop.

Select an option

Save hotgazpacho/840575 to your computer and use it in GitHub Desktop.
A StructureMap IRegistrationConvention for mapping IRepository<T> to GenericRepository<T>
using System;
using MyApplication.Domain;
using MyApplication.Persistence;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace MyApplication
{
public class GenericRepositoryRegistrationConvention : IRegistrationConvention
{
static readonly Type _openGenericRepositoryType = typeof (GenericRepository<>);
static readonly Type _openRepositoryInterfaceType = typeof (IRepository<>);
public void Process(Type type, Registry registry)
{
if(!type.IsAbstract && typeof(IEntity).IsAssignableFrom(type))
{
Type closedGenericRepositoryType = _openGenericRepositoryType.MakeGenericType(type);
Type closedRepositoryInterfaceType = _openRepositoryInterfaceType.MakeGenericType(type);
registry.For(closedRepositoryInterfaceType).Use(closedGenericRepositoryType);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment