关于服务层作为asp.net mvc验证的问题

时间:2021-05-17 16:35:28

I am a bit confused about the service layer and using it validation.

我对服务层有点困惑并使用它验证。

So I am looking through this tutorial: http://www.asp.net/learn/mvc/tutorial-38-cs.aspx

所以我正在浏览本教程:http://www.asp.net/learn/mvc/tutorial-38-cs.aspx

First if you look at List 3

首先,如果你看一下清单3

using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcApplication1.Models
{
    public class ProductService : MvcApplication1.Models.IProductService
    {

        private ModelStateDictionary _modelState;
        private IProductRepository _repository;

        public ProductService(ModelStateDictionary modelState, IProductRepository repository)
        {
            _modelState = modelState;
            _repository = repository;
        }

        protected bool ValidateProduct(Product productToValidate)
        {
            if (productToValidate.Name.Trim().Length == 0)
                _modelState.AddModelError("Name", "Name is required.");
            if (productToValidate.Description.Trim().Length == 0)
                _modelState.AddModelError("Description", "Description is required.");
            if (productToValidate.UnitsInStock < 0)
                _modelState.AddModelError("UnitsInStock", "Units in stock cannot be less than zero.");
            return _modelState.IsValid;
        }

        public IEnumerable<Product> ListProducts()
        {
            return _repository.ListProducts();
        }

        public bool CreateProduct(Product productToCreate)
        {
            // Validation logic
            if (!ValidateProduct(productToCreate))
                return false;

            // Database logic
            try
            {
                _repository.CreateProduct(productToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }


    }

    public interface IProductService
    {
        bool CreateProduct(Product productToCreate);
        IEnumerable<Product> ListProducts();
    }
}

They same interface just with a different name basically why not just use one?

他们只是使用不同的名称相同的界面为什么不只是使用一个?

    public interface IProductRepository
    {
        bool CreateProduct(Product productToCreate);
        IEnumerable<Product> ListProducts();
    }

   public interface IProductService
    {
        bool CreateProduct(Product productToCreate);
        IEnumerable<Product> ListProducts();
    }

In my book though(the author who I think wrote this tutorial) has changed it to have IProductRepository to void. So that confuses me even more.

在我的书中(我认为编写本教程的作者)已将其更改为使IProductRepository无效。所以这让我更加困惑。

So can someone explain why I need 2 interfaces that seems to do the same thing?

那么有人可以解释为什么我需要2个似乎做同样事情的接口吗?

My next questions is my repository has a delete function. Do I put this one in my Service layer too(I guess mandatory if you use one Interface but if you use 2 like about then it could be optinal).

我的下一个问题是我的存储库有一个删除功能。我是否也把这个放在我的服务层中(如果你使用一个接口,我认为是强制性的,但如果你使用2,那么它可能是optinal)。

So what would I have in my service layer? Would it just call delete function in the repository? Should it just be a void method or should it return bool? I don't think for this method any validation would need to be done?

那么我的服务层会有什么?它只是调用存储库中的删除功能吗?它应该只是一种无效方法还是应该返回bool?我不认为这种方法需要进行任何验证吗?

So I am not sure if a bool would be needed.

所以我不确定是否需要布尔。

2 个解决方案

#1


1  

From the tutorial you are reading:

从您正在阅读的教程中:

So, application flow control logic belongs in a controller and data access logic belongs in a repository. In that case, where do you put your validation logic? One option is to place your validation logic in a service layer.

因此,应用程序流控制逻辑属于控制器,数据访问逻辑属于存储库。在这种情况下,您在哪里放置验证逻辑?一种选择是将验证逻辑放在服务层中。

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic.

服务层是ASP.NET MVC应用程序中的附加层,用于调解控制器和存储库层之间的通信。服务层包含业务逻辑。特别是,它包含验证逻辑。

EDIT:

I'm not sure if I can explain it to you in a clear way('cause I'm not fluent in English), but I will try:

我不确定我能否以清晰的方式向你解释(因为我的英语不流利),但我会尝试:

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer, in that you can handle both validation and application businness. Sometimes you service will need to work with two or more methods of its correspondent repository layer so it doesnt need to have the same interface.

服务层是ASP.NET MVC应用程序中的一个附加层,它调解控制器和存储库层之间的通信,因为您可以处理验证和应用程序业务。有时您的服务需要使用其对应的存储库层的两个或更多方法,因此它不需要具有相同的接口。

A basic example, let's think you have a register form.

一个基本的例子,我们认为你有一个注册表。

you will have the following interfaces

您将拥有以下界面

public interface IUserService

{
    bool Register(User mUser);
    bool Validate(User mUser);
}

public interface IUserRepository
{
    User FindUserByEmail(string Email);
    bool Insert(User mUser);
}

so you will end up with two class that will do something like: public class UserRepository: IUserRepository{

所以你最终会得到两个类,它们会做类似的事情:public class UserRepository:IUserRepository {

    User FindUserByEmail(string Email)
{
    //do a ninja search and return an user or null
}
    bool Insert(User mUser);
    {
        //Insert user into db
    }   
}

public class UserService: IUserService
{
    public bool Validate(User mUser)
    {
        //validate user
    }
    IUserRepository _respository = new UserRepository();
    bool Register(User mUser)
    {
        if(Validate(mUser);
        var hasUser = _respository.FindUserByEmail(User.Email);
        if(hasUser==null)
            return _respository.Insert(mUser);
        return false;
    }   
}

#2


0  

I think you've made an argument for a single interface in this limited case, but the service and repositories perform two very different functions and you may run into issues down the road if they shared a single interface.

我认为你在这种有限的情况下为单个接口做了一个论证,但是服务和存储库执行两个非常不同的功能,如果他们共享一个接口,你可能会遇到问题。

What if the CreateProduct() or ListProducts() needed to have different method signatures in either the service or repository?

如果CreateProduct()或ListProducts()需要在服务或存储库中具有不同的方法签名,该怎么办?

What if ValidateProduct() should be defined in the interface? The repository certainly shouldn't have to implement that.

如果应该在接口中定义ValidateProduct()怎么办?存储库当然不应该实现它。

As you've pointed out, there's no need for two interfaces that define the same thing in this particular example, but I assume the author's assumption is that down the road they would be different and therefore necessary.

正如你所指出的那样,在这个特定的例子中不需要两个接口来定义相同的东西,但是我认为作者的假设是在路上它们会有所不同,因此是必要的。

#1


1  

From the tutorial you are reading:

从您正在阅读的教程中:

So, application flow control logic belongs in a controller and data access logic belongs in a repository. In that case, where do you put your validation logic? One option is to place your validation logic in a service layer.

因此,应用程序流控制逻辑属于控制器,数据访问逻辑属于存储库。在这种情况下,您在哪里放置验证逻辑?一种选择是将验证逻辑放在服务层中。

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic.

服务层是ASP.NET MVC应用程序中的附加层,用于调解控制器和存储库层之间的通信。服务层包含业务逻辑。特别是,它包含验证逻辑。

EDIT:

I'm not sure if I can explain it to you in a clear way('cause I'm not fluent in English), but I will try:

我不确定我能否以清晰的方式向你解释(因为我的英语不流利),但我会尝试:

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer, in that you can handle both validation and application businness. Sometimes you service will need to work with two or more methods of its correspondent repository layer so it doesnt need to have the same interface.

服务层是ASP.NET MVC应用程序中的一个附加层,它调解控制器和存储库层之间的通信,因为您可以处理验证和应用程序业务。有时您的服务需要使用其对应的存储库层的两个或更多方法,因此它不需要具有相同的接口。

A basic example, let's think you have a register form.

一个基本的例子,我们认为你有一个注册表。

you will have the following interfaces

您将拥有以下界面

public interface IUserService

{
    bool Register(User mUser);
    bool Validate(User mUser);
}

public interface IUserRepository
{
    User FindUserByEmail(string Email);
    bool Insert(User mUser);
}

so you will end up with two class that will do something like: public class UserRepository: IUserRepository{

所以你最终会得到两个类,它们会做类似的事情:public class UserRepository:IUserRepository {

    User FindUserByEmail(string Email)
{
    //do a ninja search and return an user or null
}
    bool Insert(User mUser);
    {
        //Insert user into db
    }   
}

public class UserService: IUserService
{
    public bool Validate(User mUser)
    {
        //validate user
    }
    IUserRepository _respository = new UserRepository();
    bool Register(User mUser)
    {
        if(Validate(mUser);
        var hasUser = _respository.FindUserByEmail(User.Email);
        if(hasUser==null)
            return _respository.Insert(mUser);
        return false;
    }   
}

#2


0  

I think you've made an argument for a single interface in this limited case, but the service and repositories perform two very different functions and you may run into issues down the road if they shared a single interface.

我认为你在这种有限的情况下为单个接口做了一个论证,但是服务和存储库执行两个非常不同的功能,如果他们共享一个接口,你可能会遇到问题。

What if the CreateProduct() or ListProducts() needed to have different method signatures in either the service or repository?

如果CreateProduct()或ListProducts()需要在服务或存储库中具有不同的方法签名,该怎么办?

What if ValidateProduct() should be defined in the interface? The repository certainly shouldn't have to implement that.

如果应该在接口中定义ValidateProduct()怎么办?存储库当然不应该实现它。

As you've pointed out, there's no need for two interfaces that define the same thing in this particular example, but I assume the author's assumption is that down the road they would be different and therefore necessary.

正如你所指出的那样,在这个特定的例子中不需要两个接口来定义相同的东西,但是我认为作者的假设是在路上它们会有所不同,因此是必要的。