clone entity

Posts   
 
    
heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 04-Jan-2011 03:57:59   

private static IEntity2 DoCloneEntity(IEntity2 sourceEntity) { IEntity2 clonedEntity = null; BinaryFormatter formatter = new BinaryFormatter();

        using (MemoryStream memStream = new MemoryStream())
        {
            formatter.Serialize(memStream, sourceEntity);
            memStream.Seek(0, SeekOrigin.Begin);
            clonedEntity = (IEntity2)formatter.Deserialize(memStream);
        }

        return clonedEntity;
    }

this is my code ,It took half an hour to do it. i have13,000 rows child entity in source entity . how can i do it fast. i have set SerializationHelper.Optimization = SerializationOptimization.Fast;

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Jan-2011 05:29:24   

Mmm, given the amount of entities I think that is kind of expected, even so it's a lot of time. You can add more speed setting to false SerializationHelper.PreserveObjectIDs.

David Elizondo | LLBLGen Support Team
heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 04-Jan-2011 06:39:39   

Only this way to do this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 04-Jan-2011 09:26:16   

Please check what takes the time in your code (with a profiler, do this with 10, or 20 entities, as profiling is slower than normal execution), as half an hour for 13,000 entities is very slow. Fast serialization should serialize those 13,000 entities within a couple of seconds if not faster. Also make sure you're indeed using 13,000 entities and not many many more.

Keep in mind that serialization is recursive. So if you serialize 1 entity which can reach all of them (through references, e.g. you serialize myOrder, which references myCustomer, which references 20 other Orderentity instances, you'll serialize all of them!), you serialize them all when you think you serialize just 1 entity.

Frans Bouma | Lead developer LLBLGen Pro
heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 04-Jan-2011 10:01:18   

sorry ,it has 130,000 entities ,not 13,000

so ,i want to use entity.writexml method,after then readxml.

string fileName = sourceEntity.LLBLGenProEntityName + ".xml"; string entityXml = string.Empty; sourceEntity.WriteXml(out entityXml); StreamWriter streamWriter = new StreamWriter(fileName, false); streamWriter.Write(entityXml); streamWriter.Flush();

        if (streamWriter != null)
        {
            streamWriter.Close();
            streamWriter.Dispose();
        }

        StreamReader streamReader = new StreamReader(fileName);
        entityXml = streamReader.ReadToEnd();

        streamReader = new StreamReader(fileName);
        entityXml = streamReader.ReadToEnd();

        IEntity2 clonedEntity = Shared.GetEntityByName(sourceEntity.LLBLGenProEntityName);
        clonedEntity.ReadXml(entityXml);

        if (streamReader != null)
        {
            streamReader.Close();
            streamReader.Dispose();
        }

but WriteXml(out entityXml) it throw the OutOfMemoryException.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jan-2011 10:58:11   

What about using a profiler with the memoryStream way? (edit) Also which runtime library version (build number) are you using?

heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 04-Jan-2011 11:40:02   

--------->What about using a profiler with the memoryStream way?

how can i do it?

my library version is 2.6.10.421

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jan-2011 11:52:25   

These are 3rd party tools, like ANTS profiler.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 04-Jan-2011 12:47:45   

heyu52 wrote:

--------->What about using a profiler with the memoryStream way?

how can i do it?

my library version is 2.6.10.421

Also, please read what I wrote. You didn't give any code how you call it, nor did you give information about how these 130,000 entities are stored (in a collection, in a graph, are they referencing others? etc.).

If you use xml (which is much slower), use compact25 mode.

Frans Bouma | Lead developer LLBLGen Pro
heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 04-Jan-2011 15:53:14   

Do you speak Chinese? I can speak a little English.

This is all my fault.

I mistook your meaning .

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 04-Jan-2011 21:15:59   

Does that mean we can close this thread, or do you still have an outstanding query?

Matt

heyu52
User
Posts: 21
Joined: 27-Apr-2010
# Posted on: 05-Jan-2011 02:41:40   

please close it