Is this a good way to design a class so I can prevent values being changed/ the state must remain as it was when it was intantiated...
This code would be normally instantiated after a call from the database.
public class InstructionWorkflow : IStageWorkflow
{
private readonly int _companyID;
public InstructionWorkflow(int instructionid, int progressid, int companyID, InstructionKeyInfoDTO keyInfo, InstructionStageProgress currentStage)
{
_companyID = companyID;
CurrentStage = currentStage;
KeyInfo = keyInfo;
ProgressID = progressid;
InstructionID = instructionid;
}
public int InstructionID { get; private set; }
public int ProgressID { get; private set; }
public InstructionKeyInfoDTO KeyInfo { get; private set; }
private InstructionStageProgress CurrentStage { get; set; }
public bool IsMyCompanyOwnerOfStage
{
get { return CurrentStage.StageOwnerID == _companyID; }
}
/// <summary>
/// #1 stage cannot be ticked as completed
/// #2 stage must be initiated by the user, this will mean stage is open
/// This is the best way so we can code against opening and closing a stage elsewhere,
/// that will have a lot of rules on its own
/// #3 stage must be owned by loggedon users company
/// </summary>
public bool IsStageOpenForUpdates
{
get { return IsMyCompanyOwnerOfStage && !CurrentStage.IsCompleted && CurrentStage.DateInitiated != null; }
}
public int? StageOwnerID
{
get { return CurrentStage.StageOwnerID; }
}
}
public interface IStageWorkflow
{
bool IsMyCompanyOwnerOfStage { get; }
bool IsStageOpenForUpdates { get; }
int? StageOwnerID { get; }
int ProgressID { get; }
int InstructionID { get; }
InstructionKeyInfoDTO KeyInfo { get; }
}
Update: As per suggestions
public class InstructionWorkflow : IStageWorkflow
{
private readonly IUserSession _userSession;
private readonly IReadOnlySession _readOnlySession;
public InstructionWorkflow(int instructionid, int progressid, IUserSession userSession, IReadOnlySession readOnlySession)
{
_userSession = userSession;
_readOnlySession = readOnlySession;
ProgressID = progressid;
InstructionID = instructionid;
CurrentStage = ResolveWorkflow();
}
public int InstructionID { get; private set; }
public int ProgressID { get; private set; }
public InstructionKeyInfoDTO KeyInfo { get; private set; }
private InstructionStageProgress CurrentStage { get; set; }
public bool MyCompanyIsOwnerOfStage
{
get { return CurrentStage.StageOwnerID == _userSession.MyProfile.CompanyID; }
}
/* NOTES:
* #1 stage cannot be ticked as completed
* #2 stage must be initiated by the user, this will mean stage is open
* This is the best way so we can code against opening and closing a stage elsewhere,
* that will have a lot of rules on its own
* #3 stage must be owned by loggedon users company
*/
public bool StageIsOpenForUpdates
{
get { return MyCompanyIsOwnerOfStage && !CurrentStage.IsCompleted && CurrentStage.DateInitiated != null; }
}
public int? StageOwnerID
{
get { return CurrentStage.StageOwnerID; }
}
private InstructionStageProgress ResolveWorkflow()
{
var currentStage = _readOnlySession.Single<InstructionStageProgress>(
x => x.ProgressID == ProgressID && x.InstructionID == InstructionID);
if (currentStage == null)
throw new NoAccessException(string.Format("workflow stage progress not found for instruction #{0} and progressid#{1}",
InstructionID, ProgressID));
KeyInfo = null; //set here similar to currentstage above...
return currentStage;
}
}
Additional Info: Origincal IUserSession
public interface IUserSession
{
void SetClientStore(string loginID, string identifier);
void Logout();
string LoginID { get; set; }
string CompanyIdentifier { get; set; }
UserProfile MyProfile { get; }
}
//new user session
public interface IUserSession
{
void SetClientStore(string loginID, string identifier);
void Logout();
string LoginID { get; set; }
int UserID { get; set; }
RoleName Role { get; set; }
string CompanyIdentifier { get; set; }
int CompanyID { get; set; }
CompanyType CompanyType { get; set; }
string CompanyFriendlyName { get; set; }
PricePlanType PricePlan { get; set; }
}