Пример #1
0
        public virtual StateEntry GetOrMaterializeEntry([NotNull] IEntityType entityType, [NotNull] IValueReader valueReader)
        {
            Check.NotNull(entityType, "entityType");
            Check.NotNull(valueReader, "valueReader");

            // TODO: Pre-compute this for speed
            var keyProperties = entityType.GetKey().Properties;
            var keyValue      = _keyFactorySource.GetKeyFactory(keyProperties).Create(entityType, keyProperties, valueReader);

            StateEntry existingEntry;

            if (_identityMap.TryGetValue(keyValue, out existingEntry))
            {
                return(existingEntry);
            }

            var newEntry = _subscriber.SnapshotAndSubscribe(_factory.Create(entityType, valueReader));

            _identityMap.Add(keyValue, newEntry);

            var entity = newEntry.Entity;

            if (entity != null)
            {
                _entityReferenceMap[entity] = newEntry;
            }

            newEntry.EntityState = EntityState.Unchanged;

            return(newEntry);
        }
Пример #2
0
        public virtual void StartTracking(
            [NotNull] IEntityType entityType, [NotNull] object entity, [NotNull] IValueReader valueReader)
        {
            Check.NotNull(entityType, "entityType");
            Check.NotNull(entity, "entity");
            Check.NotNull(valueReader, "valueReader");

            // TODO: Perf: Pre-compute this for speed
            var keyProperties = entityType.GetPrimaryKey().Properties;
            var keyValue      = _keyFactorySource.GetKeyFactory(keyProperties).Create(entityType, keyProperties, valueReader);

            var existingEntry = TryGetEntry(keyValue);

            if (existingEntry != null)
            {
                return;
            }

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

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

            newEntry.EntityState = EntityState.Unchanged;
        }
Пример #3
0
        public virtual EntityKey CreateKey(
            [NotNull] IEntityType entityType,
            [NotNull] IReadOnlyList <IProperty> properties,
            [NotNull] IPropertyBagEntry propertyBagEntry)
        {
            Check.NotNull(entityType, "entityType");
            Check.NotEmpty(properties, "properties");
            Check.NotNull(propertyBagEntry, "propertyBagEntry");

            return(_entityKeyFactorySource
                   .GetKeyFactory(properties)
                   .Create(entityType, properties, propertyBagEntry));
        }
Пример #4
0
        public virtual StateEntry StartTracking(
            [NotNull] IEntityType entityType, [NotNull] object entity, [NotNull] IValueReader valueReader)
        {
            Check.NotNull(entityType, "entityType");
            Check.NotNull(entity, "entity");
            Check.NotNull(valueReader, "valueReader");

            // TODO: Perf: Pre-compute this for speed
            var keyProperties = entityType.GetPrimaryKey().Properties;
            var keyValue      = _keyFactorySource.GetKeyFactory(keyProperties).Create(entityType, keyProperties, valueReader);

            if (keyValue == EntityKey.NullEntityKey)
            {
                throw new InvalidOperationException(Strings.NullPrimaryKey(entityType.SimpleName));
            }

            var existingEntry = TryGetEntry(keyValue);

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

                return(existingEntry);
            }

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

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

            newEntry.SetEntityState(EntityState.Unchanged);

            return(newEntry);
        }