In the following example, the writer of the derived class is expected to call base.Add (). If this happens 1, the base can do one kind of code. If this happens last, the base may execute different logic (see Sample). I don't seem to have two ways. And an easy fix would be to stop calling the base method altogether, because the base will never know if it is called first, last, or in the middle or twice!
What is an object oriented way to handle this? Should I just stop injecting code into the underlying methods because I will never know the prerequisites and post-conditions?
EDIT: The goal is to have a business object class that performs CRUD operations. Duplicate code will be moved to the base class. For example, checking if there is a record before adding a record, the identifier of the business object is 0 and it is checked that after saving the identifier of the business object is> 0.
namespace StackOverFlowSample
{
class BusinessObjectBase
{
private bool _isNew;
private int _id;
public virtual void Add(string newAccount)
{
if(_isNew && _id>0) throw new InvalidOperationException("Invalid precondition state");
if (!_isNew && _id == 0) throw new InvalidOperationException("Invalid post condition state");
}
}
class BusinessObject : BusinessObjectBase {
public override void Add(string newAccount)
{
base.Add(newAccount);
base.Add(newAccount);
}
}
}
source
share