Esempio n. 1
0
 public void SetUp()
 {
     _id1 = new AssembledTypeID(typeof(int), new object[] { 1, 2 });
     _id2 = new AssembledTypeID(typeof(string), new object[] { 1, 2 });
     _id3 = new AssembledTypeID(typeof(int), new object[] { 1, 3 });
     _id4 = new AssembledTypeID(typeof(int), new object[] { 1, 2 });
 }
Esempio n. 2
0
        public void ReflectionServiceAPIs_CannotRunWhileCodeIsGenerated()
        {
            // Populate cache.
            var cachedAssembledType      = _pipeline.ReflectionService.GetAssembledType(typeof(DomainType));
            var cachedAssembledTypeID    = _pipeline.ReflectionService.GetTypeIDForAssembledType(cachedAssembledType);
            var otherCachedAssembledType = _pipeline.ReflectionService.GetAssembledType(typeof(OtherDomainType));

            var newRequestedType   = typeof(object);
            var newAssembledTypeID = new AssembledTypeID(newRequestedType, new object[0]);

            var t1 = StartAndWaitUntilBlocked(() => _pipeline.Create <DomainTypeCausingParticipantToBlock>());
            var t2 = StartAndWaitUntilBlocked(() => _pipeline.ReflectionService.GetAssembledType(newRequestedType));
            var t3 = StartAndWaitUntilBlocked(() => _pipeline.ReflectionService.GetAssembledType(newAssembledTypeID));
            var t4 = StartAndWaitUntilBlocked(() => _pipeline.ReflectionService.GetAdditionalType("additional type id"));

            // Operations that only return cached results are not blocked.
            Assert.That(_pipeline.ReflectionService.GetTypeIDForRequestedType(typeof(DomainType)), Is.EqualTo(cachedAssembledTypeID));
            Assert.That(_pipeline.ReflectionService.GetTypeIDForAssembledType(cachedAssembledType), Is.EqualTo(cachedAssembledTypeID));
            Assert.That(_pipeline.ReflectionService.GetAssembledType(typeof(DomainType)), Is.SameAs(cachedAssembledType));
            Assert.That(_pipeline.ReflectionService.GetAssembledType(cachedAssembledTypeID), Is.SameAs(cachedAssembledType));
            Assert.That(_pipeline.ReflectionService.InstantiateAssembledType(cachedAssembledTypeID, ParamList.Empty, false), Is.Not.Null);
            Assert.That(_pipeline.ReflectionService.InstantiateAssembledType(otherCachedAssembledType, ParamList.Empty, false), Is.Not.Null);
            Assert.That(
                () => _pipeline.ReflectionService.PrepareExternalUninitializedObject(new object(), InitializationSemantics.Construction),
                Throws.Nothing);

            // All threads that cannot be served purely by the cache are now blocked.
            // [t1] is blocked by the mutex, [t2, ...] are blocked by the code generation in [t1].
            _blockingMutex.ReleaseMutex();

            // Now all threads run to completion (user APIs do not interfere with code generation).
            WaitUntilCompleted(t1, t2, t3, t4);
        }
Esempio n. 3
0
 public void MakeSerializable(
     MutableType proxyType,
     string participantConfigurationID,
     IAssembledTypeIdentifierProvider assembledTypeIdentifierProvider,
     AssembledTypeID typeID)
 {
     // Does nothing.
 }
