Hi Guys.
New to DDD and nhibernate and have been looking at the best way to design certain entities using inheritence. I have Payments like Credit Card, Cash, Bank Transfer etc. I set them up to derive from an abstract class and have subclasses that implement their own relevant members.
I have created the following setup: Payment abstract class with derived classes like CCPayment, CashPayment etc. I have attached a sample of how I have set this up with a quick test method.
Am I on the right route or is there another more appropriate design?
Code attached below:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace MyCo.MyProject.BusinessLayer
{
// abstract Payment class with generic members
public abstract class Payment : EntityBase
{
protected Payment() { }
public Payment(decimal amount, DateTime transactionDate)
{
Amount = amount;
TransactionDate = transactionDate;
}
public virtual decimal Amount { get; set; }
public virtual DateTime TransactionDate { get; set; }
}
// credit card payment with credit card specific members
public class CCPayment : Payment
{
protected CCPayment() { }
public CCPayment(Decimal amount, DateTime transactionDate, string ccNumber, string ccExpiryDate)
: base(amount, transactionDate)
{
CCNumber = ccNumber;
CCExpiryDate = ccExpiryDate;
}
public virtual string CCNumber { get; set; }
public virtual string CCExpiryDate { get; set; }
}
// bank transfer with bank transfer specific members
public class BankTransfer : Payment
{
protected BankTransfer() { }
public BankTransfer(Decimal amount, DateTime transactionDate, string bankName, string accountNo, string bankCode)
: base(amount, transactionDate)
{
BankName = bankName;
AccountNo = accountNo;
BankCode = bankCode;
}
public virtual string BankName { get; set; }
public virtual string AccountNo { get; set; }
public virtual string BankCode { get; set; }
}
// collection of various payments
public class Payments
{
private IList<Payment> _Payments = new List<Payment>();
public Payments() { }
public void AddPayment(Payment payment)
{
_Payments.Add(payment);
}
public IList<Payment> PaymentList
{
get
{
return _Payments.ToList<Payment>().AsReadOnly();
}
}
}
[TestFixture]
public class PaymentTest
{
[Test]
public void Can_Create_Various_Payments()
{
// create various payments
CCPayment ccPayment = new CCPayment(99.99m, DateTime.Now, "1111111111111111", "09/09");
BankTransfer bankTranser = new BankTransfer(10.95m, DateTime.Now, "My Bank", "1000", "100");
// add to a collection
Payments payments = new Payments();
payments.AddPayment(ccPayment);
payments.AddPayment(bankTranser);
// loop through and determin at runtim
foreach (Payment payment in payments.PaymentList)
{
switch (payment.GetType().Name)
{
case "CCPayment":
Console.Write("This is a cc payment");
break;
case "BankTransfer":
Console.Write("This is a bank transfer");
break;
}
}
}
}
}