コード例 #1
0
        public async Task SuppressTest()
        {
            var time   = Services.GetRequiredService <ITimeService>();
            var count1 = 0;
            var count2 = 0;

#pragma warning disable 1998
            var c1 = await SimpleComputed.New <int>(async (prev, cancellationToken) => count1++)
                     .UpdateAsync(false);

            var c2 = await SimpleComputed.New <int>(async (prev, cancellationToken) => count2++)
                     .UpdateAsync(false);

#pragma warning restore 1998
            var c12 = await SimpleComputed.New <(int, int)>(
                async (prev, cancellationToken) => {
                var a       = await c1.UseAsync(cancellationToken);
                using var _ = Computed.Suppress();
                var b       = await c2.UseAsync(cancellationToken);
                return(a, b);
            }).UpdateAsync(false);

            var v12a = await c12.UseAsync();

            c1.Invalidate(); // Should increment c1 & impact c12
            var v12b = await c12.UseAsync();

            v12b.Should().Be((v12a.Item1 + 1, v12a.Item2));
            c2.Invalidate(); // Should increment c2, but shouldn't impact c12
            var v12c = await c12.UseAsync();

            v12c.Should().Be(v12b);
        }
コード例 #2
0
        public static async Task Dependencies()
        {
            #region part02_dependencies
            WriteLine("Creating computed instances...");
            var cDate = SimpleComputed.New <DateTime>(async(prev, ct) => {
                var result = DateTime.Now;
                WriteLine($"Computing cDate: {result}");
                return(result);
            });
            var cCount = SimpleComputed.New <int>(async(prev, ct) => {
                var result = prev.Value + 1;
                WriteLine($"Computing cCount: {result}");
                return(result);
            });
            var cTitle = SimpleComputed.New <string>(async(prev, ct) => {
                var date   = await cDate.UseAsync(ct);
                var count  = await cCount.UseAsync(ct);
                var result = $"{date}: {count}";
                WriteLine($"Computing cTitle: {result}");
                return(result);
            });

            WriteLine("All the computed values below should be in invalidated state.");
            WriteLine($"{cDate}, Value = {cDate.Value}");
            WriteLine($"{cCount}, Value = {cCount.Value}");
            WriteLine($"{cTitle}, Value = {cTitle.Value}");

            WriteLine();
            WriteLine("Let's trigger the computations:");
            cTitle = await cTitle.UpdateAsync(false);

            WriteLine($"{cDate}, Value = {cDate.Value}");
            WriteLine($"{cCount}, Value = {cCount.Value}");
            WriteLine($"{cTitle}, Value = {cTitle.Value}");

            WriteLine();
            WriteLine($"The next line won't trigger the computation, even though {nameof(cCount)} will be updated:");
            cCount = await cCount.UpdateAsync(false);

            WriteLine($"Let's do the same for {nameof(cDate)} now:");
            cDate = await cDate.UpdateAsync(false);

            WriteLine();
            WriteLine($"Let's invalidate {nameof(cCount)} and see what happens:");
            cCount.Invalidate();
            WriteLine($"{cCount}, Value = {cCount.Value}");
            WriteLine($"As you see, no computation is triggered so far.");
            WriteLine($"But notice that {nameof(cTitle)} is invalidated as well, because it depends on {nameof(cCount)}:");
            WriteLine($"{cTitle}, Value = {cTitle.Value}");

            WriteLine($"Finally, let's update {nameof(cTitle)} again:");
            cTitle = await cTitle.UpdateAsync(false);

            WriteLine($"{cTitle}, Value = {cTitle.Value}");
            #endregion
        }
コード例 #3
0
        public static async Task CreateNoDefault()
        {
            #region part01_createNoDefault
            var c = SimpleComputed.New <DateTime>(async(prev, ct) => DateTime.Now);
            WriteLine($"{c}, Value = {c.Value}"); // Must be in Invalidated state
            c = await c.UpdateAsync(false);

            WriteLine($"{c}, Value = {c.Value}"); // Must be in Consistent state
            #endregion
        }
コード例 #4
0
 public static async Task Create()
 {
     #region part01_create
     // Later we'll show you much nicer ways to create IComputed instances,
     // but for now let's stick to the basics:
     var c = SimpleComputed.New(async(prev, ct) => prev.Value + 1, Result.New(1));
     WriteLine($"{c}, Value = {c.Value}");
     WriteLine($"Properties:");
     WriteLine($"{nameof(c.Value)}: {c.Value}");
     WriteLine($"{nameof(c.Error)}: {c.Error}");
     WriteLine($"{nameof(c.Output)}: {c.Output}");
     WriteLine($"{nameof(c.State)}: {c.State}");
     WriteLine($"{nameof(c.LTag)}: {c.LTag}"); // It is similar to ETag in HTTP
     #endregion
 }
コード例 #5
0
        public static async Task InvalidateAndUpdate()
        {
            #region part01_invalidateAndUpdate
            var c = SimpleComputed.New(async(prev, ct) => prev.Value + 1, Result.New(1));
            c.Invalidate();
            WriteLine($"{c}, Value = {c.Value}"); // Must be in Invalidated state

            var c1 = await c.UpdateAsync(false);

            WriteLine($"{c1}, Value = {c1.Value}"); // Must be in Consistent state

            // Equality isn't overriden for any implementation of IComputed,
            // so it relies on default Equals & GetHashCode (by-ref comparison).
            WriteLine($"Are {nameof(c)} and {nameof(c1)} pointing to the same instance? {c == c1}");
            #endregion
        }