I have a datatable with the following structure:
MyTable
-------------------------------------
rule_name char(20) PK
value_type char(20) PK
value varchar(50) not null
dt_last_update datetime not null
Among the data represented in this table, I have a rule_name
of interest:
MailboxSC
With which I have two distinct properties or call it value_type
:
numberOfEmails
dateTimeReceived
Over which I have no control nor any power.
Using DynamicObject, I have followed what I could from Ayende Rahien:
Support dynamic fields with NHibernate and .NET 4.0
So now, my model.
EmailIndicator
public class EmailIndicator {
public EmailIndicator(HashtableDynamicObject hastableDynamicObject) {
this.hashtableDynamicObject = hashtableDynamicObject;
}
public virtual dynamic Attributes { get { return hashtableDynamicObject; } }
private readonly HashtableDynamicObject hashtableDynamicObject;
}
HashtableDynamicObject
public class HashtableDynamicObject : DynamicObject {
public HashtableDynamicObject(IDictionary dictionary) {
this.dictionary = dictionary;
}
public override bool TryGetMember(GetMemberBinder binder
, object[] indexes
, out object result) {
result = dictionary[binder.Name];
return dictionary.Contains(binder.Name);
}
public override bool TrySetMember(SetMemberBinder binder
, object[] indexes
, object value) {
dictionary[binder.Name] = value;
return dictionary.Contains(binder.Name);
}
public override bool TryGetIndex(GetIndexBinder binder
, object[] indexes
, out object result) {
result = dictionary[indexes[0]];
return dictionary.Contains(indexes[0]);
}
public override bool TrySetIndex(SetIndexBinder binder
, object[] indexes
, object value) {
dictionary[indexes[0]] = value;
return dictionary.Contains(indexes[0]);
}
private readonly IDictionary dictionary;
}
Now, I'd like to map dynamic properties to each row of rule_name
MailboxSC
so that:
declare @emailCount int
declare @oldestDateTimeReceived datetime
select (
select valeur
from bqt001_pilotage_indicateurs
where nom_regle like 'boiteCourrielSC'
and type_valeur like 'nombreCourriels'
) as emailCount
, (
select valeur as olsdestDateTimeReceived
from bqt001_pilotage_indicateurs
where nom_regle like 'boiteCourrielSC'
and type_valeur like 'dateHeurePlusVieux'
) as oldestDateTimeReceived
into @emailCount
, @oldestDateTimeReceived
So I get my required values, the number of emails and the oldest date/time received.
How to map it using either classic NHibernate XML mapping or Fluent NHibernate?
Linked Ayende Rahien's article provides with a first approach:
<class name="Customer"
table="Customers">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Name" />
<join table="Customers_Extensions" optional="false">
<key column="CustomerId"/>
<dynamic-component name="Attributes" access="field.lowercase">
<property name="EyeColor" type="System.String"/>
</dynamic-component>
</join>
</class>
So my guess would be:
<class name="EmailIndicatorModel">
<join table="MyTable" optional="false">
<dynamic-component name="Attributes" access="field.lowercase">
<property name="EmailCount" type="System.Int32" />
<property name="OldestDateTimeReceived" type="System.DateTime" />
<dynamic-component>
</join>
</class>
And then again, this doesn't allow me to mention the discriminator columns rule_name
and value_type
for each of the properties.
Am I condemned to use old-style ADO.NET?
Here are some interesting articles regarding what interests me:
- dynamic-component fluent automapping
- Mapping-by-Code - dynamic component
- NHibernate mapping class properties to Rows not Columns (This article led me to Ayende Rahien's article)