Exemplo n.º 1
0
        public virtual InternalEntityEntry StartTracking(
            IEntityType entityType, EntityKey entityKey, object entity, ValueBuffer valueBuffer)
        {
            if (entityKey == EntityKey.InvalidEntityKey)
            {
                throw new InvalidOperationException(Strings.InvalidPrimaryKey(entityType.DisplayName()));
            }

            var existingEntry = TryGetEntry(entityKey);

            if (existingEntry != null)
            {
                if (existingEntry.Entity != entity)
                {
                    throw new InvalidOperationException(Strings.IdentityConflict(entityType.DisplayName()));
                }

                return existingEntry;
            }

            var newEntry = _subscriber.SnapshotAndSubscribe(_factory.Create(this, entityType, entity, valueBuffer));

            _identityMap.Add(entityKey, newEntry);
            _entityReferenceMap[entity] = newEntry;

            newEntry.SetEntityState(EntityState.Unchanged);

            return newEntry;
        }
        /// <inheritdoc />
        public override InternalEntityEntry Create(IStateManager stateManager, IEntityType entityType, object entity, ValueBuffer valueBuffer)
        {
            var entry = base.Create(stateManager, entityType, entity, valueBuffer);

            BindMixins(entry, entityType, entity);

            return entry;
        }
Exemplo n.º 3
0
        public EntityLoadInfo(
            ValueBuffer valueBuffer, [NotNull] Func<ValueBuffer, object> materializer)
        {
            // hot path
            Debug.Assert(materializer != null);

            ValueBuffer = valueBuffer;
            _materializer = materializer;
        }
        private object[] ExtractShadowValues(ValueBuffer valueBuffer)
        {
            var shadowValues = new object[EntityType.ShadowPropertyCount()];

            foreach (var property in EntityType.GetProperties().Where(property => property.IsShadowProperty))
            {
                shadowValues[property.GetShadowIndex()] = valueBuffer[property.Index];
            }

            return shadowValues;
        }
        public void Delegate_from_entity_type_is_returned_if_it_implements_IEntityMaterializer()
        {
            var materializerMock = new Mock<IEntityMaterializer>();
            var typeMock = materializerMock.As<IEntityType>();
            typeMock.SetupGet(et => et.ClrType).Returns(typeof(string));

            var reader = new ValueBuffer();
            GetMaterializer(new EntityMaterializerSource(new MemberMapper(new FieldMatcher())), typeMock.Object)(reader);

            materializerMock.Verify(m => m.CreateEntity(reader));
        }
 public InternalMixedEntityEntry(
     [NotNull] IStateManager stateManager,
     [NotNull] IEntityType entityType,
     [NotNull] IEntityEntryMetadataServices metadataServices,
     [NotNull] object entity,
     ValueBuffer valueBuffer)
     : base(stateManager, entityType, metadataServices)
 {
     Entity = entity;
     _shadowValues = ExtractShadowValues(valueBuffer);
 }
        public virtual InternalEntityEntry SnapshotAndSubscribe(InternalEntityEntry entry, ValueBuffer? values)
        {
            var entityType = entry.EntityType;

            if (entityType.UseEagerSnapshots())
            {
                if (values != null)
                {
                    entry.OriginalValues = new ValueBufferOriginalValues(entry, values.Value);
                }
                else
                {
                    entry.OriginalValues.TakeSnapshot();
                }

                entry.RelationshipsSnapshot.TakeSnapshot();
            }
            else
            {
                foreach (var navigation in entityType.GetNavigations().Where(n => n.IsNonNotifyingCollection(entry)))
                {
                    entry.RelationshipsSnapshot.TakeSnapshot(navigation);
                }
            }

            var changing = entry.Entity as INotifyPropertyChanging;
            if (changing != null)
            {
                changing.PropertyChanging += (s, e) =>
                    {
                        var property = TryGetPropertyBase(entityType, e.PropertyName);
                        if (property != null)
                        {
                            _notifier.PropertyChanging(entry, property);
                        }
                    };
            }

            var changed = entry.Entity as INotifyPropertyChanged;
            if (changed != null)
            {
                changed.PropertyChanged += (s, e) =>
                    {
                        var property = TryGetPropertyBase(entityType, e.PropertyName);
                        if (property != null)
                        {
                            _notifier.PropertyChanged(entry, property);
                        }
                    };
            }

            return entry;
        }
        public virtual void StartTracking(
            [NotNull] IStateManager stateManager, [NotNull] object entity, ValueBuffer valueBuffer)
        {
            Check.NotNull(stateManager, nameof(stateManager));
            Check.NotNull(entity, nameof(entity));

            stateManager.StartTracking(
                _entityType,
                _entityKeyFactory.Create(_entityType.RootType(), _entityKeyProperties, valueBuffer),
                entity,
                valueBuffer);
        }
        public virtual void StartTracking(
            [NotNull] IStateManager stateManager, [NotNull] object entity, ValueBuffer valueBuffer)
        {
            Check.NotNull(stateManager, nameof(stateManager));
            Check.NotNull(entity, nameof(entity));

            stateManager.StartTracking(
                _entityType,
                _keyValueFactory.Create(valueBuffer),
                entity,
                valueBuffer);
        }
