加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 具有Repository,Service Layer和使用Model Binde

发布时间:2020-12-16 03:18:16 所属栏目:asp.Net 来源:网络整理
导读:相关: What’s the best way to implement field validation using ASP.NET MVC? 让我们假设一个具有以下项目的解决方案: Foo; // the MVC web projectFoo.Models;Foo.Repositories;Foo.Services; Foo.Models是所有实体的应用程序域,无论使用EF,NH,POCO还
相关: What’s the best way to implement field validation using ASP.NET MVC?

让我们假设一个具有以下项目的解决方案:

Foo; // the MVC web project
Foo.Models;
Foo.Repositories;
Foo.Services;

Foo.Models是所有实体的应用程序域,无论使用EF,NH,POCO还是其他什么都无关紧要.这是一个例子:

public class User
{
    public string Username { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }
}

在Foo.Repositories中有一个UserRepository,在Foo.Services中有一个UserService.

在Web应用程序中,让我们考虑如下的模型绑定器:

public class UserBinder : DefaultModelBinder
{
    //...
}

我在验证的位置上看到了三种不同的选项:

>在Foo.Models中如下:

public class User
{
    public string Username { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }

    public ICollection<KeyValuePair<string,string>> ValidateErrors()
    {
        //Validate if Username,Email and Password has been passed
    }
}

>在Foo.Services喜欢:

public class UserService
{
    public ICollection<KeyValuePair<string,Email and Password has been passed
    }
}

>在模型绑定器中的Foo中:

public class UserBinder : DefaultModelBinder
{
    protected override void OnModelUpdated(ControllerContext controllerContext,ModelBindingContext bindingContext)
    {
        var user = (User)bindingContext.Model;

        // validate everything here

        base.OnModelUpdated(controllerContext,bindingContext);
    }
}

需要注意的另一件事是考虑前两个选项[模型和服务]还有另一个决定:ValidateErrors方法可以直接在控制器上或在Binder内部调用.

我对这个场景有2个问题:

>验证应该是:

>在从控制器调用的模型中?
>在从活页夹调用的模型中?
>在从控制器调用的服务中?
>在从活页夹调用的服务中?
>直接在活页夹?
>还有其他想法吗?

>以上所有场景都讨论了用户创建.但是用户登录呢?
假设用户使用用户名和密码登录应用程序,因此不需要验证电子邮件.
这个验证应该在哪里?

>在从控制器调用的模型中?
>在从控制器调用的服务中?
>还有其他想法吗?

解决方法

看看 ASP.NET MVC Contact Manager Sample Application它有一个非常好的建筑我的意见

http://www.asp.net/learn/mvc/tutorial-26-cs.aspx’u0026gt;http://www.asp.net/learn/mvc/tutorial-26-cs.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读