Ejemplo n.º 1
0
        public ICodeBlockBuilder InvokeGenericMethod(
            INamedReference targetObject,
            string methodName,
            Type[] genericArguments,
            Action <IInvocationBuilder> invocation)
        {
            Write(targetObject).Write(".").Write(methodName);

            if ((genericArguments != null) && (genericArguments.Length > 0))
            {
                Write("<");

                for (var i = 0; i < genericArguments.Length; i++)
                {
                    if (i > 0)
                    {
                        Write(", ");
                    }

                    Write(genericArguments[i]);
                }

                Write(">");
            }

            Write("(");

            var ib = new InvocationBuilder(this);

            invocation(ib);

            return(Write(")"));
        }
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var implementingType = setup.ImplementingType ?? setup.InterfaceType;

            var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}";

            var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType);

            method.Body.Write("return ").InvokeConstructor(implementingType,
                                                           invocation =>
            {
                foreach (var constructorParameter in setup.PreferredConstructor.GetParameters())
                {
                    INamedReference factoryField;
                    if (ctorParamValueFactoryFields.TryGetValue(constructorParameter.Name, out factoryField))
                    {
                        invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write(factoryField).Write("(this)"));
                    }
                    else
                    {
                        invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write("Get(typeof(").Write(constructorParameter.ParameterType).Write("))"));
                    }
                }
            }).EndStatement();

            return(method);
        }
Ejemplo n.º 3
0
 public ClassBuilder(string name, int indent)
     : base(name)
 {
     m_baseClass   = GetTypeReference(typeof(object));
     m_indent      = indent;
     m_constructor = new ConstructorBuilder(name, indent + 1);
 }
Ejemplo n.º 4
0
 public INamedReference WithParam(string name, INamedReference typeReference)
 {
     if (!m_parameters.Any(p => p.Key == name))
     {
         m_parameters.Add(new KeyValuePair <string, INamedReference>(name, typeReference));
     }
     return(new NamedReference(name));
 }
Ejemplo n.º 5
0
        public ICodeBlockBuilder Compare(INamedReference a, Action <ICodeBlockBuilder> b)
        {
            Write(a);
            Write(" == ");
            b(this);

            return(this);
        }
Ejemplo n.º 6
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (!IsApplicable(setup))
            {
                return(previousPluginMethod);
            }

            var behavior = setup.Behaviors.OfType <EntityBehavior>().Last();

            var entityClass =
                locatorBuilder.HasNestedClass($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_Impl_{Guid.NewGuid():N}")
                .WithModifier("private")
                .WithModifier("sealed")
                .Implements <IEntity>()
                .Implements(setup.InterfaceType);

            var locatorField = entityClass.HasField <IServiceLocator>();

            var ctor         = entityClass.WithConstructor().WithModifier("public");
            var locatorParam = ctor.WithParam <IServiceLocator>("locator");

            ctor.Body.Assign(locatorField, a => a.Write(locatorParam)).EndStatement();
            ctor.Body.Write("IsDirty = true").EndStatement();

            var entityProperties = ImplementProperties(entityClass, setup.InterfaceType, behavior).ToList();

            ImplementIsDirtyProperty(entityClass);
            var pkProperty = ImplementReadPrimaryKeyAndGetPkProperty(entityClass, behavior, setup.InterfaceType);

            ImplementGetSaveMethodTypeMethod(entityClass, pkProperty);
            ImplementGetValuesMethod(entityClass, entityProperties, pkProperty);
            ImplementReadSelfMethod(entityClass, locatorField, entityProperties);
            ImplementReferenceEntityProperties(entityClass, setup.InterfaceType, m_setup.EntityNamingConvention);
            ImplementDbEntityNameProperty(entityClass, setup.InterfaceType);
            ImplementPkTypeProperty(entityClass, pkProperty);
            ImplementIsPkAuto(entityClass, pkProperty);

            var collectionMethod = locatorBuilder.HasMethod("GetEntitiesCollection").Returns(typeof(IEnumerable <Type>));

            collectionMethod.Body.Write("yield return typeof(")
            .Write(setup.InterfaceType)
            .Write(")")
            .EndStatement()
            .NewLine();

            var factoryMethod = locatorBuilder.HasMethod($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType);

            factoryMethod.Body.Write("return ")
            .InvokeConstructor(entityClass, inv => inv.WithParam("this"))
            .EndStatement();

            return(factoryMethod);
        }
