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>() {}开发者_如何学Python
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.
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
.
精彩评论