FluentNHibernate子类和分层数据

时间:2011-06-11 15:56:56

标签: fluent-nhibernate

我有一个帐户表,用于存储主帐户和子帐户。主账户与子账户基本相同,只是他们可以拥有关联公司。 Account是一个抽象类,MasterAccount和SubAccount都是从它派生的。

MasterAccount是任何具有null ParentAccountId的帐户条目。如果帐户记录具有ParentAccountId,那么它是SubAccount,ParentAccountId引用MasterAccount的AccountId字段。

我正在尝试为他们获取FluentNhibernate映射。

这些类看起来如下

    public class Account : EntityBase
    {
        public Account() { }

        public virtual string AccountNumber { get; set; }

        public virtual string AccountName { get; set; }

        public virtual string ContactRole { get; set; }

        public virtual bool EmailBillDataFile { get; set; }

        public virtual bool EmailBill { get; set; }

        public virtual bool PostBill { get; set; }

        public virtual BillingMethod BillingMethod { get; set; }

        public virtual BillingAddressType BillingAddressType { get; set; }

        public virtual Contact Contact { get; set; }

        public virtual bool IsInvoiceRoot { get; set; }

        public virtual string Password { get; set; }

        public virtual bool HasRequestedInvoicing { get; set; }

        public virtual bool IsInternational { get; set; }

        public virtual decimal AmountPaid { get; set; }

        public virtual decimal PreviousBill { get; set; }

        public virtual void MakePayment(decimal amount)
        {
            MakePayment(amount, null);
        }

        public virtual void MakePayment(decimal amount, string invoiceNumber)
        {
            AmountPaid += amount;

            if (string.IsNullOrEmpty(invoiceNumber))
                LogActivity(string.Format("Made payment of {0:c}", amount));
            else {
                LogActivity(string.Format("Made payment of {0:c} on Invoice '{1}'", amount, invoiceNumber));
            }
        }

        public virtual Invoice CreateInvoice()
        {
            Invoice invoice;
            invoice = IsInternational ? new NoGstInvoice() : new Invoice();

            // Can update invoice properties that rely on account data here.


            return invoice;
        }

        #region Business Rules

        public override IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(AccountName))
                yield return new RuleViolation("Account Name required", "AccountName");

            if (string.IsNullOrEmpty(AccountNumber))
                yield return new RuleViolation("Acocunt Number required", "AccountNumber");

            if (string.IsNullOrEmpty(Password))
                yield return new RuleViolation("Password required", "Password");

            yield break;
        }

        #endregion 

    }

    public class MasterAccount : Account
    {
        private Company _company;
        private IList<SubAccount> _subAccounts;

        public MasterAccount() : this(null) { }

        public MasterAccount(Company company)
        {
            _company = company;
            _subAccounts = new List<SubAccount>();
        }

        public virtual Company Company
        {
            get { return _company;  }
        }

        public virtual IEnumerable<SubAccount> SubAccounts
        {
            get { return _subAccounts; }
        }

        public virtual SubAccount CreateSubAccount(string accountNumber, string accountName)
        {
            var subAccount = new SubAccount(this)
                                 {
                                     AccountName = accountName,
                                     AccountNumber = accountNumber,
                                     Contact = this.Contact,
                                     ContactRole = this.ContactRole,
                                     PreviousBill = 0,
                                     AmountPaid = 0,
                                     BillingAddressType = this.BillingAddressType,
                                     BillingMethod = this.BillingMethod,
                                     IsInternational = this.IsInternational,
                                     IsInvoiceRoot = false,
                                     EmailBill = this.EmailBill,
                                     EmailBillDataFile = this.EmailBillDataFile,
                                     Password = this.Password,
                                     PostBill = this.PostBill                                     
                                 };

            return subAccount;
        }     
    }

public class SubAccount : Account
    {
        private MasterAccount _masterAccount;

        public SubAccount() { }

        public SubAccount(MasterAccount master)
        {
            _masterAccount = master;
        }

        public virtual MasterAccount MasterAccount 
        { 
            get { return _masterAccount;  }
        }        
    }

我的映射是:

public class AccountMap : ClassMap<Account>
{
    public AccountMap()
    {
        Table("Account");
        Id(x => x.Id).Column("AccountId").GeneratedBy.Identity();
        Map(x => x.AccountName).Length(50).Not.Nullable();
        Map(x => x.AccountNumber).Length(10).Not.Nullable();
        Map(x => x.ContactRole).Length(50);
        Map(x => x.BillingMethod).Not.Nullable();
        Map(x => x.EmailBill).Not.Nullable();
        Map(x => x.PostBill).Not.Nullable();
        Map(x => x.EmailBillDataFile).Not.Nullable();
        Map(x => x.BillingAddressType).Not.Nullable();
        Map(x => x.IsInvoiceRoot).Not.Nullable();
        Map(x => x.HasRequestedInvoicing).Not.Nullable();
        Map(x => x.IsInternational).Not.Nullable();
        Map(x => x.PreviousBill).Not.Nullable();
        Map(x => x.AmountPaid).Not.Nullable();
        Map(x => x.Password).Length(20).Not.Nullable();

        References(x => x.Contact).Column("ContactId").Not.Nullable();

        DiscriminateSubClassesOnColumn("ParentAccountId");
    }
}

public class MasterAccountMap : SubclassMap<MasterAccount>
{
    public MasterAccountMap()
    {            
        References(x => x.Company).Column("CompanyId");
        HasMany(x => x.SubAccounts).KeyColumn("ParentAccountId").Inverse().Cascade.All();
    }
}

public class SubAccountMap : SubclassMap<SubAccount>
{
    public SubAccountMap()
    {
        References(x => x.MasterAccount).Column("ParentAccountId").Not.Nullable();
    }
}

但是,当我执行以下测试时:

[Test]
public void Can_add_subAccount_to_database()
{
    var master = Session.Get<MasterAccount>(1);
    var subAccount = master.CreateSubAccount("TST123", "Test Account");

    Session.Save(subAccount);
    Session.Flush();
    Session.Clear();

    var fromDb = Session.Get<SubAccount>(subAccount.Id);
    Assert.AreNotSame(subAccount, fromDb);
}

我在Session.Save(subAccount)上遇到异常;线。

System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

如果我在SubAccountMap中注释掉References映射,我不会得到异常 任何有关正确映射此关系的帮助都表示赞赏。

1 个答案:

答案 0 :(得分:0)

似乎我需要使用DiscriminateSubClassesOnColumn的公式方法

DiscriminateSubClassesOnColumn("").Formula("case when parentaccountid is null then '0' else '1' end");

然后在每个子类中使用以下内容

DiscriminatorValue("0");  // In MasterAccountMap

DiscriminatorValue("1"); // in SubAccountMap

请参阅http://wiki.fluentnhibernate.org/Fluent_mapping