Exemplo n.º 10
0
        public void StartTracking_throws_for_invalid_entity_key()
        {
            var model = BuildModel();
            var categoryType = model.FindEntityType(typeof(Category));
            var stateManager = CreateStateManager(model);

            var valueBuffer = new ValueBuffer(new object[] { 0, "Bjork", null });

            Assert.Equal(
                CoreStrings.InvalidPrimaryKey("Category"),
                Assert.Throws<InvalidOperationException>(
                    () => stateManager.StartTracking(categoryType, KeyValue.InvalidKeyValue, new Category { Id = 0 }, valueBuffer)).Message);
        }
Exemplo n.º 11
0
        public void StartTracking_is_no_op_if_entity_is_already_tracked()
        {
            var model = BuildModel();
            var categoryType = model.FindEntityType(typeof(Category));
            var stateManager = CreateStateManager(model);

            var category = new Category { Id = 77 };
            var entityKey = new SimpleKeyValue<int>(categoryType.FindPrimaryKey(), 77);
            var valueBuffer = new ValueBuffer(new object[] { 77, "Bjork", null });

            var entry = stateManager.StartTracking(categoryType, entityKey, category, valueBuffer);

            Assert.Same(entry, stateManager.StartTracking(categoryType, entityKey, category, valueBuffer));
        }
        private InternalEntityEntry NewInternalEntityEntry(
            IStateManager stateManager,
            IEntityType entityType,
            object entity,
            ValueBuffer valueBuffer)
        {
            if (!entityType.HasClrType())
            {
                return new InternalShadowEntityEntry(stateManager, entityType, _metadataServices, valueBuffer);
            }

            return entityType.ShadowPropertyCount() > 0
                ? (InternalEntityEntry)new InternalMixedEntityEntry(stateManager, entityType, _metadataServices, entity, valueBuffer)
                : new InternalClrEntityEntry(stateManager, entityType, _metadataServices, entity);
        }
        public InternalShadowEntityEntry(
            [NotNull] IStateManager stateManager,
            [NotNull] IEntityType entityType,
            [NotNull] IEntityEntryMetadataServices metadataServices,
            ValueBuffer valueBuffer)
            : base(stateManager, entityType, metadataServices)
        {
            _propertyValues = new object[valueBuffer.Count];

            var index = 0;
            foreach (var property in entityType.GetProperties())
            {
                _propertyValues[index++] = valueBuffer[property.GetIndex()];
            }
        }
Exemplo n.º 14
0
        public void StartTracking_throws_if_different_instance_with_same_identity_is_already_tracked()
        {
            var model = BuildModel();
            var categoryType = model.FindEntityType(typeof(Category));
            var stateManager = CreateStateManager(model);

            var entityKey = new SimpleKeyValue<int>(categoryType.FindPrimaryKey(), 77);
            var valueBuffer = new ValueBuffer(new object[] { 77, "Bjork", null });

            stateManager.StartTracking(categoryType, entityKey, new Category { Id = 77 }, valueBuffer);

            Assert.Equal(
                CoreStrings.IdentityConflict("Category"),
                Assert.Throws<InvalidOperationException>(
                    () => stateManager.StartTracking(categoryType, entityKey, new Category { Id = 77 }, valueBuffer)).Message);
        }
