Example #1
0
        public List <T> Commit(TT pars = default(TT))
        {
            Dictionary <T, object> savedobjects = new Dictionary <T, object>();

            if (_delete_not_updated)
            {
                foreach (T t in _originalItems.Keys.ToList())
                {
                    if (_references.ContainsKey(t))
                    {
                        _originalItems.Remove(t);
                    }
                }

                if (_originalItems.Keys.Count > 0)
                {
                    foreach (T e in _originalItems.Values)
                    {
                        savedobjects[e] = _repo.BeginDelete(e, pars);
                    }
                    ShokoContext ctx = _repo.Provider.GetContext();
                    ctx.AttachRange(_originalItems.Values);
                    ctx.RemoveRange(_originalItems.Values);
                    ctx.SaveChanges();
                    if (_repo.IsCached)
                    {
                        _originalItems.Values.ForEach(_repo.Cache.Remove);
                    }
                    foreach (T e in _originalItems.Values)
                    {
                        _repo.EndDelete(e, savedobjects[e], pars);
                    }
                }
            }
            savedobjects.Clear();

            List <T> returns = new List <T>();

            if (_references.Count > 0)
            {
                foreach (T t in _references.Keys)
                {
                    savedobjects[t] = _repo.BeginSave(t, _references[t], pars);
                }

                var updates = _references.Where(a => a.Value != null).ToList();
                var creates = _references.Where(a => a.Value == null).ToList();
                using (_repo.RepoLock.WriterLock())
                {
                    ShokoContext ctx = _repo.Provider.GetContext();
                    foreach (KeyValuePair <T, T> r in updates)
                    {
                        ctx.UpdateChanges(r.Value, r.Key);

                        /*
                         *
                         * r.Key.DeepCloneTo(r.Value); //Tried to be 100% atomic and failed miserably, so is 99%.
                         * //If we replace Original with Entity in cache (updating with 'this' as the model to update, will not get the changes).
                         * //So this is the best effort
                         * ctx.Attach(r.Value);
                         * ctx.Update(r.Value);*/
                        returns.Add(r.Value);
                    }

                    foreach (KeyValuePair <T, T> r in creates)
                    {
                        ctx.Add(r.Key);
                        returns.Add(r.Key);
                    }

                    if (_repo.IsCached)
                    {
                        returns.ForEach(_repo.Cache.Update);
                    }
                    ctx.SaveChanges();
                    ctx.DetachRange(returns);
                }

                // At least the current references will work with this.
                foreach (T t in savedobjects.Keys)
                {
                    if (savedobjects.ContainsKey(t))
                    {
                        _repo.EndSave(t, savedobjects[t], pars);
                    }
                }
            }

            return(returns);
        }
Example #2
0
        public List <T> Commit(TT pars = default(TT))
        {
            Dictionary <T, object> savedobjects = new Dictionary <T, object>();

            if (_originalsKeys.Count > 0)
            {
                List <T> listed = _repo.GetMany(_originalsKeys);
                foreach (T e in listed)
                {
                    savedobjects[e] = _repo.BeginDelete(e, pars);
                }
                using (_repo.RepoLock.ReaderLock())
                {
                    _repo.Table.RemoveRange(listed);
                    _repo.Context.SaveChanges();
                    if (_repo.IsCached)
                    {
                        listed.ForEach(_repo.Cache.Remove);
                    }
                }

                foreach (T e in listed)
                {
                    _repo.EndDelete(e, savedobjects[e], pars);
                }
            }
            List <T> returns = new List <T>();

            if (References.Count > 0)
            {
                Dictionary <T, object> savedObjects = new Dictionary <T, object>();
                foreach (T t in EntityList)
                {
                    savedObjects[t] = _repo.BeginSave(t, References[t], pars);
                }
                var updates = References.Where(a => a.Value != null).ToList();
                var creates = References.Where(a => a.Value == null).ToList();
                using (_repo.RepoLock.WriterLock())
                {
                    foreach (KeyValuePair <T, T> r in updates)
                    {
                        r.Key.DeepCloneTo(r.Value); //Tried to be 100% atomic and failed miserably, so is 99%.
                                                    //If we replace Original with Entity in cache (updating with 'this' as the model to update, will not get the changes).
                                                    //So this is the best effort
                        returns.Add(r.Value);
                    }
                    foreach (KeyValuePair <T, T> r in creates)
                    {
                        _repo.Table.Add(r.Key);
                        returns.Add(r.Key);
                    }
                    if (_repo.IsCached)
                    {
                        returns.ForEach(_repo.Cache.Update);
                    }
                }
                _repo.Context.SaveChanges();
                foreach (T t in returns)
                {
                    _repo.EndSave(t, savedObjects[t], pars);
                }
            }
            Release();

            return(returns);
        }