开发者

NHibernate: Mapping different dynamic components based on a discriminator

开发者 https://www.devze.com 2022-12-27 14:59 出处:网络
My domain entities each have a set of \"fixed\" properties and a set of \"dynamic\" properties which can be added at runtime.I handle this by using NHibernate\'s dynamic-component functionality.

My domain entities each have a set of "fixed" properties and a set of "dynamic" properties which can be added at runtime. I handle this by using NHibernate's dynamic-component functionality.

public class Product {
  public virtual Guid Id { get; }
  public virtual string Name { get; set;}
  public virtual IDictionary DynamicComponents { get; }
}

Now I have the following situation

public class Customer {
  public virtual Guid Id { get; }
  public virtual string Type { get; set;}
  public virtual IDictionary DynamicProperties { 开发者_StackOverflowget; }
}

Where a CustomerType is something like "Online" or "InPerson". Furthermore an Online customer has dynamic properties "Name" and "IPAddress" and an InPerson Customer has dynamic properties "Name" and "Salesman".

Which customer types are available and the extra properties on them are configured in meta-data which is used to generate hbm files on application start.

I could figure out some way to knock this together using an intermediate DTO layer, but is there any support in NHibernate for this scenario? The only difficulty seems to be that all the different "types" of customer map to the same Customer class.


Maybe a stupid question, but why don't you just use two subclasses of Customer?

Other than that it is not immediately clear to me what it is you want NHibernate to support. Can you clarify what "any support in NHibernate for this scenario" means, what do you want NHibernate to do for you?


I think you can find a solution using the dynamic properties... in your subclasses, refer to the dynamic properties such as IPAddress { get { return DynamicProperties["ipAddress"] as IPAddress; } }

Interesting part for me is how do you map these properties in such a way that is scalable...

Did you come up with a different solution?

0

精彩评论

暂无评论...
验证码 换一张
取 消