Gang,
I've got a database scenario that I'm not sure how to map into
NHibernate. I've gone through a variety of tutorials but am still
quite new to NHibernate, particularly with mapping and non-standard
scenarios. I'm currently working through Manning's "NHibernate in
Action", which is great. This is a greenfield .NET project on top of
an existing database whose schema CAN be changed, if needed.
The actual domain would be lengthy to describe, but a good analog is a
query builder where a query request has a "filter group" that contains
all the filters for that query. Each element in that filter group can
either be a filter or a nested filter group. By recursing down into
any nested filter groups, you could build the entire filter set for
the request.
In SQL terms, think of a WHERE clause:
WHERE (c1 AND c2 AND (c3 OR c4))
... the theoretical object graph in this case would be a
DataRequest.FilterCriteria member (the entire WHERE clause) that
contains three members: a Filter (c1), another Filter (c2), and a
nested FilterGroup (which itself contains two Filters, c3 and c4).
In relational terms, we currently have a table structure that looks
like this:
* The table for query requests has a FilterGroupID foreign key to
the FilterGroup table (representing the top level of the request - the
WHERE clause).
* The FilterGroup table has a ParentFilterGroupID foreign key to
itself (self-join) so that any nested FilterGroups know which parent
FilterGroup they belong to.
* There is a Filter table that has the distinct filters for the
request (in this case, a row each for c1/c2/c3/c4).
* There is a N:N mapping table between FilterGroup and Filter (as
filters may be reused across multiple filter groups).
So the above filter example would have two FilterGroup rows (the
overall WHERE clause, plus the nested group that has a parent ID for
the overall clause)... plus four Filter rows (c1/c2/c3/c4)... plus
four rows in the mapping table (c1 and c2 mapped to the parent
FilterGroup, and c3/c4 mapped to the nested FilterGroup).
>From a relational perspective, this seems to make sense and is
workable (again, I'm simplifying the domain problem here). However,
the MAPPING of this to NHibernate entities has us confused. Ideally, I
think the entities look like this (simplified):
interface IFilter {
CriteriaWidget GetFilterCriteria();
}
class DataRequest {
IFilter FilterCriteria {get;set;}
}
class FilterGroup : IFilter {
List<IFilter> Filters {get;set;} // elements are either Filters or
nested FilterGroups
CriteriaWidget GetFilterCriteria() {
// return combined criteria for all filters in the list
}
}
class Filter : IFilter {
CriteriaWidget GetFilterCriteria() {
// return criteria for just this instance
}
}
Given that the List<IFilter> in a FilterGroup can contain either a
Filter instance or a nested FilterGroup, how should the entities be
mapped to the database here? It seems as if the filter group's
List<IFilter> needs to be populated in two steps -- first via the self-
join to any nested filter groups, and then to any filters mapped to
the filter group.
Again, this project is relatively green-field, so if this can be
simplified a bit at the relational level or with a different approach
to entity types, let me know. I believe the main problem we're trying
to solve here is the notion of a relational model with a table that
maps to both a foreign table and back to itself -- but either type of
mapping appears in a single collection in the object model.
Thanks for any assistance you can provide,
JD
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.