I am working on legacy system,and I'm in the process of porting it to nhibernate.
The system is based on generated "Entity" C# classes, and a data access infrastructure which is based on wrapping cross-entity references in a special "Link" class. This was a misguided attempt to implement lazy-loading, which in practice harms more than it helps.
Example:
public class Order
{
public Guid Id { get; protected set;}
public int Total {get; set;}
public Link<Customer> Customer {get; set;}
}
public class Customer
{
public Guid Id { get; protected set;}
public string Name {get; set;}
}
public class Link<T>
{
... Snip ...
public T Entity { get; private set;}
}
In the order table, the "Customer" column is a standard foreign key.
this means that in the entire system, any work with these "entities" takes the following form:
if (Order.Customer != null)
{
if (Order.Customer.Entity != null)
{
// Do stuff
}
}
After much research, I couldn't find a method to map these classes in a way that will allow sane usage of nhibernate's querying capabilities. The closest solution I found is the IPropertyAccessor interface, which will possibly solve my mapping problems, but won't play nice with queries.
A few notes: 1) Unfortunately, the main problem of the Link wrapper classes is here to stay, I can't remove it without rewriting the entire legacy infrastructure. 2) I Have full access to the code-generation templates, and can change the entity classes structure as I see fit. For example, to fix a different major issue I generated a poco-like interface for each entity: public interface IOrder { public ICustomer Customer {get; set;} ... }
public class Order : IOrder
{
...
public Link<Customer> Customer {get; set;}
ICustomer IOrder.Customer
{
(Adapting code here)
}
...
}
In short: any tips from NH gurus will be greatly appreciated.