This is because the restriction is really different, so it does not satisfy the interface.
IModelBuilder<IBusinessTemplate> sample = new BusinessModelBuilder(); sample.Build(??)
The compiler will expect the Build arguments to be BaseModel , but since you may have a type that inherits from BaseModel , which is not a BusinessModel , this is obviously not valid.
Now, to get to the actual meat of the problem, you can solve it with another general argument.
public interface IModelBuilder<TemplateType, ConstraintType> { public ConstraintType Build<ConstraintType>(TemplateType template, params object[] parameters); }
Alternatively, you can switch amongst yourself and get some kind of general type of output if you really want to go crazy (if that even applies to your argument):
public interface IStandardTemplate<TModel> { } public interface IModelBinder<TModel> { TModel ApplyParameters(IStandardTemplate<TModel> template, params object[] parameters); } public class ModelBuilder { public TModel Build<TModel>(IStandardTemplate<TModel> template, params object[] parameters) { var model = Activator.CreateInstance<TModel>(); var modelBinder = ModelBinderFactory.CreateBinderFor(model); return modelBinder.ApplyParameters(template, parameters); } }
Then you can simply create the various ModelBinder classes and link them in the factory.
Call example:
public class BusinessTemplate : IStandardTemplate<BusinessModel> { } var businessTemplate = new BusinessTemplate(); var model = new ModelBinder().Build(businessTemplate);
source share