I have the following working system and looking for ways to make it DRY:
public class EMailMetaData
{
[Display(Prompt="myemail@mydomain.com"])
public string Data;
}
public class PhoneMetaData
{
[Display(Prompt="+1 (123) 456-7890"])
public string Data;
}
public class AddressMetaData
{
[Display(Prompt="Central st. W., St Francisco, USA"])
public string Data;
}
// 7 more metadata templates
public class ContactVM
{
[Required]
public string DataLabel { get; set; }
[Required(ErrorMessage="Please fill in the data field")]
public string Data { get; set; }
}
[MetadataType(typeof(EmailMetaData))]
EmailVM : ContactVM
{
}
[MetadataType(typeof(PhoneMetaData))]
PhoneVM : ContactVM
{
}
[MetadataType(typeof(AddressMetaData))]
AddressVM : ContactVM
{
}
// 7 more contact view models
Controller is obviously initializes them with right content and in the view I run over the foreach loop of ContactVMs having TemplateEditor for every one of contacts: EmailVM.cshtml, PhoneVM.cshtml, AddressVM.cshtml UrlVM.cshtml etc.
The main view looks (omitting all the setup and details like this:
@model ContactsVM
foreach (var contact in Model.Contacts)
{
@Html.EditorFor(m => contact)
}
and under EditorTemplates
@model EmailVM
@Html.EditorFor(model => model.DataLabel)
@Html.EditorFor(model => model.Data)
<br />
@Html.ValidationMessageFor(model => model.DataLabel)
@Html.ValidationMessageFor(model => model.Data)
… and obviously few more editor templates for every view model I have defined.
So in simple words – very similar contact types with minor differences in watermarking, naming, validation, but essentially all strings and all have the same fields (address is one long string rather than struct, same for all of them).
My question is not specific for watermarking, it can be any of properties – name, description, prompt etc.
[Display(Name="name", Description="description", Prompt="prompt")]
Its all pretty much working and showing right labels, watermarks for each, but it seems like huge DRY violation since all the template editors are exactly the same except the model type. What I show is here simplification to concentrate on the problem at hand, main view and editor templates are significantly more complicated that what you see here, so duplication is tremendous.
Can any of you suggest better way of making it not duplicating so much code please?
Thanks!
To answer my own question:
It is blunt solution but much simpler than those suggested and most important requires least duplication compared to straightforward solution (although not too elegant considering we’re in object oriented world):
So Contact.cshtml will look like this:
Calling Contact.cshtml editor template will go in the following manner (thanks Serg):
The partial view that is dedicated to only show the right data annotations _ContactDataAnnotation.cshtml will look like this:
By this the duplication will only exist in places that is required, rather than having the same editor template duplicated 10 times with different names and model classes.
I know it contradicts the title of the question “best practices”, but unfortunately this is the easiest and most minimalistic way to go. Other solutions, as Serg pointed out, are way too complex and require deep MVC infrastructure intervention, which I don’t like spending time on, as well as having prompts and tooltips injected rather than defined in data annotation, which appears to be a standard way of decorating and validating your models.
I consider my solution to be a workaround MVC limitation of lacking polymorphism for data annotations.