Business Rules, Generics and inheritance

Business Rules, Generics and inheritance

Old forum URL: forums.lhotka.net/forums/t/10728.aspx


Curelom posted on Friday, September 30, 2011

I'm trying to get an authorization rule to work with multple objects. Within the Execute method, I use the Parent property derived from the BusinessBase object.

protected override void Execute(AuthorizationContext context) {
            if (_roles.Count > 0) {
                FullyLoadedCost flc = context.Target as FullyLoadedCost;
                if (flc != null) {
                    FullyLoadedCostList fl = flc.Parent as FullyLoadedCostList;
                    if (fl != null && fl.WhatIf) {
                        context.HasPermission = true;
                        return;
                    }
                }
...

In the current example this is specific to the FullyLoadedCost object.

I'd like to make the authorization rule more generic.
I can use an interface for the boolean WhatIf property, but I can't
quite figure how to get BusinessBase<T> out of the context Target property.

The below doesn't work as it is requiring T to be defined.
Csla.BusinessBase<T> myBase = context.Target as Csla.BusinessBase<T>;
Perhaps I could do this if the AuthorizationRule was generic itself.

Any ideas?

tiago replied on Friday, September 30, 2011

Hi Curelom,

Have a look at NoDuplicates business rule. You can find it at http://cslagenfork.codeplex.com/

JonnyBee replied on Saturday, October 01, 2011

Hi,

You could also look at Csla.Rules.RuleExtensions and how it  "wrap" a lambda expression into a rule.

http://www.lhotka.net/cslacvs/viewvc.cgi/core/trunk/Source/Csla/Rules/BusinessRulesExtensions.cs?view=markup 

I suppose you could create generic Authz rule with a lamda "WhatIf". (I haven't tried this on Authz rules myself).

Curelom replied on Saturday, October 01, 2011

Interesting. I'll have to give this a try.

Copyright (c) Marimer LLC