Esempio n. 4
0
        public object InstantiateAssembledType(AssembledTypeID typeID, ParamList constructorArguments, bool allowNonPublicConstructor)
        {
            ArgumentUtility.CheckNotNull("constructorArguments", constructorArguments);

            var constructorCall = _constructorCallCache.GetOrCreateConstructorCall(typeID, constructorArguments.FuncType, allowNonPublicConstructor);
            var instance        = constructorArguments.InvokeFunc(constructorCall);

            return(instance);
        }
        public void MakeSerializable(
            MutableType proxyType,
            string participantConfigurationID,
            IAssembledTypeIdentifierProvider assembledTypeIdentifierProvider,
            AssembledTypeID typeID)
        {
            ArgumentUtility.CheckNotNull("proxyType", proxyType);
            ArgumentUtility.CheckNotNullOrEmpty("participantConfigurationID", participantConfigurationID);
            ArgumentUtility.CheckNotNull("assembledTypeIdentifierProvider", assembledTypeIdentifierProvider);

            if (!proxyType.IsTypePipeSerializable())
            {
                return;
            }

            var assembledTypeIDData = assembledTypeIdentifierProvider.GetAssembledTypeIDDataExpression(typeID);

            if (typeof(ISerializable).IsTypePipeAssignableFrom(proxyType))
            {
                // If the mutable type already implements ISerializable, we only need to extend the implementation to include the metadata required for
                // deserialization. Existing fields will be serialized by the base ISerialization implementation. Added fields will be serialized by
                // the TypePipe (ProxySerializationEnabler).
                try
                {
                    proxyType
                    .GetOrAddImplementation(s_getObjectDataMethod)
                    .SetBody(
                        ctx => Expression.Block(
                            ctx.PreviousBody,
                            CreateMetaDataSerializationExpression(
                                ctx.Parameters[0], typeof(ObjectWithDeserializationConstructorProxy), participantConfigurationID, assembledTypeIDData)));
                }
                catch (NotSupportedException)
                {
                    // Overriding and re-implementation failed because the base implementation of GetObjectData is not accessible from the proxy.
                    // Do nothing here; error reporting code will be generated in the ProxySerializationEnabler.
                    // Reasoning: Users often cannot influence the requested type and do not care about any serialization problem.
                }
            }
            else
            {
                // If the mutable type does not implement ISerializable, we need to add the interface and then also serialize all the fields on the object.
                // We cannot add a deserialization constructor because there is no base constructor that we could call. Therefore, ProxySerializationEnabler
                // cannot take care of serializing the added fields, and we thus have to serialize both existing and added fields ourselves via
                // ReflectionSerializationHelper.AddFieldValues.

                proxyType.AddInterface(typeof(ISerializable));

                proxyType.AddExplicitOverride(
                    s_getObjectDataMethod,
                    ctx => Expression.Block(
                        CreateMetaDataSerializationExpression(
                            ctx.Parameters[0], typeof(ObjectWithoutDeserializationConstructorProxy), participantConfigurationID, assembledTypeIDData),
                        Expression.Call(s_addFieldValuesMethod, ctx.Parameters[0], ctx.This)));
            }
        }
Esempio n. 6
0
 public Expression GetAssembledTypeIDDataExpression(AssembledTypeID typeID)
 {
     return(CreateNewTypeIDExpression(
                s_assembledTypeIDDataConstructor,
                typeID.RequestedType.AssemblyQualifiedName,
                typeID.Parts,
                typeof(IFlatValue),
                (p, id) => p.GetFlatValueExpressionForSerialization(id),
                "GetFlatValueExpressionForSerialization"));
 }
Esempio n. 7
0
        public void SetUp()
        {
            _mutableTypeFactoryMock          = new Mock <IMutableTypeFactory> (MockBehavior.Strict);
            _complexSerializationEnablerMock = new Mock <IComplexSerializationEnabler> (MockBehavior.Strict);
            _participantStateMock            = new Mock <IParticipantState> (MockBehavior.Strict);

            _requestedType = typeof(RequestedType);
            _typeID        = AssembledTypeIDObjectMother.Create(_requestedType);
            _assembledType = typeof(AssembledType);
        }
Esempio n. 8
0
        public void SetUp()
        {
            var typeID1 = new AssembledTypeID(typeof(int), new object[0]);
            var typeID2 = new AssembledTypeID(typeof(object), new object[0]);

            _key1 = new ConstructionKey(typeID1, typeof(Action), true);
            _key2 = new ConstructionKey(typeID2, typeof(Action), true);
            _key3 = new ConstructionKey(typeID1, typeof(Func <int>), true);
            _key4 = new ConstructionKey(typeID1, typeof(Action), false);
            _key5 = new ConstructionKey(typeID1, typeof(Action), true);
        }
