Skip to content

Instantly share code, notes, and snippets.

@davehng
Created December 30, 2014 00:17
Show Gist options
  • Save davehng/062dbe64de15c333d702 to your computer and use it in GitHub Desktop.
Save davehng/062dbe64de15c333d702 to your computer and use it in GitHub Desktop.
StructureMap registration to register NHibernate Linq IQueryables for all entity types
/// <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() });
});
}
}
}
@davehng
Copy link
Author

davehng commented Dec 30, 2014

Possibly completely evil 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment