asp.net web api - NHibernate returning results from Web API -


i'm using nhibernate fetch collection has lazy loaded properties having trouble returning serializer tries serialize lazy property after nhibernate session closed. there way tell nhibernate give me true list in if there unloaded lazy collections leave them empty?

for example

ienumerable<store> stores = storeservice.getlist(1, 2); 

store has one-to-many mapping stockitems set lazy load causes serialization error. tried

list<store> stores_r = stores.tolist(); 

but same thing. there traverses through list , fetches one-to-one relations , ignores one-to-many lazy loading , return finished list?

thanks

edit:solution i've tried still not working

public class nhibernatecontractresolver: defaultcontractresolver     {     protected override jsoncontract createcontract(type objecttype)     {         if (typeof(nhibernate.proxy.inhibernateproxy).isassignablefrom(objecttype) || typeof(nhibernate.proxy.ilazyinitializer).isassignablefrom(objecttype))         {             var otype = objecttype.getinterfaces().firstordefault(i => i.fullname.startswith("navace.models"));             return otype != null ? base.createcontract(otype) : base.createcontract(objecttype.basetype);         }          return base.createcontract(objecttype);     }      protected override list<memberinfo> getserializablemembers(type objecttype)     {         if (typeof(nhibernate.proxy.inhibernateproxy).isassignablefrom(objecttype))         {             return base.getserializablemembers(objecttype.basetype);         }         else         {             return base.getserializablemembers(objecttype);         }     } } 

try manually serialize can use what's happening

ienumerable<store> stores = storeservice.getlist(1, 2);  store> storess = stores.tolist();  jsonserializer sr = new jsonserializer             {                 referenceloophandling = referenceloophandling.ignore,                 contractresolver = new nhibernatecontractresolver(),                 nullvaluehandling = nullvaluehandling.ignore,             };  stringwriter stringwriter = new stringwriter(); jsonwriter jsonwriter = new newtonsoft.json.jsontextwriter(stringwriter); sr.serialize(jsonwriter, storess);  string res = stringwriter.tostring(); 

the error is

outer exception : error getting value 'datedcost' on 'partproxy'.

inner exception: no row given identifier exists[navace.models.part#0]

my recommendation return view models instead of domain models. it's confusing return empty collection property when may have data. converting domain model view model (using linq select or automapper), serializer touch (and attempt lazy load) properties in view model.


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -