Ejemplo n.º 1
0
        public void Save(object entity)
        {
            CheckDisposed();

            int id;

            // already being saved or updated?
            if (_saved.Contains(entity) || _updated.Contains(entity))
            {
                return;
            }

            // is it a new object?
            if (_identityMap.TryGetDocumentId(entity, out id))
            {
                // already being updated?
                if (_updated.Contains(entity))
                {
                    return;
                }

                _updated.Add(entity);
                return;
            }

            // Does it have a valid identifier?
            var accessor = _store.GetIdAccessor(entity.GetType(), "Id");

            if (accessor != null)
            {
                id = accessor.Get(entity);

                if (id > 0)
                {
                    _identityMap.Add(id, entity);
                    _updated.Add(entity);
                    return;
                }
            }

            // Then assign a new identifier if it has one
            if (accessor != null)
            {
                // it's a new entity
                var collection = CollectionHelper.Current.GetSafeName();
                id = _store.GetNextId(collection);
                accessor.Set(entity, id);
                _identityMap.Add(id, entity);
            }

            _saved.Add(entity);
        }
Ejemplo n.º 2
0
        private async Task SaveEntityAsync(object entity, bool update)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("obj");
            }

            var index    = entity as IIndex;
            var document = entity as Document;

            if (document != null)
            {
                throw new ArgumentException("A document should not be saved explicitely");
            }
            else if (index != null)
            {
                throw new ArgumentException("An index should not be saved explicitely");
            }
            else
            {
                // if the object is not new, reload to get the old map
                int id;
                if (_identityMap.TryGetDocumentId(entity, out id))
                {
                    if (update)
                    {
                        var oldDoc = await GetDocumentByIdAsync(id);

                        var oldObj = await _storage.GetAsync(id, entity.GetType());

                        // Update map index
                        MapDeleted(oldDoc, oldObj);
                        MapNew(oldDoc, entity);

                        // Save entity
                        await _storage.UpdateAsync(new IdentityDocument(id, entity));
                    }
                }
                else
                {
                    var doc = new Document
                    {
                        Type = entity.GetType().SimplifiedTypeName()
                    };

                    // Get the entity's Id if assigned
                    var accessor = _store.GetIdAccessor(entity.GetType(), "Id");
                    if (accessor != null)
                    {
                        doc.Id = accessor.Get(entity);
                    }
                    else
                    {
                        doc.Id = _store.GetNextId();
                    }

                    await new CreateDocumentCommand(doc, _store.Configuration.TablePrefix).ExecuteAsync(_connection, _transaction);
                    await _storage.CreateAsync(new IdentityDocument(doc.Id, entity));

                    // Track the newly created object
                    _identityMap.Add(doc.Id, entity);

                    MapNew(doc, entity);
                }
            }
        }