Esempio n. 9
0
        public void AddTypeID(MutableType proxyType, AssembledTypeID typeID)
        {
            ArgumentUtility.CheckNotNull("proxyType", proxyType);
            ArgumentUtility.CheckNotNull("typeID", typeID);

            var typeIDField      = proxyType.AddField(c_typeIDFieldName, FieldAttributes.Private | FieldAttributes.Static, typeof(AssembledTypeID));
            var typeIDExpression = CreateNewTypeIDExpression(
                s_assembledTypeIDConstructor, typeID.RequestedType, typeID.Parts, typeof(object), (p, id) => p.GetExpression(id), "GetExpression");
            var typeIDFieldInitialization = Expression.Assign(Expression.Field(null, typeIDField), typeIDExpression);

            proxyType.AddTypeInitialization(typeIDFieldInitialization);
        }
        public void SetUp()
        {
            _enabler = new ComplexSerializationEnabler();

            _participantConfigurationID          = "configID";
            _assembledTypeIdentifierProviderStub = new Mock <IAssembledTypeIdentifierProvider>();
            _typeID = AssembledTypeIDObjectMother.Create();
            _assembledTypeIDData = ExpressionTreeObjectMother.GetSomeExpression();
            _assembledTypeIdentifierProviderStub
            .Setup(_ => _.GetAssembledTypeIDDataExpression(It.Is <AssembledTypeID> (id => id.Equals(_typeID))))
            .Returns(_assembledTypeIDData);
        }
Esempio n. 11
0
        public void SetUp()
        {
            _settings              = PipelineSettings.New().Build();
            _codeManagerMock       = MockRepository.GenerateStrictMock <ICodeManager>();
            _reflectionServiceMock = MockRepository.GenerateStrictMock <IReflectionService>();
            _typeAssemblerMock     = MockRepository.GenerateStrictMock <ITypeAssembler>();

            _pipeline = new Pipeline(_settings, _codeManagerMock, _reflectionServiceMock, _typeAssemblerMock);

            _requestedType = ReflectionObjectMother.GetSomeType();
            _typeID        = AssembledTypeIDObjectMother.Create();
        }
Esempio n. 12
0
        public void ComputeTypeID()
        {
            var requestedType = ReflectionObjectMother.GetSomeType();

            _identifierProviderMock.Setup(_ => _.GetID(requestedType)).Returns("abc");

            var result = _provider.ComputeTypeID(requestedType);

            var expectedTypeID = new AssembledTypeID(requestedType, new object[] { "abc" });

            Assert.That(result, Is.EqualTo(expectedTypeID));
        }
Esempio n. 13
0
        public void SetUp()
        {
            _settings              = PipelineSettings.New().Build();
            _codeManagerMock       = new Mock <ICodeManager> (MockBehavior.Strict);
            _reflectionServiceMock = new Mock <IReflectionService> (MockBehavior.Strict);
            _typeAssemblerMock     = new Mock <ITypeAssembler> (MockBehavior.Strict);

            _pipeline = new Pipeline(_settings, _codeManagerMock.Object, _reflectionServiceMock.Object, _typeAssemblerMock.Object);

            _requestedType = ReflectionObjectMother.GetSomeType();
            _typeID        = AssembledTypeIDObjectMother.Create();
        }
Esempio n. 14
0
        public void SetUp()
        {
            _enabler = new ComplexSerializationEnabler();

            _participantConfigurationID          = "configID";
            _assembledTypeIdentifierProviderStub = MockRepository.GenerateStub <IAssembledTypeIdentifierProvider>();
            _typeID = AssembledTypeIDObjectMother.Create();
            _assembledTypeIDData = ExpressionTreeObjectMother.GetSomeExpression();
            _assembledTypeIdentifierProviderStub
            .Stub(_ => _.GetAssembledTypeIDDataExpression(Arg <AssembledTypeID> .Matches(id => id.Equals(_typeID))))
            .Return(_assembledTypeIDData);
        }
Esempio n. 15
0
        public object GetPart(AssembledTypeID typeID, IParticipant participant)
        {
            ArgumentUtility.CheckNotNull("participant", participant);

            int index;

            if (_identifierProviderIndexes.TryGetValue(participant, out index))
            {
                return(typeID.Parts[index]);
            }

            return(null);
        }
