The state of an object controlled by the object itself or some other source?

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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.
 
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#)
------------------------------------------------------------------------
 
So, I should be able to set a readonly bit and put it in all of the
properties. When I want to save the signed state of the object, I can write
the signed bit to a database bit column. Just making sure I get how this
works first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top