Update to DI Container initialization

Posted Thursday, March 13, 2008 by

 Since I wrote the earlier code in "Using LINQ to initialize DI Containers" I've cleaned up the LINQ query to incorporate the assemblies loop and also to use the First extension method to make things a bit more readable.

var services = from a in assemblies

               from t in Assembly.Load(a).GetTypes()

               where t.GetInterfaces().Length > 0 && t.IsSubclassOf(typeof(ServiceBase))

               let i = t.GetInterfaces().First()

               where !Container.Kernel.HasComponent(i)

               select new

               {

                   Interface = i.IsGenericType ? i.GetGenericTypeDefinition() : i,

                   Component = t

               };

 

foreach(var service in services)

{

    Container.AddComponent(service.Interface.FullName, service.Interface, service.Component);

}

 

 

Professional Windows App Development