private static Func <object, IEntityWrapper> CreateWrapperDelegateTypedWithoutRelationships <TEntity>() where TEntity : class
        {
            bool overridesEquals = typeof(TEntity).OverridesEqualsOrGetHashCode();
            Func <object, IPropertyAccessorStrategy> propertyAccessorStrategy;
            Func <object, IEntityKeyStrategy>        keyStrategy;
            Func <object, IChangeTrackingStrategy>   changeTrackingStrategy;

            EntityWrapperFactory.CreateStrategies <TEntity>(out propertyAccessorStrategy, out changeTrackingStrategy, out keyStrategy);
            return((Func <object, IEntityWrapper>)(entity => (IEntityWrapper) new EntityWrapperWithoutRelationships <TEntity>((TEntity)entity, propertyAccessorStrategy, changeTrackingStrategy, keyStrategy, overridesEquals)));
        }
        public void Factory_sets_override_flag_appropriately_for_pure_POCO_entities()
        {
            var wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new object(), new EntityKey());

            Assert.Same(typeof(EntityWrapperWithoutRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.False(wrappedEntity.OverridesEqualsOrGetHashCode);

            wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new Mock <PocoWithEquals>().Object, new EntityKey());
            Assert.Same(typeof(EntityWrapperWithoutRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.True(wrappedEntity.OverridesEqualsOrGetHashCode);
        }
        public void Factory_sets_override_flag_appropriately_for_entities_with_relationships()
        {
            var wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new EntityWithRelationshipsButWithoutEquals(), new EntityKey());

            Assert.Same(typeof(EntityWrapperWithRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.False(wrappedEntity.OverridesEqualsOrGetHashCode);

            wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new EntityWithRelationshipsAndEquals(), new EntityKey());
            Assert.Same(typeof(EntityWrapperWithRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.True(wrappedEntity.OverridesEqualsOrGetHashCode);
        }
        public void Factory_sets_override_flag_appropriately_for_IPOCO_EntityObject_entities()
        {
            var wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new EntityObjectWithoutEquals(), new EntityKey());

            Assert.Same(typeof(LightweightEntityWrapper <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.False(wrappedEntity.OverridesEqualsOrGetHashCode);

            wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new EntityObjectWithEquals(), new EntityKey());
            Assert.Same(typeof(LightweightEntityWrapper <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.True(wrappedEntity.OverridesEqualsOrGetHashCode);
        }
        internal virtual IEntityWrapper WrapEntityUsingStateManagerGettingEntry(
            object entity,
            ObjectStateManager stateManager,
            out EntityEntry existingEntry)
        {
            IEntityWrapper wrapper = (IEntityWrapper)null;

            existingEntry = (EntityEntry)null;
            if (entity == null)
            {
                return(NullEntityWrapper.NullWrapper);
            }
            if (stateManager != null)
            {
                existingEntry = stateManager.FindEntityEntry(entity);
                if (existingEntry != null)
                {
                    return(existingEntry.WrappedEntity);
                }
                if (stateManager.TransactionManager.TrackProcessedEntities && stateManager.TransactionManager.WrappedEntities.TryGetValue(entity, out wrapper))
                {
                    return(wrapper);
                }
            }
            IEntityWithRelationships withRelationships = entity as IEntityWithRelationships;

            if (withRelationships != null)
            {
                RelationshipManager relationshipManager = withRelationships.RelationshipManager;
                if (relationshipManager == null)
                {
                    throw new InvalidOperationException(Strings.RelationshipManager_UnexpectedNull);
                }
                IEntityWrapper wrappedOwner = relationshipManager.WrappedOwner;
                if (!object.ReferenceEquals(wrappedOwner.Entity, entity))
                {
                    throw new InvalidOperationException(Strings.RelationshipManager_InvalidRelationshipManagerOwner);
                }
                return(wrappedOwner);
            }
            EntityProxyFactory.TryGetProxyWrapper(entity, out wrapper);
            if (wrapper == null)
            {
                IEntityWithKey entityWithKey = entity as IEntityWithKey;
                wrapper = EntityWrapperFactory.CreateNewWrapper(entity, entityWithKey == null ? (EntityKey)null : entityWithKey.EntityKey);
            }
            if (stateManager != null && stateManager.TransactionManager.TrackProcessedEntities)
            {
                stateManager.TransactionManager.WrappedEntities.Add(entity, wrapper);
            }
            return(wrapper);
        }
예제 #6
0
        public void Factory_sets_override_flag_appropriately_for_entities_with_relationships()
        {
            var mockWithRelationships = new Mock <IEntityWithRelationships>();

            mockWithRelationships.Setup(m => m.RelationshipManager).Returns(RelationshipManager.Create(mockWithRelationships.Object));
            var wrappedEntity = EntityWrapperFactory.CreateNewWrapper(mockWithRelationships.Object, new EntityKey());

            Assert.Same(typeof(EntityWrapperWithRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.False(wrappedEntity.OverridesEqualsOrGetHashCode);

            wrappedEntity = EntityWrapperFactory.CreateNewWrapper(new Mock <EntityWithRelationshipsAndEquals>().Object, new EntityKey());
            Assert.Same(typeof(EntityWrapperWithRelationships <>), wrappedEntity.GetType().GetGenericTypeDefinition());
            Assert.True(wrappedEntity.OverridesEqualsOrGetHashCode);
        }
        private static void CreateStrategies <TEntity>(
            out Func <object, IPropertyAccessorStrategy> createPropertyAccessorStrategy,
            out Func <object, IChangeTrackingStrategy> createChangeTrackingStrategy,
            out Func <object, IEntityKeyStrategy> createKeyStrategy)
        {
            Type type  = typeof(TEntity);
            bool flag1 = typeof(IEntityWithRelationships).IsAssignableFrom(type);
            bool flag2 = typeof(IEntityWithChangeTracker).IsAssignableFrom(type);
            bool flag3 = typeof(IEntityWithKey).IsAssignableFrom(type);
            bool flag4 = EntityProxyFactory.IsProxyType(type);

            createPropertyAccessorStrategy = !flag1 || flag4?EntityWrapperFactory.GetPocoPropertyAccessorStrategyFunc() : EntityWrapperFactory.GetNullPropertyAccessorStrategyFunc();

            createChangeTrackingStrategy = !flag2?EntityWrapperFactory.GetSnapshotChangeTrackingStrategyFunc() : EntityWrapperFactory.GetEntityWithChangeTrackerStrategyFunc();

            if (flag3)
            {
                createKeyStrategy = EntityWrapperFactory.GetEntityWithKeyStrategyStrategyFunc();
            }
            else
            {
                createKeyStrategy = EntityWrapperFactory.GetPocoEntityKeyStrategyFunc();
            }
        }