Exemplo n.º 15
0
        public virtual InternalEntityEntry StartTracking(
            IEntityType entityType, IKeyValue keyValue, object entity, ValueBuffer valueBuffer)
        {
            if (keyValue == KeyValue.InvalidKeyValue)
            {
                throw new InvalidOperationException(CoreStrings.InvalidPrimaryKey(entityType.DisplayName()));
            }

            var existingEntry = TryGetEntry(keyValue);
            if (existingEntry != null)
            {
                if (existingEntry.Entity != entity)
                {
                    throw new InvalidOperationException(CoreStrings.IdentityConflict(entityType.DisplayName()));
                }

                return existingEntry;
            }

            var newEntry = _subscriber.SnapshotAndSubscribe(_factory.Create(this, entityType, entity, valueBuffer), valueBuffer);

            AddToIdentityMap(entityType, keyValue, newEntry);

            _entityReferenceMap[entity] = newEntry;
            _detachedEntityReferenceMap.Remove(entity);

            newEntry.SetEntityState(EntityState.Unchanged);

            return newEntry;
        }
Exemplo n.º 16
0
 protected ValueBufferSidecar([NotNull] InternalEntityEntry entry, ValueBuffer values)
     : base(entry)
 {
     _values = values;
 }
 public override EntityKey Create(
     IEntityType entityType, IReadOnlyList<IProperty> properties, ValueBuffer valueBuffer)
     => Create(entityType, properties, p => valueBuffer[p.Index]);
Exemplo n.º 18
0
            public void StartTracking([NotNull] IStateManager stateManager, ValueBuffer valueBuffer)
            {
                Check.NotNull(stateManager, nameof(stateManager));

                stateManager.StartTracking(
                    IncludedEntityTrackingInfo.EntityType,
                    IncludedEntityTrackingInfo.CreateKeyValue(valueBuffer),
                    Entity,
                    valueBuffer);
            }
 public ValueBufferOriginalValues([NotNull] InternalEntityEntry entry, ValueBuffer values)
     : base(entry, values)
 {
 }
 public abstract EntityKey Create(
     [NotNull] IEntityType entityType,
     [NotNull] IReadOnlyList<IProperty> properties,
     ValueBuffer valueBuffer);
Exemplo n.º 21
0
 public abstract IKeyValue Create(ValueBuffer valueBuffer);
 public virtual EntityKey CreateEntityKey(ValueBuffer valueBuffer)
 {
     return EntityKeyFactory.Create(EntityType.RootType(), EntityKeyProperties, valueBuffer);
 }
Exemplo n.º 23
0
 public abstract IKeyValue Create(
     [NotNull] IReadOnlyList<IProperty> properties,
     ValueBuffer valueBuffer);
        public virtual void PropagateResults(ValueBuffer valueBuffer)
        {
            Check.NotNull(valueBuffer, nameof(valueBuffer));

            // Note that this call sets the value into a sidecar and will only commit to the actual entity
            // if SaveChanges is successful.
            var index = 0;
            foreach (var modification in ColumnModifications.Where(o => o.IsRead))
            {
                modification.Value = valueBuffer[index++];
            }
        }
 public virtual InternalEntityEntry Create(IStateManager stateManager, IEntityType entityType, object entity, ValueBuffer valueBuffer)
     => NewInternalEntityEntry(stateManager, entityType, entity, valueBuffer);
Exemplo n.º 26
0
 public virtual IKeyValue CreateKeyValue(ValueBuffer valueBuffer)
     => KeyValueFactory.Create(valueBuffer);
Exemplo n.º 27
0
 public InternalEntityEntry StartTracking(IEntityType entityType, IKeyValue keyValue, object entity, ValueBuffer valueBuffer)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 28
0
 public virtual EntityKey CreateEntityKey(ValueBuffer valueBuffer)
     => EntityKeyFactory.Create(EntityKeyProperties, valueBuffer);