C# NHibernate - Remove all references to object on delete -


i have 2 objects. one, parent, references locale. locale list of locales. when locale deleted, want clean references referencing types (setting relevant value null).

right now, have system walks across entities nhibernate mapping and, using class metadata, determines types reference locale type. then, build query (using icriteria) referencing type property of type locale equals locale's id i'm trying delete. objects come back, set property null , update them.

question: there better way - using built nhibernate - instruct object remove references on delete?

objects:

public class parent {     public virtual guid id { get; set; }     public virtual locale loc { get; set; } }  public class locale {     public virtual guid id { get; set; } } 

mappings:

public class parentmapping : classmap<parent> {     id(x => x.id).generatedby.guid();      references(x => x.loc).nullable(); }  public class localemapping : classmap<locale> {     id(x => x.id).generatedby.guid(); } 

as requested, here's how wound dealing problem. used suggestion given @fran come solution.

solution

this solution specific type of application , involves using number of parts of application working achieve desired result. specifically, application restful web service, powered wcf, json.net, , nhibernate.

first, added reference parents in locale , used hasmany mapping, locale knew of parents reference it:

public virtual ilist<parent> parents { get; set; } 

and

hasmany(x => x.parents); 

it's important point out here use lazy loading throughout application.

while allowed me delete locale using proper cascade behaviors, posed problem in loading/get scenarios in when passed locale json.net (on way out door client), json.net walk parents collection, , serialize whole thing. obviously, undesired we're feeding client more asked for. problem alluded in comment in op.

as @fran mentioned, use projections; however, of reference lists accessed through common endpoint in order abstract crud operations , reduce amount of repeated code: of reference lists implement abstract class called referencelistbase. anyways, wanted solution in implementing class able decide how of should sent client (serialized).

my solution put [jsonignore] attribute on parents collection, which, in conjuction lazy loading, means json.net never looks @ property , therefore, relationship never gets loaded.

this solution has kind of felt hack, has achieved of results want , made adding new reference lists easy. hope helps you; if doesn't, post new question, link here, , i'll try out. :)


Comments