Search code examples
c#dependency-injectionsimple-injector

Converting IsClosedTypeOf from autofac to simple.injector


I am following the CQS posts by .net junkie and have implemented a QueryProcessor. In this post he quotes:-

Using the IQueryProcessor means we have to write a test that confirms there is a corresponding query handler for every query in the system, because the DI framework can not check this for you.

In the comments of this post someone has created a test using that uses an extension method IsClosedTypeOf(typeof(IQuery<>)) I would like to use

However I am struggling to work out how to convert/create an extension method IsClosedTypeOf for use without using auto-fac?

    var allQueryTypes = Assembly.GetAssembly(typeof(IQuery<>)).GetTypes()
            .Where(t => t.IsClass && t.IsClosedTypeOf(typeof(IQuery<>)))
            .ToList();

I am not even 100% sure what IsClosedTypeOf does as I am not familiar with autofac

Source of test code.


Solution

  • var allQueryTypes =
        from assembly in AppDomain.CurrentDomain.GetAssemblies()
        from type in assembly.GetTypes()
        where !type.IsAbstract && !type.IsGenericTypeDefinition
        let queryInterfaces =
            from iface in type.GetInterfaces()
            where iface.IsGenericType
            where iface.GetGenericTypeDefinition() == typeof(IQuery<>)
            select iface
        where queryInterfaces.Any()
        select type;