Databases Reference
In-Depth Information
public interface IDynamicValidatorException
{
IDictionary<string, Exception> InnerExceptions { get; }
}
The built-in
EntityDataSource
control takes advantage of this interface when reporting
errors that occur when it tries to assign field values received from the form controls to the
properties of entity objects. The Entity Framework implements its own, limited, validation
based on the rules embedded directly in the entity data model (EDMX), such as prevent-
ing assignment of a
null
value to an entity property that does not allow it. When these
errors are detected, the
EntityDataSource
control collects all errors for a given entity
object and throws the
EntityDataSourceValidationException
, which implements the
IDynamicValidatorException
interface. In the presentation layer, the
DynamicValidator
controls check all exceptions thrown by the data source control, and if a particular excep-
tion implements the
IDynamicValidatorException
interface, the validator controls use its
InnerExceptions
dictionary to find exceptions that apply to the entity properties they
validate.
Unfortunately, the
IDynamicValidatorException
is defined in the
System.Web.Extensions
assembly and where the
EntityDataSourceValidationException
is defined in the
System.Web.Entity
. This is not a problem for web applications; however, for other types
of applications, using these types means taking a dependency on ASP.NET, which is only
included in the full version of the .NET framework and
not
in the client profile.
Modifying ObjectContext to Report Multiple Errors
To avoid adding a dependency on the presentation framework to the business layer of the
application, you can use another built-in exception type
- AggregateException
. The .NET
Framework version 4 introduced this class in the
System
namespace to support reporting
of multiple errors in the Parallel Task Library (TPL).
AggregateException
also defines a
property called
InnerExceptions
, although it stores a
ReadOnlyCollection
of Exception
objects instead of a
Dictionary
. Here is how declaration of this class looks in simplified
form:
public class AggregateException : Exception
{
public ReadOnlyCollection<Exception> InnerExceptions { get; }
}
Here is a new version of the
Validate
method of the
UnleashedObjectContext
class that
reports multiple validation errors in a single
AggregateException
:
private void Validate(ObjectStateEntry stateEntry)
{
object entity = stateEntry.Entity;
var context = new ValidationContext(entity, null, null);
var results = new List<ValidationResult>();