I am new to DDD and NHibernate, when I try to config for table(using code-first) with NHibernate fluent.
In EntityFramework we can use ComplexTypeConfiguration to config for value object and using that config in many table, but I have no idea how NHibernate separate config for value object like 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();
});
}
}
I googled for find out solutions, but nothing really helpful.
Any help is greatly appreciated, thanks.
As my understanding, you need a reusable ComplextType configuration which can be implemented on various tables. So far so good, according to this "If you are mapping an entity you'd use ClassMap and if you are mapping a value object you'd use ComponentMap class". Hence, I built a solution based on it, perhaps it's not the right answer, but hopefully, this would help.
//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();
}
}
Final result: