Created
February 23, 2011 15:47
-
-
Save hotgazpacho/840575 to your computer and use it in GitHub Desktop.
A StructureMap IRegistrationConvention for mapping IRepository<T> to GenericRepository<T>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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