Most importantly which bothers me in the following implementation is the call in BranchAddManager
, do I really need to call base.StartAddNewOperation();
? How can I get away without making this call?
public override void AddNew()
{
//I would like to avoid calling the base method, if I forget then I may run into bug.
base.StartAddNewOperation();
}
Please help me to verify whether this approach make sense to you? Am also open to take another approach, if you think it make more sense. I have just noticed, this could be a builder pattern
approach too. It will be a bit too much to add the entire code here but I try to demonstrate with relevant code, if you think the explanation is not enough then please let me know.
Program (main call)
Making a call to add a new Branch
Record by setting properties to BranchModel
.
class Program
{
static void Main(string[] args)
{
var model = new BranchModel()
{
BranchName = "Test Branch",
Description = "The description of a branch",
BranchTypeId = 2
};
try
{
new BranchAddManager(model).AddNew();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
AddManagerBase (abstract)
Concrete classes which needs to Add new records into the system should inherit from this base class
.
abstract class AddManagerBase<T> where T : class
{
private TransactionModel _transaction;
protected readonly T _model;
public AddManagerBase(T model)
{
_model = model;
_transaction = new TransactionModel()
{
TransactionDate = DateTimeOffset.Now
};
}
public abstract void AddNew();
protected abstract void RunValidations();
protected abstract void PreDatabaseOperation();
protected abstract void DatabaseOperations();
protected void StartAddNewOperation()
{
RunValidations();
PreDatabaseOperation();
DatabaseOperations();
LogThisActivity();
}
protected void SetTransactionId(int transactionId)
{
Console.WriteLine("SetTransactionId()");
_transaction.TransactionId = transactionId;
}
private void LogThisActivity()
{
Logger.AuditLog(AuditType.Add, _transaction.TransactionId, _transaction.TransactionDate);
}
}
BranchAddManager (concrete)
This class inherits the base class. The following example is supposed to add a new Branch Record
.
class BranchAddManager : AddManagerBase<BranchModel>
{
public BranchAddManager(BranchModel model) : base(model)
{ }
public override void AddNew()
{
//I would like to avoid calling the base method, if I forget then I may run into bug.
base.StartAddNewOperation();
}
protected override void RunValidations()
{
//In this example ensure the BranchTypeId exists in the DB.
Console.WriteLine("RunValidations()");
}
protected override void PreDatabaseOperation()
{
//Convert the Model to Pojo/Poco, etc..
Console.WriteLine("PreDatabaseOperation()");
}
protected override void DatabaseOperations()
{
//Insert into Branch Table
Console.WriteLine("DatabaseOperations()");
//manually setting the ID for this example but usually this is given by the Database when the new record gets inserted.
int newBranchSqlIdentityId = 9990;
base.SetTransactionId(newBranchSqlIdentityId);
}
}
Logger, AuditType, BranchModel and TransactionModel (supporting classes)
public enum AuditType
{
Add,
Delete,
Update
}
public class Logger
{
public static void AuditLog(AuditType auditType, int transactionId, DateTimeOffset date)
{
Console.WriteLine(string.Format("{0} {1} {2}", auditType, transactionId, date));
}
}
class TransactionModel
{
public int TransactionId { get; set; }
public DateTimeOffset TransactionDate { get; set; }
}
class BranchModel
{
public string BranchName { get; set; }
public string Description { get; set; }
public int BranchTypeId { get; set; }
}