Design Question for Depandant Properties

Design Question for Depandant Properties

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


czuvich posted on Tuesday, May 27, 2008

Hello all,

I have a question about how to best construct my class. I have a class with 3 different private flags that signify the state of the object. These flags aren't set until a Command (CommandBase) is run. I also have a public readonly property (which is a enum) that gives the user the state of the object based on those flags (property name is CurrentState).

Depending on the CurrentState, certain fields are required. How can I create this sort of dependency so that when the CommandBase tries to run, it fires off validation for the properties that are affected by it? The easiest solution I thought of was to fire off validation for each property I'm concerned with for each command; however, I have to believe there is a way to fire off validation for "CurrentState" and that property would have dependencies to the other corresponding properties.. or perhaps there is a better way. I have tried adding a dependency to the property and CurrentState, but the validation never occurs. Here is a small code snippet.

ValidationRules.AddRule<NowaBO>(CustomRules.IsDOIValid, "BpoGwi");

ValidationRules.AddDependantProperty("BpoGwi", "TotalBpoGwi");

ValidationRules.AddDependantProperty("TotalBpoGwi", "CurrentState");

ValidationRules.AddRule<NowaBO>(NowaBO.IsBpoGwiTotalValid<NowaBO>, "TotalBpoGwi");

private static bool IsBpoGwiTotalValid<T>(T sender, RuleArgs e)

where T : NowaBO

{

switch (sender.CurrentState)

{

case NowaState.New:

return true;

default:

if (sender.TotalBpoGwi != 1)

{

e.Description = string.Format(Resources.DoiTotalInvalid,

"Bpo Gwi");

return false;

}

else

return true;

}

}

 

Any guidance would be great. Thank you.

 

czuvich replied on Tuesday, May 27, 2008

public NowaState CurrentState

{

get

{

if (!this._isDistributed && !this._pendingApproval)

return NowaState.New;

else if (this._pendingApproval)

return NowaState.Pending;

else

return NowaState.Distributed;

}

}

Copyright (c) Marimer LLC