I’m working in a content development scenario where entities are likely to be created in an incomplete state and remain incomplete for some time. The workflow involved is extremely ad-hoc, preventing me from using a more structured DDD approach.
I have a set of validation rules which must be satisfied at all times, and a further set of validation rules which must be satisfied before an entity is “complete”.
I’m already using the built-in ASP.NET MVC validation to validate the former. What approaches could I use to capture the latter?
For example:-
public class Foo
{
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
[Required] // A Bar must be owned by a Foo at all times
public int FooId { get; set; }
public virtual Foo Foo { get; set; }
[Required] // A Bar must have a working title at all times
public string WorkingTitle { get; set; }
public bool IsComplete { get; set; }
// Cannot use RequiredAttribute on Description as the
// entity is very likely to be created without one,
// however a Description is required before the entity
// can be marked as "IsComplete"
public string Description { get; set; }
}
There are different approaches you could use:
IValidatableObjectinterface and perform conditional validation without using data annotations.Descriptionproperty based on the value of theIsCompleteproperty.RequiredIfvalidation attribute defined for you so that you don’t need to write it yourself.Personally I would go with the FV.NET but you could use any of the other approaches if it better suits your needs.