Ejemplo n.º 7
0
        public ICodeBlockBuilder Invoke(INamedReference targetObject, string methodName, Action <IInvocationBuilder> invocation)
        {
            Write(targetObject).Write(".").Write(methodName).Write("(");

            var ib = new InvocationBuilder(this);

            invocation(ib);

            return(Write(")"));
        }
Ejemplo n.º 8
0
        public ICodeBlockBuilder InvokeConstructor(INamedReference ctorType, Action <IInvocationBuilder> invocation)
        {
            var ib = new InvocationBuilder(this);

            Write("new ").Write(ctorType).Write("(");

            invocation(ib);

            return(Write(")"));
        }
Ejemplo n.º 9
0
        public ICodeBlockBuilder ForEach(INamedReference collection, Action <INamedReference, ICodeBlockBuilder> itemVariableAndInnerBlock)
        {
            var variableName = new NamedReference($"v{Guid.NewGuid():N}");

            Write("foreach(var ").Write(variableName).Write(" in ").Write(collection).Write(")").NewLine();

            InnerBlock(i => itemVariableAndInnerBlock(variableName, i));

            return(NewLine());
        }
Ejemplo n.º 10
0
        public ICodeBlockBuilder Invoke(INamedReference target, Action <IInvocationBuilder> invocation)
        {
            var ib = new InvocationBuilder(this);

            Write(target).Write("(");

            invocation(ib);

            return(Write(")"));
        }
Ejemplo n.º 11
0
        public bool IsSourceBranch(INamedReference sourceBranchCandidate)
        {
            if (Equals(sourceBranchCandidate, this.branch))
            {
                return(false);
            }

            var branchName = sourceBranchCandidate.Name.Friendly;

            return(this.sourceBranchRegexes
                   .Any(regex => Regex.IsMatch(branchName, regex)));
        }
Ejemplo n.º 12
0
            public INamedReference GenerateFactoryMethod(
                IServiceSetupRecord setup,
                Dictionary <string, INamedReference> ctorParamValueFactoryFields,
                INamedReference valueFactoryField,
                IClassBuilder locatorBuilder,
                INamedReference previousPluginMethod)
            {
                var method = locatorBuilder.HasMethod("error");

                method.Body.Write("cannot compile this");

                return(method);
            }
Ejemplo n.º 13
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var implClass =
                locatorBuilder.HasNestedClass(
                    $"{TypeNameHelper.GetTypeMark(this.GetType())}_implements_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}")
                .Implements(setup.InterfaceType)
                .WithModifier("private")
                .WithModifier("sealed");

            var locatorField = implClass.HasField <IServiceLocator>();
            var locatorParam = implClass.WithConstructor().WithModifier("public").WithParam("locator", typeof(IServiceLocator));

            implClass.WithConstructor().Body.Assign(locatorField, a => a.Write(locatorParam));

            foreach (var property in setup.InterfaceType.GetProperties())
            {
                if (!ImplementProperty(implClass, property, locatorField))
                {
                    DefaultImplementProperty(implClass, property);
                }
            }

            foreach (var method in setup.InterfaceType.GetMethods())
            {
                if (method.IsSpecialName)
                {
                    continue;
                }

                if (!ImplementMethod(implClass, method, locatorField))
                {
                    DefaultImplementMethod(implClass, method);
                }
            }

            var factoryMethod =
                locatorBuilder.HasMethod($"factory_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType).WithModifier("private");

            factoryMethod.Body.Write(" return ")
            .InvokeConstructor(implClass, i => i.WithParam(p => p.Write("this")))
            .EndStatement();

            return(factoryMethod);
        }
Ejemplo n.º 14
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}";

            var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType);

            method.Body.Write("return ").Write("(").Write(setup.InterfaceType).Write(")").Write(valueFactoryField).Write("(this)").EndStatement();

            return(method);
        }
Ejemplo n.º 15
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var collectionMethod = locatorBuilder.HasMethod("GetCollectionItems");

            collectionMethod.Body.If(
                condition => condition.Write("collectionType == typeof(").Write(CollectionType).Write(")"),
                then => then.Write("yield return Get(typeof(").Write(setup.InterfaceType).Write("))").EndStatement()).NewLine();

            return(previousPluginMethod);
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     Gets all named references bound to formulas in the engine
        /// </summary>
        /// <returns>An array of named references</returns>
        /// <remarks>Use this function when you need to get all the named references bound to formulas in this engine.</remarks>
        public INamedReference[] GetNamedReferences()
        {
            IList found = new ArrayList();

            foreach (DictionaryEntry de in _referenceFormulaMap)
            {
                if (ReferenceEquals(de.Key.GetType(), typeof(NamedReference)))
                {
                    found.Add(de.Key);
                }
            }

            var arr = new INamedReference[found.Count];

            found.CopyTo(arr, 0);
            return(arr);
        }
