自定义验证的用户名重复在DB [英] Custom Validation for Duplicate UserName in DB

查看:96
本文介绍了自定义验证的用户名重复在DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有更好的方法来处理自定义验证,请让我知道。我不想为这个服务层吧。

阅读下面我想要的第五选项。

  1  -  IUserRepository  - >布尔IsUserRegistered(字符串用户名);

2 - UserRepository与法

 只读EFDBContainer _db =新EFDBContainer();
公共BOOL IsUserRegistered(用户名字符串)
{
    返回_db.Users.Any(D => d.UserName ==用户名);
}

3 - Ninject - > UserController的是DI

 公共静态无效RegisterServices(内核的iKernel)
{
    kernel.Bind&所述; IUserRepository方式>()到< UserRepositary>();
}

4 - UserController的

 私人只读IUserRepository _repository;
公共ProfileController可(IUserRepository库)
{
    _repository =库;
}

在控制器创建方法

  HttpPost]
公众的ActionResult创建(字符串confirmButton,用户用户)
{
    如果(ModelState.IsValid)
    {
        尝试
        {
            _repository.Create(用户); - >这调用Create方法如下在此之前EnsureValid被调用
            返回//完成重定向        }
        赶上(RuleViolationException)
        {
            this.UpdateModelStateWithViolations(用户,ViewData.ModelState);
        }
    }
    返回//查看;
}

从存储库中创建方法

 公共无效创建(用户用户)
{
    user.EnsureValid(); - >进入用户对象,并做验证
    //添加对象数据库
}

5 - 我想要什么:

在这里我要DI,这样我可以调用用户对象上一日IsUserRegistered接口方法
IsUserRegistered下面是不是现在的工作。我需要一种方法来使用接口

 公共部分类用户:IRuleEntity
{
    公开名单< RuleViolation> GetRuleViolations()
    {
        清单< RuleViolation> validationIssues =新的List< RuleViolation>();        如果(IsUserRegistered(用户名))
            validationIssues.Add(新RuleViolation(用户名,用户名用​​户名已存在请输入不同的用户名。));        返回validationIssues;
    }    公共无效EnsureValid()
    {
        清单< RuleViolation>问题= GetRuleViolations();        如果(issues.Count!= 0)
            抛出新RuleViolationException(企业违规的问题);
    }
}


解决方案

写自己的验证属性,并将其添加到用户名。

请参阅的http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/.它说明了如何把依赖注入的验证。

另请参阅随附Ninject MVC扩展它拥有具有相关性的验证程序的示例,示例应用程序。 https://github.com/ninject/ninject.web.mvc

If you have better approach to handle custom Validation please let me know. I don't want service layer for this please.

Read below 5th option what I want.

I have

1 - IUserRepository -> bool IsUserRegistered(string userName);

2 - UserRepository with Method

readonly EFDBContainer _db = new EFDBContainer();
public bool IsUserRegistered(string userName)
{
    return _db.Users.Any(d => d.UserName == userName);
}

3 - Ninject --> UserController is DI

public static void RegisterServices(IKernel kernel) 
{
    kernel.Bind<IUserRepository>().To<UserRepositary>();
}

4 - UserController

private readonly IUserRepository _repository;
public ProfileController(IUserRepository repository)
{
    _repository = repository;
}

Create Method on Controller

HttpPost]
public ActionResult Create(string confirmButton, User user)
{
    if (ModelState.IsValid)
    {
        try
        {
            _repository.Create(user); --> This calling Create Method below before this EnsureValid is Called
            return //Do Redirection

        }
        catch (RuleViolationException)
        {
            this.UpdateModelStateWithViolations(user, ViewData.ModelState);
        }
    }
    return //to View;
}

Create Method from Repository

public void Create(User user)
{
    user.EnsureValid(); --> Go to User object and do validation
    //Add object to DB
}

5 - What I want:

Here I want DI so that I can call 1st IsUserRegistered interface method on User object IsUserRegistered below is not working right now. I need a way to use the Interface

public partial class User: IRuleEntity
{
    public List<RuleViolation> GetRuleViolations()
    {
        List<RuleViolation> validationIssues = new List<RuleViolation>();

        if (IsUserRegistered(userName))
            validationIssues.Add(new RuleViolation("UserName", UserName, "Username already exists. Please enter a different user name."));

        return validationIssues;
    }

    public void EnsureValid()
    {
        List<RuleViolation> issues = GetRuleViolations();

        if (issues.Count != 0)
            throw new RuleViolationException("Business Rule Violations", issues);
    }
}

解决方案

Write your own validation attribute and add it to the user name.

See http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/. It explains how to inject dependencies into validators.

See also the sample application that comes with the Ninject MVC extension it has an example of a validator that has a dependency. https://github.com/ninject/ninject.web.mvc

这篇关于自定义验证的用户名重复在DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