Пример #1
0
                public ICommitContext Fill(IValueSource valueSource, Func <IValueSource, MappedObjectKeys> getKeyFunction, Func <T> createObject)
                {
                    lock (_cache)
                    {
                        Init(true);

                        CommitContext commitContext = new CommitContext(this);
                        while (valueSource.Next())
                        {
                            MappedObjectKeys keys = getKeyFunction(valueSource);
                            if (_cache._itemsByKey.ContainsKey(keys))
                            {
                                IFillAbleObject item = _cache._itemsByKey[keys].Value;
                                if (item == null)
                                {
                                    _cache._itemsByKey[keys] = new WeakReference <IFillAbleObject>(item = (IFillAbleObject)createObject());
                                    ((System.ComponentModel.INotifyPropertyChanged)item).PropertyChanged += _cache.EntryValuePropertyChanged;
                                }
                                item.Fill(valueSource);
                                commitContext.AddEntryToCommit((T)item);
                            }
                            else
                            {
                                IFillAbleObject newObject = (IFillAbleObject)createObject();
                                newObject.Fill(valueSource);
                                _cache._itemsByKey.Add(keys, new WeakReference <IFillAbleObject>(newObject));
                                commitContext.AddEntryToCommit((T)newObject);
                                ((System.ComponentModel.INotifyPropertyChanged)newObject).PropertyChanged += _cache.EntryValuePropertyChanged;
                            }
                        }

                        return(commitContext);
                    }
                }
Пример #2
0
            public void SaveObjects(
                IEnumerable <IFillAbleObject> items,
                Func <IFillAbleObject, ICommandBuilder> getBuilder,
                Action <IFillAbleObject, IValueSource> refill,
                Action <IFillAbleObject> afterFill)
            {
                DbConnection connection = GetConnection();

                try
                {
                    foreach (IFillAbleObject item in items.ToList())
                    {
                        try
                        {
                            ICommandBuilder commandBuilder = getBuilder(item);
                            if (commandBuilder == null)
                            {
                                continue;
                            }

                            item.FillCommand(commandBuilder);
                            using (DbCommand command = commandBuilder.GetDbCommand())
                            {
                                command.Connection = connection;
                                using (IValueSource valueSource = _objectProvider._databaseProvider.GetValueSource(command))
                                {
                                    refill(item, valueSource.Next() ? valueSource : null);
                                }
                            }
                            afterFill(item);
                        }
                        catch (Exception ex)
                        {
                            if (ex is EntitySaveException)
                            {
                                throw;
                            }
                            else
                            {
                                throw new EntitySaveException(item, ex);
                            }
                        }
                    }
                }
                finally
                {
                    _objectProvider._databaseProvider.ReleaseConnection(connection);
                }
            }