首页 新闻 会员 周边 捐助

将类的属性的类型设计为接口所产生的问题

0
悬赏园豆:20 [已关闭问题]

有这样一个场景:我做了一个小程序,需要对每天的开支情况进行登记.
我将开支类(Expense)设计如下:
    public class Expense
    {
        private IAccount account;
        private decimal amount;

        public decimal Amount
        {
            get { return amount; }
            set { amount = value; }
        }

        public IAccount Account
        {
            get { return account; }
            set { account = value; }
        }
        public void Register()
        {
            account.Draw(amount);
        }
    }

这里Expense类中的Account属性我设计成了IAccount接口.原因是因为用于消费的帐户有很多种,比如CreditCard(信用卡)\Cash(现金)\DebitCard(借记卡)等等各种帐户:
    public interface IAccount
    {
        string Name
        {
            get;
            set;
        }

        decimal Amount
        {
            get;
            set;
        }

        Individual Holder
        {
            get;
            set;
        }

        Currncy Currncy
        {
            get;
            set;
        }
        void Draw(decimal amount);
        void Deposit(decimal amount);
        void Transfer(IAccount account, decimal amount);
    }

CreditCard类
    public class CreditCard : IAccount
    {
        private Institution bank;
        private string name;
        private string number;
        private Individual holder;
        private decimal overdraft;
        private decimal cashLimit;
        private decimal interestRate;
        private int repaymentDay;
        private int billDay;
        private DateTime startDate;

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }
        private decimal amount;
        private DateTime expiryMonth;
        private Currncy currency;

        #region IAccount 成员

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public decimal Amount
        {
            get
            {
                return amount;
            }
            set
            {
                amount = value;
            }
        }

        public Individual Holder
        {
            get
            {
                return holder;
            }
            set
            {
                holder = value;
            }
        }

        public Currncy Currncy
        {
            get
            {
                return currency;
            }
            set
            {
                currency = value;
            }
        }

        public void Draw(decimal amount)
        {
            this.amount = this.amount - amount;
        }

        public void Deposit(decimal amount)
        {
            this.amount = this.amount + amount; ;
        }

        public void Transfer(IAccount account, decimal amount)
        {
            this.amount = this.amount - amount;
            account.Deposit(amount);
        }

        #endregion
    }

用这几个类来表达我想实现的功能是没有问题的,但是确没办法映射到关系型数据库.各个帐户类倒没有问题,现在主要的问题存在于Expense类的持久化.其中IAccount属性没办法用数据表表示,或都说很难表示.

请问我这里类的设计有什么问题吗?如何才能实现我的想法呢?

我在另一个场景下用另外一种思路来设计了一组类,但很相似:在GIS中我要用到行政区划,于是我设计了一个行政区(District)类,其中它的一个属性Category我设计成了一个抽象类DistrictCategory,从DistrictCategory派生出六个子类为国家\省\市\县\乡\村.也是同样的问题:虽然这样可用七个数据库表来存储相应的数据,但是在将数据表还原为对象时也会有问题

请各位大侠帮忙解决!谢谢了

青林一霸的主页 青林一霸 | 初学一级 | 园豆:184
提问于:2009-10-27 18:40
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册