Background: I am connecting to a legacy Database that has two entities:
Order (Id, Date, ...)
ForwardOrder(OrderId)
… as you can see ForwardOrder could be just a property of an Order
Requirements: Try to reflect ForwardOrder as a Boolean property of Order class in .NET. Connect to the database with NHibernate.
The problem: I tried implementing IUserType but no luck – column doesn’t exist error.
The mapping is Map(x=>x.IsForwardOrder).CustomType<ForwardOrderType>();
Adding a Formula does make it load properly, but the IUserType setter is never used.
Question: Can it be done? Can an object’s existence in a separate table be a flag on my Entity? I know that I can just map a related object but I would rather to avoid that.
The way to do this is set up the
ForwardOrderas a protected property on the code level and treat it like a lazy component. Then you can actually leaveIsForwardOrderout of your NHibernate mapping completely and just do something like:You could use polymorphism as well, but it seems like overkill for a boolean. This approach would entail creating a subclass that has the ForwardOrder property and defining a table-per-subclass join to the
ForwardOrdertable. You would still need to define a boolean property likeon the base class
on the joined subclass.
Alternatively, with this approach, you could use an extension method such as
(assuming the base class is
Orderand the subclass isForwardOrder)