Update to DI Container initialization

 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);

}

 

 

Shout It Kick It submit to reddit

0 Comments

Comments are closed