-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Inheritence in DDD - am I on the right route...
PostPosted: Sun Jun 28, 2009 10:58 am 
Newbie

Joined: Sat Mar 07, 2009 2:24 am
Posts: 9
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;
                }
            }
        }
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.