如何将自定义对象与 NHibernate 一起使用
How can I use custom objects with NHibernate
请查看下面的域对象:
public Class Person
{
public virtual Guid Id { get; protected set; }
public virtual string FirstName { get; protected set; }
public virtual string Surname { get; protected set; }
public virtual System.DateTime DateOfBirth { get; protected set; }
//Domain methods are here.
}
和下面的 NHibernate 映射:
public class PersonMap : ClassMapping<Person>
{
public PersonMap()
{
Id<Guid>(x => x.Id);
Property<string>(x => x.FirstName);
Property<string>(x => x.Surname);
Property<DateTime>(x => x.DateOfBirth);
}
}
这按预期工作。假设我想将域模型更改为:
public Class Person
{
public virtual Guid Id { get; protected set; }
public virtual FirstName FirstName { get; protected set; }
public virtual Surname Surname { get; protected set; }
public virtual DateOfBirth DateOfBirth { get; protected set; }
}
请注意,原始类型已替换为对象。我这样做的原因是为了消除原始的痴迷,如下所述:http://enterprisecraftsmanship.com/2015/03/07/functional-c-primitive-obsession/
我已阅读此处的文档(第 144 页):http://stc.sbu.ac.ir/AdminTools/Docs/Files/nhibernate_reference.pdf. It is telling me to introduce a custom type. I have also read this question: nHibernate mapping to custom types。我仍在努力使用 NHibernate 代码映射来做到这一点,这也是问题的原因。
继 David Osbournes 的评论之后;答案是这样做:
Component(x => x.FirstName, y =>
{
y.Property<string>(z => z.FirstName);
});
NHibernate 使用 class 的名字 属性 映射到数据库。
您是否考虑过通过 components 映射它们?
如果您的数据位于同一 table。
,与自定义类型相比,这可能是一个更容易的起点
请查看下面的域对象:
public Class Person
{
public virtual Guid Id { get; protected set; }
public virtual string FirstName { get; protected set; }
public virtual string Surname { get; protected set; }
public virtual System.DateTime DateOfBirth { get; protected set; }
//Domain methods are here.
}
和下面的 NHibernate 映射:
public class PersonMap : ClassMapping<Person>
{
public PersonMap()
{
Id<Guid>(x => x.Id);
Property<string>(x => x.FirstName);
Property<string>(x => x.Surname);
Property<DateTime>(x => x.DateOfBirth);
}
}
这按预期工作。假设我想将域模型更改为:
public Class Person
{
public virtual Guid Id { get; protected set; }
public virtual FirstName FirstName { get; protected set; }
public virtual Surname Surname { get; protected set; }
public virtual DateOfBirth DateOfBirth { get; protected set; }
}
请注意,原始类型已替换为对象。我这样做的原因是为了消除原始的痴迷,如下所述:http://enterprisecraftsmanship.com/2015/03/07/functional-c-primitive-obsession/
我已阅读此处的文档(第 144 页):http://stc.sbu.ac.ir/AdminTools/Docs/Files/nhibernate_reference.pdf. It is telling me to introduce a custom type. I have also read this question: nHibernate mapping to custom types。我仍在努力使用 NHibernate 代码映射来做到这一点,这也是问题的原因。
继 David Osbournes 的评论之后;答案是这样做:
Component(x => x.FirstName, y =>
{
y.Property<string>(z => z.FirstName);
});
NHibernate 使用 class 的名字 属性 映射到数据库。
您是否考虑过通过 components 映射它们?
如果您的数据位于同一 table。
,与自定义类型相比,这可能是一个更容易的起点