Esempio n. 1
0
        public static CodeCompileUnit ConvertToOrchestrationBindingCodeCompileUnit(this Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!typeof(BTXService).IsAssignableFrom(type))
            {
                throw new ArgumentException(string.Format("{0} is not an orchestration type.", type.FullName), "type");
            }

            var ports        = (PortInfo[])Reflector.GetField(type, "_portInfo");
            var unboundPorts = ports
                               // filter out direct ports
                               .Where(p => p.FindAttribute(typeof(DirectBindingAttribute)) == null)
                               .ToArray();

            var @namespace = new CodeNamespace(type.Namespace);

            @namespace.ImportNamespace(typeof(Action <>));
            @namespace.ImportNamespace(typeof(GeneratedCodeAttribute));
            @namespace.ImportNamespace(typeof(OrchestrationBindingBase <>));
            @namespace.ImportNamespace(typeof(PortInfo));
            @namespace.ImportNamespace(type);

            if (unboundPorts.Any())
            {
                var @interface = @namespace.AddBindingInterface(type);
                unboundPorts.Each(port => @interface.AddPortPropertyMember(port));
                var @class = @namespace.AddBindingClass(type, @interface);
                ports.Each(port => @class.AddPortOperationMember(port));
                @class.AddDefaultConstructor();
                @class.AddBindingConfigurationConstructor(@interface);
                unboundPorts.Each(port => @class.AddPortPropertyMember(port, @interface));
            }
            else
            {
                var @class = @namespace.AddBindingClass(type);
                ports.Each(port => @class.AddPortOperationMember(port));
                @class.AddDefaultConstructor();
                @class.AddBindingConfigurationConstructor();
            }

            var compileUnit = new CodeCompileUnit();

            compileUnit.Namespaces.Add(@namespace);
            return(compileUnit);
        }
Esempio n. 2
0
        /// <summary>
        /// Generates POCO entities based on the supplied model
        /// </summary>
        /// <param name="model">Model to generate classes from</param>
        /// <returns>Generated code files</returns>
        public IEnumerable <FileContents <string> > GenerateEntityClasses(EntityModelSchema model)
        {
            ExceptionUtilities.CheckArgumentNotNull(model, "model");
            PocoAnnotator.Annotate(model, this.PocoOption);
            List <FileContents <string> > results = new List <FileContents <string> >();

            foreach (var ns in model.EntityTypes.Select(e => e.NamespaceName).Concat(model.EnumTypes.Select(e => e.NamespaceName)).Concat(model.ComplexTypes.Select(e => e.NamespaceName)).Distinct())
            {
                var           codeUnit      = new CodeCompileUnit();
                CodeNamespace codeNamespace = codeUnit.AddNamespace(ns);
                codeNamespace.ImportNamespace("System.Collections.Generic");

                foreach (var type in model.ComplexTypes.Where(e => e.NamespaceName == ns))
                {
                    codeNamespace.Types.Add(this.BuildType(type));
                }

                foreach (var type in model.EntityTypes.Where(e => e.NamespaceName == ns))
                {
                    codeNamespace.Types.Add(this.BuildType(type));
                }

                foreach (var type in model.EnumTypes.Where(e => e.NamespaceName == ns))
                {
                    codeNamespace.Types.Add(this.BuildType(type));
                }

                string code = this.GenerateCodeFromCompileUnit(codeUnit);
                results.Add(new FileContents <string>(ns + this.language.FileExtension, code));
            }

            return(results);
        }
        /// <summary>
        /// Generates the object layer for the given model
        /// </summary>
        /// <param name="compileUnit">The compile unit to add code to</param>
        /// <param name="model">The model to base the object layer on</param>
        public void GenerateObjectLayer(CodeCompileUnit compileUnit, EntityModelSchema model)
        {
            var namespaces = model.EntityTypes.Select(et => et.NamespaceName).Union(model.ComplexTypes.Select(ct => ct.NamespaceName)).Union(model.Functions.Select(f => f.NamespaceName)).Distinct().ToList();

            foreach (string namespaceName in namespaces)
            {
                CodeNamespace codeNamespace = Code.AddNamespace(compileUnit, namespaceName);

                foreach (string ns in this.GetNamespaceImports())
                {
                    codeNamespace.ImportNamespace(ns);
                }

                foreach (ComplexType type in model.ComplexTypes.Where(ct => ct.NamespaceName == namespaceName))
                {
                    if (this.ShouldGenerateTypeDefinition(type))
                    {
                        this.DeclareComplexType(type, codeNamespace.DeclareType(type.Name));
                    }
                }

                foreach (EntityType type in model.EntityTypes.Where(ct => ct.NamespaceName == namespaceName))
                {
                    if (this.ShouldGenerateTypeDefinition(type))
                    {
                        this.DeclareEntityType(type, codeNamespace.DeclareType(type.Name));
                    }
                }

                this.AddFunctionsInNamespaceIfRequired(namespaceName, codeNamespace, model.Functions);
            }
        }