在 C# 中使用 NHibernate 为值对象配置复杂类型

Config complex type for value object using NHibernate in C#

我是 DDD 和 NHibernate 的新手,当我尝试使用 NHibernate fluent 配置 table(使用代码优先)时。

在 EntityFramework 中,我们可以使用 ComplexTypeConfiguration 来配置值对象并在许多 table 中使用该配置,但我不知道 NHibernate 如何为值对象单独配置,例如 EntityFramework .

internal class EmployeeConfiguration : ClassMap<Employee>
{
    public EmployeeConfiguration()
    {
        Table("Employees");
        Id(emp => emp.Id).GeneratedBy.Identity();

        Component(emp => emp.FullName, name =>
        {
            name.Map(ele => ele.FirstName).Column("FirstName").Length(255).Not.Nullable();
            name.Map(ele => ele.LastName).Column("LastName").Length(255).Not.Nullable();
            name.Map(ele => ele.MiddleName).Column("MiddleName").Length(255).Nullable();
        });
    }

}

我在谷歌上搜索了解决方案,但没有任何帮助。

非常感谢任何帮助,谢谢。

据我了解,您需要一个可重用的 ComplexetType 配置,它可以在各种表上实现。根据 this 的说法,到目前为止一切顺利 "If you are mapping an entity you'd use ClassMap and if you are mapping a value object you'd use ComponentMap class"。因此,我基于它构建了一个解决方案,也许这不是正确的答案,但希望这会有所帮助。

//Complex type class
public class Address
{
    public virtual string City { get; set; }
    public virtual string Street { get; set; }
    public virtual string StateOrProvince { get; set; }
    public virtual string Country { get; set; }
}

//Entity
public class Employee
{
    public virtual int Id { get; set; }
    public virtual string FullName { get; set; }
    public virtual Address Address { get; set; }
    public virtual decimal Salary { get; set; }
}

//ComplexTypeConfiguration
public class AddressMap : ComponentMap<Address>
{
   public AddressMap()
   {
       Map(c => c.City).Column("City").Length(255).Not.Nullable();
       Map(c => c.Country).Column("Country").Length(255);
       Map(c => c.StateOrProvince).Nullable();
       Map(c => c.Street).Nullable();
   }
}

//Mapping
public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("Employees");

        Id(c => c.Id);
        Map(c => c.FullName);
        Map(c => c.Salary);
        Component(c => c.Address);
    }
}

//Connection string
public class NHibernateHelper
{
    public static ISession OpenSession()
    {
        ISessionFactory sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2012
            .ConnectionString(@"Server=.;Database=TestDB;Trusted_Connection=True;")
            .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<EmployeeMap>())
            .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
            .BuildSessionFactory();

        Console.WriteLine("Database Connected");
        return sessionFactory.OpenSession();
    }
}    

//Test script    
static void Main(string[] args)
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        Employee employee = new Employee()
        {
            Id = 1,
            FullName = "Nidust Ashen",
            Salary = 12345678,
            Address = new Address()
            {
                City = "test1",
                Street = "test2",
                Country = "test3",
                StateOrProvince = "test4",
            }
        };

        session.Save(employee);
        Console.WriteLine("Done!");
        Console.ReadLine();
    }
}

最终结果: