简体   繁体   English

从单独项目中的验证类填充MVC视图模型

[英]Populating MVC view model from validation class in separate project

Here is my situation. 这是我的情况。 My solution structure is as follows. 我的解决方案结构如下。

Project Used to handle routes, displaying data, ... Project用于处理路线,显示数据,...

Project.Core Used to handle business logic, validation, querying, ... Project.Core用于处理业务逻辑,验证,查询,...

In Project.Core I have a validation class that validates my DTO (Data Transfer Object). Project.Core我有一个验证类来验证我的DTO(数据传输对象)。

My validation class (in Project.Core ): 我的验证类(在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 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 ): 我的视图模型(在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? 所以我的问题是, Project.Core是否应该引用Project来访问我的视图模型,以便我的验证类可以填充它? 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? 也许我的视图模型可以存在于Project.Core还是被认为是不好的设计?

What can I do? 我能做什么?

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 . 为每个视图模型类型创建一个位于Project.Core的接口,并让实际的视图模型实现这些接口并位于Project That way, you'll be able to use the stuff you need for validation in Project.Core without caring about implementation. 这样,您就可以在Project.Core使用验证所需的内容,而无需关心实现。

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). 另一种可能性是将视图模型外部化为一个单独的程序集,供您在Project.Core引用(尽管第一种方法似乎更好)。 You shouldn't reference Project in Project.Core in any case. 无论如何,您都不应在Project.Core中引用Project

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. 验证工厂也许可以包含一个验证器接口,但是验证器逻辑与模型一起驻留在Project中。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM