The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Serialize before Load() with Query set, deserialze after?

Last post 03-20-2008, 10:01 AM by Scoutn. 5 replies.
Sort Posts: Previous Next
  •  03-19-2008, 9:03 PM 8473

    Serialize before Load() with Query set, deserialze after?

    I am not sure if I should post this here or in the webservices forum, but here goes anyway. I am trying to implement webservices between the admin grids and the db server, and I am having a little issue. My basic question is this; is it possible to serialize an entity that has had it's Query set, pass it to a webservice, deserialize it, call load, serialize again, pass it back to the consumer, and deserialize that into an entity? I've tried and I get the "An Entity can only hold 1 record of data" error. Should I be looking at something along these lines instead? I've tried serializing and deserializing bypassing the webservice and I get the same result.

     

    Code:
            try
            {
                ExclusionLists obj = new ExclusionLists();
                obj.Query.Where(obj.Query.Id == this.Id);
                ExclusionListsProxyStub proxy = new ExclusionListsProxyStub(obj);
                string data = Utility.SerializeProxy(proxy);
                proxy = (ExclusionListsProxyStub)Utility.DeserializeProxy(data, typeof(ExclusionListsProxyStub));            
                obj = proxy.entity;
                obj.Query.Load();
                return obj;
            }
            catch (Exception)
            {
                throw;
            }
     
  •  03-19-2008, 11:50 PM 8474 in reply to 8473

    Re: Serialize before Load() with Query set, deserialze after?

    Aren't you mixing scenarios? Maybe I am wrong, but if you want to return a collection, then shouldn't you build a query based on a collection (ExclusionListsCollection version ExclusionLists)?

    ... just a thought.

     

  •  03-20-2008, 3:46 AM 8475 in reply to 8473

    Re: Serialize before Load() with Query set, deserialze after?

    In order to serialize/deserialize the query, you must use binary serialization directly on "obj", not XML string serialization on its proxy. Without it, obj.Query.Load() has no where condition, and is defaulting to SELECT *. If you look at "string data", it does not contain any XML for the query. Checking obj.Query.es.LastQuery after obj.Query.Load() should confirm the SELECT *.
    David Neal Parsons
    www.entityspaces.net
  •  03-20-2008, 4:39 AM 8476 in reply to 8475

    Re: Serialize before Load() with Query set, deserialze after?

    Yes, you must first load the true entity before using it with a proxy, the order is all wrong in your code. Cool avatar by the way ...

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-20-2008, 4:45 AM 8477 in reply to 8476

    Re: Serialize before Load() with Query set, deserialze after?

    A complete test:

    Code:
    // Client-side query to load an Employee
    Employee emp = new Employee();
    emp.Query.Where(emp.Query.EmployeeID == 1);
    
    // Use binary serialization to include the query
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, emp);
    byte[] query = ms.ToArray();
    
    // Send it to the server over the wire
    
    // Server-side deserialize
    bf = new BinaryFormatter();
    ms = new MemoryStream(query);
    Employee newEmp = bf.Deserialize(ms) as Employee;
    
    // Now load it 
    Assert.IsTrue(newEmp.Query.Load());
    Assert.AreEqual(1, newEmp.EmployeeID.Value);
    
    //  Server-side Create a Proxy and Serialize to XML string
    EmployeeProxyStub proxy =
        new EmployeeProxyStub(newEmp);
    
    XmlSerializer sf =
        new XmlSerializer(typeof(EmployeeProxyStub));
    StringWriter sw = new StringWriter();
    sf.Serialize(sw, proxy);
    
    string packet = sw.ToString();
    
    // Send it back to the client over the wire
    
    // Client-side Proxy XML string deserialize
    XmlSerializer xs =
        new XmlSerializer(typeof(EmployeeProxyStub));
    StringReader sr = new StringReader(packet);
    
    proxy = xs.Deserialize(sr) as EmployeeProxyStub;
    emp = proxy.Entity;
    
    Assert.AreEqual("Unchanged", emp.es.RowState.ToString());
    Assert.AreEqual(1, emp.EmployeeID.Value);

    David Neal Parsons
    www.entityspaces.net
  •  03-20-2008, 10:01 AM 8492 in reply to 8477

    Re: Serialize before Load() with Query set, deserialze after?

    David, thanks. What you posted is exactly what I was looking to do; I failed to use binary serialization on the object. Makes perfect sense. Thanks for the example!
View as RSS news feed in XML