Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global find object references in NHibernate

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.

like image 716
Miral Avatar asked Mar 09 '10 00:03

Miral


1 Answers

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.

like image 78
Jaguar Avatar answered Nov 12 '22 16:11

Jaguar