【问题标题】:Count calls to method with Dependency Injection使用依赖注入计算对方法的调用
【发布时间】:2022-01-02 18:20:43
【问题描述】:

我正在尝试学习依赖注入。我正在使用 .net 6.0 Razor Pages 并且我想“计数”,即通过依赖注入调用方法的次数。 我的索引如下:

public class IndexModel : PageModel
{
    public ICounter _counter;

    public IndexModel(ICounter counter)
    {
        _counter = counter;
    }
    public int Count { get; set; }
    public void OnPostInfo()
    {
        Count = _counter.Calls(Count);
    }
}

我的“计数器”类如下

public int Number { get; set; }
public int Calls(int Number)
{
    return Number += 1 ;
}

还有我的界面

public interface ICounter
{
   int Calls(int Number);     
}

我的 Program.cs 类

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddSingleton<ICounter, Counter>();

我的观点。

<div class="card">
    <div class="card-body">
        @Model.Count
    </div>
</div>
<div class="row">
    <form asp-page-handler="Info" method="post">
        <button class="btn btn-default">Push to count</button>
    </form>
</div>

它在 Program.cs 中作为瞬态添加。该网站是通过一个按钮更新的,并且“有效”,但它只更新到 1。但随后它停止了。当我按下使用 OnPostInfo 方法的按钮时,它不会继续“2、3、4”等。

我在这里错过了什么?

【问题讨论】:

  • 了解不同的生命周期(transientscopedsingleton)。注册为 transient 的依赖项在每次访问时都会重新创建,因此每次都会重置计数器。
  • @nbokmans 尝试了所有三种不同的方法。结果还是一样。
  • @Mr.Jones - 瞬态、作用域和单例与同一个请求相关,我相信您每次尝试计数时都有不同的回发请求。
  • 您可以使用单例并在启动时获取它将创建对象的服务。
  • 每个对象和每个请求的单例应该是相同的。如果单例无法向我们展示您如何注入/连接您的 di 容器。

标签: c# asp.net-core dependency-injection razor-pages


【解决方案1】:

其实不是生命周期造成的。这是由于Count的值没有传递给后端,所以后端总是会得到Count的int默认值0

您需要设置一个隐藏的输入或路由参数来传递Count

设置隐藏输入的第一种方式:

查看:

<div class="card">
    <div class="card-body">
        @Model.Count
    </div>
</div>
<div class="row">
    <form asp-page-handler="Info" method="post">
        <input hidden asp-for="Count" value="@Model.Count" /> <!--set the hidden input-->
        <button class="btn btn-default">Push to count</button>
    </form>
</div>

页面模型:

public class IndexModel : PageModel
{
    public ICounter _counter;

    public IndexModel(ICounter counter)
    {

        _counter = counter;
    }
    [BindProperty]  //add this attribute to get the Count value
    public int Count { get; set; }
    public void OnPostInfo()
    {
        Count = _counter.Calls(Count);
    }
}

设置路由参数的第二种方式:

查看:

<div class="card">
    <div class="card-body">
        @Model.Count
    </div>
</div>
<div class="row">                  <!--add asp-route-countNum -->
    <form asp-page-handler="Info" asp-route-countNum="@Model.Count" method="post">
        <button class="btn btn-default">Push to count</button>
    </form>
</div>

页面模型:

public class IndexModel : PageModel
{
    public ICounter _counter;

    public IndexModel(ICounter counter)
    {

        _counter = counter;
    }

    public int Count { get; set; }
    public void OnPostInfo(int countNum)   //add parameter here
    {
        Count = _counter.Calls(countNum);   //change here... 
    }
}

结果:

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多