Pergunta

I don't know whether it's my mistake or a bug in Devexpres XPO? (Version 12.1.5)
take a look at the following sample:

static void Main(string[] args)
    {
        var dxs = Session.DefaultSession;
        var sw = new Stopwatch();

        using (var uow = dxs.BeginNestedUnitOfWork())
        {
            var dbp = new DBParent(uow) { TitleXX = "Morgan" };

            // add 1000 child to the parent table
            for (var i = 0; i < 1000; i++)
            {
                var dbc = new DBChild(uow)
                              {
                                  Name = string.Format("Child {0}", i),
                                  Parent = dbp
                              };
            }

            var count = uow.GetObjectsToSave();
            // count = 1001
            sw.Start();
            uow.CommitChanges();
            sw.Stop();
            Console.WriteLine("Time:" +sw.Elapsed);
            // Takes about 7 sec
        }


        using (var uow = dxs.BeginNestedUnitOfWork())
        {
            var dbp = new XPCollection<DBParent>(uow).First();
            dbp.TitleXX = "Another title";
            dbp.Save();

            var count = uow.GetObjectsToSave();
            // count = 1
            sw.Reset();
            sw.Start();
            uow.CommitChanges();
            sw.Stop();
            Console.WriteLine("Time:" + sw.Elapsed); // Takes about 4 sec ????
        }
        Console.ReadLine();
    }
}

and here are my objects :

public class DBParent : XPObject
{
    public DBParent(){}
    public DBParent(Session session) : base(session) { }

    private string _TitleXX;
    public string TitleXX
    {
        get { return _TitleXX; }
        set { SetPropertyValue("TitleXX", ref _TitleXX, value); }
    }

    [Association("a1"), Aggregated]
    public XPCollection<DBChild> Childs
    {
        get
        {
            return GetCollection<DBChild>("Childs");                
        }
    }      
}


public class DBChild : XPObject
{
    public DBChild(){}
    public DBChild(Session session): base(session){}

    private string _Name;       
    public string Name
    {
        get { return _Name; }
        set { SetPropertyValue("Name", ref _Name, value); }
    }

    private DBParent _Parent;
    [Association("a1")]
    public DBParent Parent
    {
        get { return _Parent; }
        set { SetPropertyValue("Parent", ref _Parent, value); }
    }       
}

as you can see , saving 1001 ( 1000 child + 1 parent ) takes 7 seconds , and in the next block updating 1 parent object is taking 4 seconds. i have tested against MS Access and MS SQL 2008 and MSSQL-Compact , but all have the same result. any advices are appreciated.

Foi útil?

Solução

This seems like a reasonable length of time for 1001 discrete Inserts. The objects are held in memory until you call CommitChanges at which point they are written to the database. I haven't profiled the SQL that XPO generates, but I wouldn't be surprised if each insert took place inside its own implicit transaction.

EDIT

I've compiled your code with the tracing suggested by Filip, and the second NestedUnitOfWork is actually updating all of the children, even though there's no work to do (this is in version 12.1.7). I'm not sure if this is by design, but it does seem like a bug to me.

To avoid updating the children unnecessarily, you can just instantiate a plain UnitOfWork:

using (var uow = new UnitOfWork())
{
    //...
}

This will only update the parent object. But be careful: you will still need a NestedUnitOfWork for updates that will affect the child objects.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top