Global object references in NHibernate

Can I perform a global reverse check on objects managed by NHibernate?

In particular, I have a permanent class called "Io". There are a huge number of fields in several tables that could potentially contain an object of this type. Is there a way (based on a specific instance of an Io object) to get a list of objects (of any type) that really reference this particular object? (Bonus points, if he can determine which specific fields really contain the link, but this is not critical.)

Since NHibernate mappings define all references (and the underlying database has corresponding foreign key references), this should be some .

Imagine this 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; }
  // ...
}

Io, , ThingTwo 12. , ThingOne 16. , , - SensorInput2, .

+5
2

, , , FK, - , . , , nhibernate- , # 3.0 LINQ.

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);

, , -, MultiCriteria, .

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();

, , . - , , .

+3

"lo" (), "lo"?

. "Lo" "Hi":

public class Lo
{
    List<Hi> hiObjects;
}

, "Lo":

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

, .

0

All Articles