jspuij / Cortex.Net

State management like MobX for .NET and Blazor

Home Page:https://jspuij.github.io/Cortex.Net.Docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Great project

johnhamm opened this issue · comments

I'm a huge fan of mobx, so finding this library for Blazor makes me happy.

I'm new to Cortex.net and trying to understand why Computed seems to be recomputing a property when none of the data used in the previous computation has changed. Am I missing something?

using Cortex.Net.Api;

var test = new Person { FirstName = "John", LastName = "Doe" };
Console.WriteLine("1: " + test.FullName + " " + test.IsAJohn);

test.LastName = "Smith";

Console.WriteLine("2: " + test.FullName + " " + test.IsAJohn);
Console.WriteLine("3: " + test.FullName + " " + test.IsAJohn); // why compute again?

test.FirstName = "David";
Console.WriteLine("4: " + test.FullName + " " + test.IsAJohn);

[Observable]
public class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }

    [Computed]
    public string FullName
    {
        get
        {
            Console.WriteLine("Computing FullName");
            return FirstName + " " + LastName;
        }
    }

    [Computed]
    public string IsAJohn
    {
        get
        {
            Console.WriteLine("Computing IsAJohn");
            return FirstName == "John" ? "Yep" : "Nope";
        }
    }    
}

Outputs:

Computing FullName
Computing IsAJohn
1: John Doe Yep
Computing FullName
Computing IsAJohn
2: John Smith Yep
Computing FullName
Computing IsAJohn
3: John Smith Yep
Computing FullName
Computing IsAJohn
4: David Smith Nope