Created
December 30, 2014 00:17
-
-
Save davehng/062dbe64de15c333d702 to your computer and use it in GitHub Desktop.
StructureMap registration to register NHibernate Linq IQueryables for all entity types
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
/// <summary> | |
/// Creates registrations for NHibernate implementations of IQueryable<> for all derivatives of Entity. | |
/// </summary> | |
public class QueryableRegistrationConvention : IRegistrationConvention | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(typeof(QueryableRegistrationConvention)); | |
private static readonly Type OpenQueryableType = typeof(IQueryable<>); | |
public void Process(Type type, Registry registry) | |
{ | |
if (!type.IsAbstract && typeof(Entity).IsAssignableFrom(type)) | |
{ | |
Log.InfoFormat("Creating StructureMap registration for IQueryable<{0}>, implemented by NHibernate", type.Name); | |
var queryableType = OpenQueryableType.MakeGenericType(type); | |
registry.For(queryableType).Use(context => | |
{ | |
var databaseSession = context.GetInstance<IDatabaseSession>(); | |
var queryable = typeof(NhQueryable<>).MakeGenericType(type); | |
var ctor = queryable.GetConstructor(new Type[] { typeof(ISessionImplementor) }); | |
return ctor.Invoke(new object[] { databaseSession.Session.GetSessionImplementation() }); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possibly completely evil 😀