Count calls to method with Dependency Injection

Mr.Jones

I'm trying to learn about dependency injection. I'm using .net 6.0 Razor Pages and I want to "count", the number of times a method is called via dependency injection. My Index is as following:

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);
    }
}

My "counter" class, is as following

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

And my interface

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

My Program.cs class

var builder = WebApplication.CreateBuilder(args);

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

My View.

<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>

It's added as an Transient in Program.cs. The site is updated via a button, and that "works", but it only updates to 1. But then it stops. It doesn't continue to "2, 3, 4" etc when i push the button that uses the OnPostInfo method.

What am i missing here?

Rena

Actually it is not caused by lifetime. It dues to the Count value is not passed to the backend, so backend will always get the int default value 0 for Count.

You need set a hidden input or route parameter to pass the Count.

First way to set a hidden input:

View:

<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>

PageModel:

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);
    }
}

The second way to set a route parameter:

View:

<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>

PageModel:

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... 
    }
}

Result:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dependency Injection in HtmlHelper extension method?

From Dev

.net core method dependency injection

From Java

Count concurrent calls on a method

From Dev

Failed Laravel dependency injection in controller method

From Java

Java: method-chaining and dependency injection

From Dev

Pass Dependency Injection Container to static method

From Dev

Dependency Injection into Entity Framework seed method?

From Dev

dependency injection in factory method causes NullPointerException

From Dev

Is there a way to have Dependency Injection and an Extension method interact?

From Dev

Mock method on the same class with dependency Injection - PHP

From Dev

Is there a way to use static method with dependency injection in NestJS?

From Dev

Dependency injection to class-method in ABAP

From Dev

Handle dependency injection with factory method DP

From Dev

Expose private method when doing dependency injection

From Dev

Dependency Injection in EF-core OnModelCreating method

From Dev

Webjobs Method Indexing - Dependency Injection Exception IConfiguration

From Java

How to count method calls by instance

From Java

Count indirect method calls Mockito

From

Reader Monad for Dependency Injection: multiple dependencies, nested calls

From Dev

Can modern compilers devirtualize function calls when using dependency injection

From Dev

Java - Count Method Calls Inside A Method

From Dev

How dependency injection using annotation works internally whether it calls to setter methods or constructor based injection

From Dev

Dependency Injection

From Dev

Dependency Injection in Angular/ Calling a method that belongs to a service from a class

From Dev

Can Dependency Injection be considered to be a replacement for factory method pattern?

From Java

How Dependency Injection by name works in Spring @Bean method parameters

From Java

Dependency Injection leads to main method being filled with new keywords

From Dev

Factory method and Dependency injection, Some services are not able to be constructed

From Dev

Symfony 4 Service Dependency Injection - Constructor vs Method

Related Related

  1. 1

    Dependency Injection in HtmlHelper extension method?

  2. 2

    .net core method dependency injection

  3. 3

    Count concurrent calls on a method

  4. 4

    Failed Laravel dependency injection in controller method

  5. 5

    Java: method-chaining and dependency injection

  6. 6

    Pass Dependency Injection Container to static method

  7. 7

    Dependency Injection into Entity Framework seed method?

  8. 8

    dependency injection in factory method causes NullPointerException

  9. 9

    Is there a way to have Dependency Injection and an Extension method interact?

  10. 10

    Mock method on the same class with dependency Injection - PHP

  11. 11

    Is there a way to use static method with dependency injection in NestJS?

  12. 12

    Dependency injection to class-method in ABAP

  13. 13

    Handle dependency injection with factory method DP

  14. 14

    Expose private method when doing dependency injection

  15. 15

    Dependency Injection in EF-core OnModelCreating method

  16. 16

    Webjobs Method Indexing - Dependency Injection Exception IConfiguration

  17. 17

    How to count method calls by instance

  18. 18

    Count indirect method calls Mockito

  19. 19

    Reader Monad for Dependency Injection: multiple dependencies, nested calls

  20. 20

    Can modern compilers devirtualize function calls when using dependency injection

  21. 21

    Java - Count Method Calls Inside A Method

  22. 22

    How dependency injection using annotation works internally whether it calls to setter methods or constructor based injection

  23. 23

    Dependency Injection

  24. 24

    Dependency Injection in Angular/ Calling a method that belongs to a service from a class

  25. 25

    Can Dependency Injection be considered to be a replacement for factory method pattern?

  26. 26

    How Dependency Injection by name works in Spring @Bean method parameters

  27. 27

    Dependency Injection leads to main method being filled with new keywords

  28. 28

    Factory method and Dependency injection, Some services are not able to be constructed

  29. 29

    Symfony 4 Service Dependency Injection - Constructor vs Method

HotTag

Archive