0

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

Community
  • 1
  • 1
  • Hi @Gabriel, welcome to SO. Share the relevant parts of your code trying to point *where* the problem is and/or *what* result you expect. – Marcelo De Zen Aug 25 '12 at 21:20
  • Did you used google translator? This is very hard to understand... not sure what you really want except that you want to serialize and deserialize something from nhibernate to json and has a xml somewhere in middle... try to clarify please. – Marcelo De Zen Aug 25 '12 at 21:25
  • Sorry guys I already correctly the info so i hope it is more easy to understand ehat is the problem – Gabriel Monteiro Nepomuceno Aug 25 '12 at 22:21

1 Answers1

1

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.

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • The problem is with the children elements and that the entity itself is HUGE and if I create a dto object if I change the original object i would have to change it too – Gabriel Monteiro Nepomuceno Aug 26 '12 at 13:07
  • AFAIK this is only way as you have already found out by running into recursive calls with your NH Poco. – Rippo Aug 26 '12 at 14:06
  • I am not sure but I bet the stack overflow is caused by bi-directional one-to-many relationhips. Can you not mark what you want to be serialized on the class level? – Rippo Aug 26 '12 at 17:51
  • I started trying this but i'm talking about a order that have something like 300 properties witch 100 of than are other entities or collection of entities and I don't know if this will work but I will try – Gabriel Monteiro Nepomuceno Aug 26 '12 at 21:38
  • In that case I would flatten the model into a two db views, one for entity and one for children and map these with no relationships. Then simply serialise both. You will have to change code in two places if the mode changes but that is something you will have to live with. – Rippo Sep 01 '12 at 06:56