views:

49

answers:

2

Is it possible to perform a global reversed-find on NHibernate-managed objects?

Specifically, I have a persistent class called "Io". There are a huge number of fields across multiple tables which can potentially contain an object of that type. Is there a way (given a specific instance of an Io object), to retrieve a list of objects (of any type) that actually do reference that specific object? (Bonus points if it can identify which specific fields actually contain the reference, but that's not critical.)

Since the NHibernate mappings define all the links (and the underlying database has corresponding foreign key links), there ought to be some way to do it.

Imagine this sort of structure:

class Io
{
  public int Id { get; set; }
  // other fields specific to the Io type
}

class ThingOne
{
  public int Id { get; set; }
  public Io SensorInput { get; set; }
  public Io SolenoidOutput { get; set; }
  // other stuff
}

class ThingTwo
{
  public int Id { get; set; }
  public Io SensorInput1 { get; set; }
  public Io SensorInput2 { get; set; }
  public SubThing Doohickey { get; set; }
  // ...
}

class SubThing
{
  public int Id { get; set; }
  public Io ControlOutput1 { get; set; }
  // ...
}

Given a specific instance of Io, I want to discover that it's referenced by the ThingTwo with id 12. Or that it's referenced by that and also by the ThingOne with id 16. If possible, also that the first reference is via SensorInput2, for example.

A: 

Doesn't your "lo" class contain a reference to the object(s) that contain the "lo" object(s)?

i.e. if "Lo" is reference by some number of "Hi" objects:

public class Lo
{
    List<Hi> hiObjects;
}

Now if you have an instance of "Lo":

Lo lo = new Lo();
List<Hi> hiObjects = lo.hiObjects;

If you do not have these type of references, you could add them.

Kevin Crowell
Only if there's a way for NHibernate to fill that information in for me. It definitely doesn't belong in the persisted state -- this is information recovered as part of building the object graph. I've expanded my question with an example which hopefully explains things better.
Miral
Also, at current count there are about 20 different types that might contain an Io. So I really don't want to have to explicitly list them anywhere.
Miral
+2  A: 

Well the configuration mappings don't seem to expose the FK relationship so for the time being some reflection can find which object types reference this. Note that the code below assumes that you have all nhibernate-mapped classes to a single assembly and also uses C# 3.0 and above for the LINQ support.

IO toSearch = nhSession.Get<IO>(5);
var assembly = Assembly.Load("EntityAssembly");
IList<Type> assemblyTypes = assembly.GetTypes();
var searchType = toSearch.GetType();
var typesThatContainedSearchTypeProperty =
    assemblyTypes.Where(
    ast => ast.GetProperties().Count() > 0 &&
    ast.GetProperties().Where(
        astp => astp.PropertyType != null && astp.PropertyType == searchType).Count() > 0);

Now if you also want to get the objects that contain this particular instance of IO you can have a nice MultiCriteria to do it in a single round trip.

var multiCrit = nhSession.CreateMultiCriteria();

foreach (var type in typesThatContainedSearchTypeProperty)
{
    //maybe this class has multiple properties of the same Type
    foreach (PropertyInfo pi in type.GetProperties().Where(astp => astp.PropertyType == toSearch.GetType()))
        multiCrit.Add(nhSession.CreateCriteria(type).Add(Restrictions.Eq(pi.Name, toSearch)));
}
IList results = multiCrit.List();

as you can guess, since we begin with reflection we can only end with reflection. The results list is an array with each entry the result of each criteria, where each criteria search may be a single result or a list of results.

Jaguar
At first this gave me some trouble, but that turned out to be an error in my hbm mappings. After flattening the result into a single list, this does exactly what I was after. Thanks!
Miral