Ejemplo n.º 17
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var factory = locatorBuilder.HasMethod("GetEntityCollectorInstance").Returns(typeof(IEntityCollector));

            factory.WithModifier("private");

            factory.Body.Write("return new ")
            .Write(typeof(EntityCollector))
            .Write("(System.Linq.Enumerable.ToList(this.GetEntitiesCollection()))")
            .EndStatement();

            return(factory);
        }
Ejemplo n.º 18
0
        public void BuildCall(
            IClassBuilder proxy,
            IMethodBuilder executeMethod,
            INamedReference contextParameter,
            Type controllerType,
            Func <Type, INamedReference> privateObjectsFactory,
            INamedReference interceptorField)
        {
            //public void Execute(RequestContext requestContext)

            var methodNameVar =
                executeMethod.Body.Var(
                    asg =>
                    asg.Write(typeof(MethodNameExtractor))
                    .Write(".ExtractMethodName(")
                    .Write(contextParameter)
                    .Write(")"));

            var resultVar = executeMethod.Body.DeclareLocal("__methodCallResult", typeof(object));

            executeMethod.Body.Assign(resultVar, x => x.Write("null"));

            foreach (var method in controllerType.GetMethods())
            {
                if ((method.DeclaringType != controllerType) && (!Attribute.IsDefined(method, typeof(CanBeInheritedAttribute))))
                {
                    continue;
                }

                var methodKey = method.Name.ToLowerInvariant();

                executeMethod.Body.If(
                    c => c.Compare(methodNameVar, b => b.String(methodKey)),
                    then => ImplementCall(controllerType, method, contextParameter, resultVar, privateObjectsFactory, proxy, interceptorField, then));
            }

            executeMethod.Body.Write("throw new ")
            .Write(typeof(InvalidOperationException))
            .Write("(")
            .String("Unknown method: ").Write(" + ").Write(methodNameVar)
            .Write(")")
            .EndStatement();
        }
Ejemplo n.º 19
0
        private INamedReference CreateFactoryMethod(IClassBuilder locator, InstanceRecord record, Dictionary <string, INamedReference> allFactoryFields)
        {
            var ctorParamFactoryFields = new Dictionary <string, INamedReference>();

            if (record.ConstructorParameters != null)
            {
                foreach (var ctorParam in record.ConstructorParameters)
                {
                    if (ctorParam.ValueProvider != null)
                    {
                        ctorParamFactoryFields[ctorParam.ParameterName] = allFactoryFields[ctorParam.ValueProvider.Name];
                    }
                }
            }

            INamedReference valueFactoryField = null;

            if (record.Factory != null)
            {
                valueFactoryField = allFactoryFields[record.Factory.Name];
            }


            INamedReference lastFactoryMethod = null;

            foreach (var plugin in record.ApplicablePlugins)
            {
                lastFactoryMethod = plugin.GenerateFactoryMethod(
                    record,
                    ctorParamFactoryFields,
                    valueFactoryField,
                    locator,
                    lastFactoryMethod);
            }

            if (lastFactoryMethod == null)
            {
                throw new InvalidOperationException($"Not any plugin was able to implement factory for {record.InterfaceType}");
            }

            return(lastFactoryMethod);
        }
Ejemplo n.º 20
0
 private void ComposeParameterRead(ICodeBlockBuilder code, INamedReference readerField, ParameterInfo parameterInfo, INamedReference methodInfoField, INamedReference parameterInfoField, INamedReference contextParameter, INamedReference interceptorField)
 {
     code.InvokeGenericMethod(
         interceptorField,
         nameof(IControllerInterceptor.ObtainParameterValue),
         new Type[] { parameterInfo.ParameterType },
         inv =>
         inv.WithParam("this")
         .WithParam(methodInfoField)
         .WithParam(parameterInfoField)
         .WithParam(contextParameter)
         .WithParam(
             factory =>
             factory.Write("() => ")
             .InvokeGenericMethod(
                 readerField,
                 nameof(IParameterReader.Read),
                 new Type[] { parameterInfo.ParameterType },
                 arguments => arguments.WithParam(parameterInfoField).WithParam(contextParameter))));
 }
