HDI: implement a business rule which enables updates & deletes

HDI: implement a business rule which enables updates & deletes

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


superkuton posted on Sunday, December 25, 2011

How do I implement a business rule that if an item is "posted" it can no longer be updated nor deleted?

 

thank you.

JonnyBee replied on Sunday, December 25, 2011

First a question: What do you expect to happen when item is "Posted"?

1. can fields be edited or should they be disabled
2. Save/Delete method is not allowed (and should throw Security exception).

For 1) the simplest solution is to override CanEditProperty to return false if item is "posted".

for 2: You will create a rule class that inherits from AuthorizationRule and make sure CachResult is set to false.

In the Execute method you will set HasPermission to not IsPosted.

This sample rule is from Samples\Net\cs\BusinessRuleDemo:

  public class OnlyForUS : Csla.Rules.AuthorizationRule
  {
    private IMemberInfo CountryField { get; set; }
  
    public OnlyForUS(AuthorizationActions action, IMemberInfo element, IMemberInfo countryField)
      : base(action, element)
    { CountryField = countryField; }

    public override bool CacheResult
    { get { return false; }
    }

    protected override void Execute(AuthorizationContext context)
    {
      var country = (string)MethodCaller.CallPropertyGetter(context.Target, CountryField.Name);
      context.HasPermission = country.Equals(CountryNVL.UnitedStates);
    }
  }

 

Copyright (c) Marimer LLC