Tutorial :How to get all possible generic type in StructureMap?



Question:

I just used StructureMap few days ago. I use StructureMap for collecting all validator class like the following code.

public class BaseClassA {}    public class ClassB : BaseClassA {}    public class ClassC : BaseClassB {}    public BaseClassAValidator : IValidator<BaseClassA>() {}  

In StructureMap, I only register IValidator<BaseClassA> interface for BaseClassAValidator class. But I want to get the same result when I call IValidator<ClassB> or IValidator<ClassC> that mean StructureMap should return IValidator<T> where T is requested class or parent class of requested class.

Is it possible? Or I need to manually call it.


Solution:1

From the answer to How to use a convention for IRepository<T> with StructureMap mapping

var c = new Container(x =>  {      x.Scan(scan =>      {          // there are other options to expand which assemblies to scan for types          scan.TheCallingAssembly();           scan.ConnectImplementationsToTypesClosing(typeof (IValidator<>));      });  });  

Now if you request IValidator<ClassC>, you should get BaseClassCValidator.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »