Reputation: 1244
I have a problem I'm already trying to solve for some time.
The scenario is as follows
The person will make an insertion of a new entity in the database.
To begin insertion is necessary that I make a costly transformation from an xml file to fill some basic properties.
I wanted to process this xml before and store the object already transformed as o object in some short of cache.
After that, the process of filling the other properties of this object is very time consuming and I want the person could stop in the middle and back to end it after. however I can not save this object in the database incomplete, due to some validations that are made in the database when this object is inserted.
The solution I had tidied involved serialize the object when the person stop filling it, even if it is not done yet,than when the person come back to continue filling i would deserialize the object so the person could continue.
The problem is that when i try to serialize this object nihibernate serialize almost the entire database because of the lazy load and i get stackoverlow error ( lol it is funny to say this error here lol ).
Does anyone have any idea what I should do?
One of the things I had done was to keep the object in the cache (don't know how the /net 4 cache serialized it but seams to work) however everytime I retrieve the object it loses half of children objects, and always exactly half the children ....
if is live I have a order with 20 items if i try to get it back from the cache it will come back with 10 items if i try to retrieve it again I would got the order with only 5 itens.
Does anyone have any idea how to solve this?
already tries there
NHibernate serializing lazy-loaded entities with WCF
Serialize nHibernate query to JSON
Upvotes: 0
Views: 1958
Reputation: 22424
One way is to project what you do want first then serialise the result into a DTO.
However by doing this you run into an issue where you are creating DTO's with lots of setters/getters. Wouldn't it be easier to create a simple anonymous type and serialise that instead? I have written a blog post that explains this.
//first create our anonymous type DTO
var dto = new {
Id = 0L,
Source = string.Empty,
Destination = string.Empty,
Is301 = false
};
//notice the ListAs(dto) extension method
var model = Session.QueryOver<CmsRedirect>()
.SelectList(s => s
.Select(x => x.Id).WithAlias(() => dto.Id)
.Select(x => x.Source).WithAlias(() => dto.Source)
.Select(x => x.Destination).WithAlias(() => dto.Destination)
.Select(x => x.Do301).WithAlias(() => dto.Is301)
)
.Take(take).Skip(page * pageSize)
.ListAs(dto);
return Json(new { Total = total, List = model },
JsonRequestBehavior.AllowGet);
ListAs is a simple(ish) extension method. (credit goes to Filip Kinský)
public static class NHibernateExtensions {
public static IList<TRes> ListAs<TRes>(
this IQueryOver qry, TRes resultByExample) {
var ctor = typeof(TRes).GetConstructors().First();
return qry.UnderlyingCriteria
.SetResultTransformer(
Transformers.AliasToBeanConstructor(
(ConstructorInfo) ctor)
).List<TRes>();
}
}
But be warned in my example I do not serialise children collections so you may need to handle this manually yourself.
Upvotes: 1