Here is my situation. My solution structure is as follows.
Project
Used to handle routes, displaying data, ...
Project.Core
Used to handle business logic, validation, querying, ...
In Project.Core
I have a validation class that validates my DTO (Data Transfer Object).
My validation class (in Project.Core
):
public class IncidentValidator<T> : IValidator<T>
where T : AuditReport
{
public IncidentValidator(IList<T> ar)
{
AR = ar;
}
public IList<T> AR { get; set; }
public IList<Model> Validate()
{
var开发者_如何学Go list = new List<Model>();
foreach (T ar in AR)
{
list.Add(new Model
{
IncidentId = new KeyValuePair<int, RuleType>(
ar.IncidentId,
new OccurrenceCountRule(ar).RulesValidate()
),
Circuit = new KeyValuePair<string, RuleType>(
ar.Circuit,
new CircuitRule(ar).RulesValidate()
)
});
}
return list;
}
}
My view model (in Project
):
public class Model
{
public KeyValuePair<int, RuleType> IncidentId { get; set; }
public KeyValuePair<string, RuleType> Circuit { get; set; }
}
So my question is, should Project.Core
reference Project
to have access to my view models so my validation class can populate it? I don't really like that approach however. I've thought about doing the validation inside my controller but don't like that idea either. Perhaps my view model can live inside Project.Core
or is that considered bad design?
What can I do?
If this validator class is intended to validate view models, then you should put it in the same project as the one containing your view models. Another possibility is to externalize your view models into a separate assembly which you reference in Project.Core
(the first approach seems better though). You shouldn't reference Project
in Project.Core
in any case.
Create an interface for each view model type, that resides in Project.Core
, and let the actual view models implement the interfaces and reside in Project
. That way, you'll be able to use the stuff you need for validation in Project.Core
without caring about implementation.
I'd say do it in the controller, create a component that manages the validation process (the framework validates in the controller right now anyway) so the controller doesn't have to do a lot of work, except for delegate to another process. Additionally, interfaces could work, or you could utilize another design pattern for the validation. Maybe the validation factory can contain a validator interface, but the validator logic resides in Project with the models.
HTH.
精彩评论