开发者

Clone LINQ To SQL object Extension Method throws object dispose exception

I have this extension method for cloning my LINQ To SQL objects:

public static T CloneObjectGraph<T>(this T obj) where T : class 
{
    var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
    using (var ms = new System.IO.MemoryStream())
    {
        serializer.WriteObject(ms, obj);
        ms.Position = 0;
        return (T)serializer.ReadObject(ms);
    }
}

But while i carry objects with not all references loaded, while qyuerying with DataLoadOptions, sometimes it throws the object disposed exception, but thing is I 开发者_开发技巧don't ask for references that is not loaded (null).

e.g. I have Customer with many references and i just need to carry on memory the Address reference EntityRef<> and i don't Load anything else. But while i clone the object this exception forces me to load all the EntitySet<> references with the Customer object, which might be too much and slow down the application speed.

Any suggestions?


In my experience it is best to keep away from serializing LINQ to SQL objects if possible. Rather use Data Transfer Objects (DTO). They will simply contain data and no hard to serialize references and hidden connections to a DataContext. This way it is easy to serialize them and only serialize the stuff you need to be serialized.


Suppose you have a Customer, with some Orders.

  • If you use LoadOptions.LoadWith<Customer>(c => c.Orders), your get a Customer and a populated Customer.Orders EntitySet.
  • If you don't, you get a Customer and a lazy loadable Customer.Orders EntitySet. Any attempt to enumerate the Orders property will cause the DataContext to load this customer's orders.

Now that you have the Customer, you dispose the DataContext as should happen (preferable with a using statment).

And some time after that, you serialize. The serialization code enumerates the Orders property. The DataContext that would load those Orders is gone, and you get the error.

Check out EntitySet.IsDeferred and EntitySet.HasLoadedOrAssignedValues.


I'm not 100% sure of the solution, but I'd try something heavy handed like:

if (!c.Orders.HasLoadedOrAssignedValues)
{
  c.Orders = null;
}

as a pre-step to serialization. Any lazy EntitySets must be disconnected from the Customers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