Andy said:
I have an object (Contract). After it has been signed (it's a legal
document), it can't be changed. It is read only, frozen or locked
depending on what word you want to use after the customer signs it.
Should this state be controlled by the Contract itself or some other
source? And the other question would be, should the fact that the
contract has been signed be stored inside the contract itself, or
somewhere else (like a database table row) with the contract itself.
OO is about encapsulation (among other things), so the 'Contract'
object contains the Contract data and behavior. If another object wants
to read Contract data, it has to access a Contract object's properties.
If the Contract is readonly, the easiest way to enable that is inside
Contract. This way, the code USING 'Contract', no matter where or when,
will not be able to change Contract's data when calling a property
setter.
Say you place it OUTSIDE the Contract object:
Contract c = GetContract(id);
// some code
// now you want to manipulate c. But as the ReadOnly bit is
// outside c, you have to test HERE
if(!IsReadOnly(c))
{
// manipulate c
}
this is of course error prone: if you forget to check, you introduce a
bug: even if 'c' is readonly, it will be manipulated. So the best place
to switch Contract into readonly is inside Contract.
Now, you can make this happen inside Contract by writing its
properties like:
// inside contract
public string ContractOwnerName
{
get { return _contractOwnerName;}
set {
if(!IsSigned())
{
_contractOwnerName = value;
}
}
}
so signing it will make it readonly.
Of course, this is in-memory. If you want to persist this state, you
have to store it somewhere, like in a database.
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------