Esempio n. 16
0
        private GeneratedTypesContext GenerateTypes(AssembledTypeID typeID, ProxyTypeAssemblyContext context, IMutableTypeBatchCodeGenerator codeGenerator)
        {
            // Add [AssembledType] attribute.
            var attribute = new CustomAttributeDeclaration(s_assembledTypeAttributeCtor, new object[0]);

            context.ProxyType.AddCustomAttribute(attribute);

            // Add '__typeID' and initialization code.
            _assembledTypeIdentifierProvider.AddTypeID(context.ProxyType, typeID);

            // Enable complex serialization.
            _complexSerializationEnabler.MakeSerializable(context.ProxyType, _participantConfigurationID, _assembledTypeIdentifierProvider, typeID);

            var mutableTypes = context.AdditionalTypes.Values.Concat(new[] { context.ProxyType });

            return(GenerateTypesWithDiagnostics(codeGenerator, mutableTypes, context.RequestedType.Name));
        }
Esempio n. 17
0
        public void CreateObject_Generic()
        {
            var typeID = new AssembledTypeID(typeof(RequestedType), new object[0]);

            _reflectionServiceMock.Expect(mock => mock.GetTypeIDForRequestedType(typeof(RequestedType))).Return(typeID);
            var assembledInstance = new AssembledType();

            _reflectionServiceMock
            .Expect(
                mock => mock.InstantiateAssembledType(
                    // Use strongly typed Equals overload.
                    Arg <AssembledTypeID> .Matches(id => id.Equals(typeID)),
                    Arg.Is(ParamList.Empty),
                    Arg.Is(false)))
            .Return(assembledInstance);

            var result = _pipeline.Create <RequestedType>();

            Assert.That(result, Is.SameAs(assembledInstance));
        }
Esempio n. 18
0
        public void CreateObject_Generic()
        {
            var typeID = new AssembledTypeID(typeof(RequestedType), new object[0]);

            _reflectionServiceMock.Setup(mock => mock.GetTypeIDForRequestedType(typeof(RequestedType))).Returns(typeID).Verifiable();
            var assembledInstance = new AssembledType();

            _reflectionServiceMock
            .Setup(
                mock => mock.InstantiateAssembledType(
                    // Use strongly typed Equals overload.
                    It.Is <AssembledTypeID> (id => id.Equals(typeID)),
                    ParamList.Empty,
                    false))
            .Returns(assembledInstance)
            .Verifiable();

            var result = _pipeline.Create <RequestedType>();

            Assert.That(result, Is.SameAs(assembledInstance));
        }
Esempio n. 19
0
        public TypeAssemblyResult AssembleType(AssembledTypeID typeID, IParticipantState participantState, IMutableTypeBatchCodeGenerator codeGenerator)
        {
            ArgumentUtility.CheckNotNull("typeID", typeID);
            ArgumentUtility.CheckNotNull("participantState", participantState);
            ArgumentUtility.CheckNotNull("codeGenerator", codeGenerator);

            var requestedType = typeID.RequestedType;

            CheckRequestedType(requestedType);

            if (ShortCircuitTypeAssembly(requestedType))
            {
                return(new TypeAssemblyResult(requestedType));
            }

            var typeModificationTracker = _mutableTypeFactory.CreateProxy(requestedType, ProxyKind.AssembledType);
            var context = new ProxyTypeAssemblyContext(
                _mutableTypeFactory, _participantConfigurationID, participantState, requestedType, typeModificationTracker.Type);

            foreach (var participant in _participants)
            {
                var idPart = _assembledTypeIdentifierProvider.GetPart(typeID, participant);
                participant.Participate(idPart, context);
            }

            if (!typeModificationTracker.IsModified())
            {
                return(new TypeAssemblyResult(requestedType));
            }

            var generatedTypesContext = GenerateTypes(typeID, context, codeGenerator);

            context.OnGenerationCompleted(generatedTypesContext);

            return(new TypeAssemblyResult(
                       generatedTypesContext.GetGeneratedType(context.ProxyType),
                       context.AdditionalTypes.ToDictionary(kvp => kvp.Key, kvp => generatedTypesContext.GetGeneratedType(kvp.Value))));
        }
Esempio n. 20
0
 public Type GetAssembledType(AssembledTypeID typeID)
 {
     return(_typeCache.GetOrCreateType(typeID));
 }