ASP.NET Core 中使用 IActionResult 的 C# 接口(interface)概念说明

标签 c# asp.net-core oop

我试图理解 C#,学习了基础知识并开始尝试使用 ASP.NET Core,但无法理解接口(interface)部分。

我理解接口(interface)就像是类应该包含什么的契约(Contract),接口(interface)方法没有主体,例如:

interface IAnimal 
{
    void animalSound(); 
}

然后我们在类中提供实现,如下所示:

class Pig : IAnimal 
{
    public void animalSound() 
    {
        Console.WriteLine("The pig says: wee wee");
    }
}

但是,当我进入现实世界(像往常一样)时,情况就不同了:)

在 ASP.NET Core 中,我们可以使用 IActionResult 接口(interface)来帮助我们进行响应 - 返回 Ok()NotFound()

就像这个例子

namespace CityInfo1.API.Controllers
{
    [ApiController]
    [Route("api/cities")]
    public class CitiesControllers : ControllerBase
    {
        [HttpGet]
        public IActionResult GetCities()
        {
            return Ok(CitiesDataStore.Current.Cities);
        }

        [HttpGet("{id}")]
        public IActionResult GetCity(int id)
        {
            var cityToReturn = CitiesDataStore.Current.Cities
                .FirstOrDefault(c => c.Id == id);

            if (cityToReturn == null)
            {
                return NotFound();
            }

            return Ok(cityToReturn);
        }
    }
}

但在这里,IActionResult实际上是在提供不同方法的功能和实现。

另外一个问题,在IAnimal的例子中,是否必须在使用这个接口(interface)的类中使用AnimalSound()?如果是这样,是否可以将其设为可选?

最佳答案

IActionResult声明如下:

public interface IActionResult
{
    Task ExecuteResultAsync(ActionContext context);
}

非常类似于您的IAnimal,不是吗?

还有一个类叫OkObjectResult实现 IActionResult,它具有此 ExecuteResultAsync 方法。

public class OkObjectResult : IActionResult {
    public Task ExecuteResultAsync(ActionContext context)
    {
        // ...
    }
}

OkObjectResult 实际上并没有直接实现 IActionResult。它继承自ObjectResult,后者继承自ActionResult,后者实现了IActionResult。该代码仅用于说明目的。

这对应于非真实示例中的 Pig 类。

现在您的 CitiesController 代码对应于您的非现实示例的哪一部分?好吧,您在非真实世界的示例中错过了那部分。如果我们继续它,我们可以说有一个 AnimalFactory 类产生 IAnimal:

public class AnimalFactory {
    public IAnimal GetAnimal() {
        return new Pig();
    }
}

为了论证,假设 AnimalFactory 的用户不关心他们得到的是哪种动物,只要它是 IAnimal - 即具有 animalSounds 方法的东西。作为实现者,我们可以自由决定返回哪种动物,在这种情况下,我决定始终返回一头 pig 。

回到您的 CitiesController,您应该在 GetCity 中返回一个 IActionResult,但调用者不关心具体是哪一种IActionResult,只要它是一个 IActionResult - 就有 ExecuteResultAsync(ActionContext) 方法。您已决定返回 Ok(这是 CitiesControllerBaseController 继承的方法)返回的任何内容。仅供引用,OK 返回 OkObjectResult 的实例。现在你知道为什么我一开始就提到这个吧!

is it mandatory to declare the AnimalSound() method in the classes that implement the interface?

是的,因为通过实现接口(interface),您是说该类具有接口(interface)所需的所有成员。声明您的类实现一个接口(interface),然后不提供所需的成员,这只是说谎,不是吗? :D

关于ASP.NET Core 中使用 IActionResult 的 C# 接口(interface)概念说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67444114/

相关文章:

c# - 我最近开始自己学习 WPF。声明 Name 与 x :Name? 时有什么区别

azure - 从 Asp.Net Core 中的 URL 中删除附加斜杠

java - 在这种情况下我应该如何处理异常

php - 关键字 'finally' 如何在 PHP 中使用?

oop - 有没有办法将 Trait 实现和防御拆分到不同的模块中?

c# - 转换日期表示不支持

c# - 将可枚举复制到列表? "Cannot implicitly convert type ' MyClass.Items' 到 'System.Collections.Generic.List<MyClass.Items>' "

c# - 将 IEnumerable<string> 作为用户定义的表类型参数传递给 SQL Server 存储过程

self-hosting - 自托管 vnext 应用程序

c# - System.Text.Json:使用自动转换反序列化 JSON