Kai
Kai

Reputation: 5910

Class Design: Configuration Object

I need to write a class for different "configuration objects" that hold something like "if xyz = 5 then .." to transfer some Rules and Actions to those Rules.

Can anybody please help me with a clever design of a class like that?

I'm using C#.NET 3.5

Upvotes: 1

Views: 301

Answers (2)

eulerfx
eulerfx

Reputation: 37719

If the rules you are referring to should map to actions, then you can have a class like this:

interface IRule<T>
{
    void Apply(T obj);
}

class PredicateRule<T> : IRule<T>
{
    public ActionRule(Func<T, bool> p, Action<T> a) 
    {
       this.predicate = p;
       this.action = a;
     }

    readonly Func<T, bool> predicate;
    readonly Action<T> action;

    public void Apply(T obj)
    {
      if (this.predicate(obj))
          this.action(obj);
    }   
}

So then your "if xyz = 5 then .." rule could be declared like this:

var r = new PredicateRule<XyzClass>(x => x == 5, x => { //then...  });

Upvotes: 2

Mark Redman
Mark Redman

Reputation: 24515

Not sure what you're after but but you could have an overridable SetDefaults() method to perform actions when creating an object and a similar ApplyRules() method to perform actions before saving an object?

Upvotes: 0

Related Questions