MarimerLLC / csla

A home for your business logic in any .NET application.
https://cslanet.com
MIT License
1.26k stars 403 forks source link

Just an idea around property / rule depencencies #674

Closed dazinator closed 7 years ago

dazinator commented 7 years ago

I was working with knockoutjs quite recently, which has the concept of computed functions - basically an ordinary function, but knockoutjs tracks which "observable" properties that function accesses when the function runs, then it sets up dependencies automatically, so that when those properties change, the function is re-executed.

This is quite similar in nature to CSLA. However in CSLA, you configure the dependencies explicitly, like this:

            // Changing CompletedProperty should trigger StatusProperty:
            BusinessRules.AddRule(new Dependency(CompletedProperty, StatusProperty));

            BusinessRules.AddRule(new Lambda(StatusProperty, (ctx) =>
            {
                var order = ctx.Target as ISupplierOrder;
                if (order.Completed)
                {
                    ctx.AddOutValue(SupplierOrderStatuses.Completed); 
                    return;
                }        
                // otherwise status is ready
                ctx.AddOutValue(SupplierOrderStatuses.Ready); 
                return;
            }));

So the idea / proposal I was thinking about would be to do something like this:

            BusinessRules.AddRule(new Lambda(StatusProperty, (ctx) =>
            {
                var order = ctx.Target as ISupplierOrder;
                var propValue = ctx.GetDependentValue<bool>(CompletedProperty);

                if (order.Completed)
                {
                    ctx.AddOutValue(SupplierOrderStatuses.Completed); 
                    return;
                }        
                // otherwise status is ready
                ctx.AddOutValue(SupplierOrderStatuses.Ready); 
                return;
            }));

In the above example, the idea would be, you'd no longer need to add Dependency rules explicitly for the lamda rule, CLSA would instead discover the dependent properties that the lambda expression observes in its body, and set up the dependencies automatically.

  var propValue = ctx.GetDependentValue<bool>(CompletedProperty);

Would cause the lamda to execute whenever that property changed.

As to how possible this is I don't know..

dazinator commented 7 years ago

Im closing this because it remains a pipedream that I won't have time to work on

dazinator commented 7 years ago

Also there is nothing wrong with the current mechanism of explicitly wiring up dependencies.

jonnybee commented 7 years ago

Just want to add:

When your rule declares InputProperties and AffectedProperties these are automatically considered dependencies. Then there is no need to add an explicit Dependency rule.

See code for Dependency rule:

public Dependency(Csla.Core.IPropertyInfo primaryProperty, 
                   params Csla.Core.IPropertyInfo[] dependencyProperty)
  : base(primaryProperty)
{
  AffectedProperties.AddRange(dependencyProperty);
}
dazinator commented 7 years ago

Good point! :-)

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.