Ejemplo n.º 21
0
        private static IEnumerable <string> GetSourceBranchRegexes(INamedReference branch, Config configuration)
        {
            var branchName          = branch.Name.WithoutRemote;
            var currentBranchConfig = configuration.GetConfigForBranch(branchName);

            if (currentBranchConfig?.SourceBranches == null)
            {
                yield return(".*");
            }
            else
            {
                foreach (var sourceBranch in currentBranchConfig.SourceBranches)
                {
                    var regex = configuration.Branches[sourceBranch]?.Regex;
                    if (regex != null)
                    {
                        yield return(regex);
                    }
                }
            }
        }
Ejemplo n.º 22
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (!setup.GetBehavior(s_defaultBehavior).Dispose)
            {
                return(previousPluginMethod);
            }

            var method = locatorBuilder.HasMethod($"Dispose{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}")
                         .Returns(setup.InterfaceType);

            method.Body.Write("return ")
            .Invoke("TryRegisterDisposable", invocation => invocation.WithParam(p => p.Invoke(previousPluginMethod,
                                                                                              inv => { })))
            .EndStatement();

            return(method);
        }
Ejemplo n.º 23
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var behavior = setup.Behaviors.OfType <ControllerBehavior>().FirstOrDefault();

            if (behavior == null)
            {
                return(previousPluginMethod);
            }

            var indexFactory =
                locatorBuilder.HasMethod("GetRoboapiControllerIndexFactory")
                .Returns <IEnumerable <KeyValuePair <string, Type> > >()
                .WithModifier("private");

            indexFactory.Body.Write("yield return new ")
            .Write(typeof(KeyValuePair <string, Type>))
            .Write("(")
            .String(behavior.ControllerName)
            .Write(", ")
            .Typeof(setup.ImplementingType)
            .Write(")")
            .EndStatement();

            var builder = Activator.CreateInstance(behavior.ProxyBuilderType) as ControllerProxyBuilder;

            var proxy = builder.BuildProxyClass(locatorBuilder, setup.ImplementingType, behavior.CallBuilderType);

            var factoryMethod =
                locatorBuilder.HasMethod($"factory{setup.ImplementingType.Name}").Returns(setup.ImplementingType);

            factoryMethod.Body.Write("return new ").Write(proxy).Write("(this)").EndStatement();

            return(factoryMethod);
        }
Ejemplo n.º 24
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (setup.GetBehavior(s_defaultBehavior).AlwaysNewInstance)
            {
                return(previousPluginMethod);
            }

            var field = locatorBuilder.HasField(setup.InterfaceType).WithModifier("private");

            var fMethod = locatorBuilder.HasMethod($"Singleton_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType);

            fMethod.Body.Write(" return ")
            .LazyReadOrAssign(field, a => a.Invoke(previousPluginMethod, inv => { }))
            .EndStatement();

            return(fMethod);
        }
Ejemplo n.º 25
0
 public ICodeBlockBuilder LazyReadOrAssign(INamedReference target, Action <ICodeBlockBuilder> assignment)
 {
     Write("(").Write(target).Write(" ?? (").Write(target).Write(" = ");
     assignment(this);
     return(Write("))"));
 }
Ejemplo n.º 26
0
 public ICodeBlockBuilder Assign(INamedReference target, Action <ICodeBlockBuilder> assignment)
 {
     Write(target).Write(" = ");
     assignment(this);
     return(EndStatement());
 }
Ejemplo n.º 27
0
 public INamedReference DeclareLocal(string name, INamedReference typeReference)
 {
     Write(typeReference).Space().Write(name).EndStatement();
     return(new NamedReference(name));
 }
Ejemplo n.º 28
0
 public ICodeBlockBuilder Write(INamedReference reference)
 {
     return(Write(reference.Name));
 }
Ejemplo n.º 29
0
 public ICodeBlockBuilder Compare(INamedReference a, string b)
 {
     return(Write(a).Write(" == ").Write(b));
 }
Ejemplo n.º 30
0
 public ClassFieldBuilder(string name, INamedReference type, int indent)
     : base(name)
 {
     m_type   = type;
     m_indent = indent;
 }