我正在学习 NHibernate,当我定义了一个名为“EntityBase”的抽象类时:
public abstract class EntityBase
{
public EntityBase()
{
CreateTime = DateTime.Now;
IsDelete = false;
}
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual DateTime CreateTime { get; protected set; }
public virtual bool IsDelete { get; set; }
public virtual int Version { get; protected set; }
}
像这样的一些子类:
public class Role : EntityBase, IAggregateRoot
{
public virtual string Name { get; set; }
public virtual IList<User> Users { get; set; }
public virtual IList<Permission> Permissions { get; set; }
}
public class Department : EntityBase, IAggregateRoot
{
public virtual string Name { get; set; }
public virtual IList<User> Users { get; set; }
}
我会在下面写很多 *.hbm.xml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="LY.OA.Model.Authority" assembly="LY.OA.Model">
<class name="Role" table="T_Role" lazy="false">
<id name="Id" column="Id" type="int">
<generator class="native"></generator>
</id>
<property name="Name">
<column name="RoleName" sql-type="varchar(30)" not-null="true"></column>
</property>
<bag name="Users" table="T_UserRole" inverse="true" cascade="all" lazy="true">
<key column="RoleId"></key>
<many-to-many class="User" column="UserId"/>
</bag>
<!--Common properties-->
<property name="Description" column="DESCRIPTION" type="String"/>
<property name="CreateTime" column="CREATETIME" type="DateTime"/>
<property name="IsDelete" column="ISDELETE" type="Boolean"/>
<property name="Version" column="VERSION" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="LY.OA.Model.Authority" assembly="LY.OA.Model">
<class name="Department" table="T_Department" lazy="false">
<id name="Id" column="Id" type="int">
<generator class="native"></generator>
</id>
<property name="Name">
<column name="DepartmentName" sql-type="varchar(30)" not-null="true"></column>
</property>
<!--Common properties-->
<property name="Description" column="DESCRIPTION" type="String"/>
<property name="CreateTime" column="CREATETIME" type="DateTime"/>
<property name="IsDelete" column="ISDELETE" type="Boolean"/>
<property name="Version" column="VERSION" />
</class>
</hibernate-mapping>
但是,我不会在每个xml中编写这么无聊的通用属性,但是我可以将这些通用属性放在文件中的哪里,以便每个映射文件都可以引用它呢?