Reputation: 147
I wanted to know, if creating of marker dummy classes (with no members inside) just for the use in generic signatures is a good idea or not.
For example, think of an interface signature below:
public interface IBusinessValidatorFor<TContext, TEntity>
where TContext : IContext
{
IList<ValidationStatus> Validate (TEntity entityToValidate);
}
A sample concrete class that implements the above interface can be:
public sealed class BusinessValidatorFor<CreationContext, Customer>
: IBusinessValidatorFor<CreationContext, Customer>
{
public IList<ValidationStatus> Validate (Customer customerToValidate)
{
// Creation of customer related business validation code here.
}
}
In the above concrete class definition, the class CreationContext (which implements the marker interface IContext) is a dummy class with no properties, but used for just to distinguish it from other validators for the same Customer class, but for different other contexts.
Is this approach a good idea? Are there any better design alternatives than using dummy member-less empty classes?
The purpose of the above design was to allow creating of multiple validator concrete classes for the same entity, but for various contexts, and use it with a Dependency Injection container.
The following code shows the usage of such validators for various contexts:
public sealed class CustomerController
: Controller
{
private readonly IBusinessValidatorFor<CreationContext, Customer>
custCreateValidator;
private readonly IBusinessValidatorFor<ModificationContext, Customer>
custModifyValidator;
public CustomerController
(
IBusinessValidatorFor<CreationContext, Customer> custCreateValidator,
IBusinessValidatorFor<ModificationContext, Customer> custModifyValidator,
)
{
this.custCreateValidator = custCreateValidator;
this.custModifyValidator= custModifyValidator;
}
public ActionResult Create (Customer customerToCreate)
{
var results = this.custCreateValidator.Validate (customerToCreate);
...
}
public ActionResult Modify (Customer customerToModify)
{
var results = this.custModifyValidator.Validate (customerToModify);
...
}
}
Thanks a lot for your time and help!
Upvotes: 1
Views: 369