public TsType ConvertType(TypeInfo type, TypeContext context)
        {
            if (!TypeFilterStrategy.IsAllowedType(type))
            {
                return(null);
            }

            if (type.IsInterface || type.IsClass)
            {
                return(InterfaceMapper.GetInterface(type, context));
            }

            if (type.IsEnum)
            {
                return(TypeDefinitionMapper.GetTypeDefinition(type, context));
            }

            return(null);
        }
        internal void LoadFrom(XmlNode root)
        {
            var mapper = root.SelectSingleNode("mapper");

            if (mapper != null)
            {
                var map = InterfaceMapper.Create(mapper);
                if (map != null)
                {
                    this.RepositoryMapper = map;
                }
            }

            var transactionManagerFactory = root.SelectSingleNode("transactionManagerFactory");

            if (transactionManagerFactory != null)
            {
                var imp = InterfaceImplementer.Create(transactionManagerFactory);
                if (imp != null)
                {
                    this.TransactionManagerFactoryImplementer = imp;
                }
            }
        }
        private UnityConfigContainer GetContainerFromXmlNode(XmlNode node)
        {
            //UnityContainerTypeMapper mapper = new UnityContainerTypeMapper();
            InterfaceMapper mapper        = new InterfaceMapper();
            string          containerName = node.Attributes["name"] == null ? "default" : node.Attributes["name"].Value;

            //string baseContainerName = node.Attributes["base"] == null ? string.Empty : node.Attributes["base"].Value;
            //if (!string.IsNullOrEmpty(baseContainerName))
            //{
            //    UnityConfigContainer baseContainer = this.GetContainer(baseContainerName) as UnityConfigContainer;
            //    if (baseContainer == null)
            //        throw new UnityException("没有找到名称为" + baseContainerName + "的反转控制容器");
            //    baseContainer.Mapper.FillMapper(mapper);//从基础容器中填充信息
            //}

            foreach (XmlNode item in node.ChildNodes)
            {
                switch (item.Name)
                {
                case "types":
                {
                    foreach (XmlNode typeNode in item.ChildNodes)
                    {
                        if (typeNode.Name == "add")
                        {
                            string interfaceName = typeNode.Attributes["type"].Value, implementName = typeNode.Attributes["mapTo"].Value;
                            bool   isSingleton          = typeNode.Attributes["isSingleton"] != null && typeNode.Attributes["isSingleton"].Value == "true";
                            UnityResolveArgument[] args = null;
                            XmlNodeList            list = typeNode.SelectNodes("params/add");
                            if (list.Count > 0)
                            {
                                var argsCount = list.Count;
                                args = new UnityResolveArgument[argsCount];
                                for (var i = 0; i < argsCount; i++)
                                {
                                    XmlNode argNode = list[i];
                                    args[i] = new UnityResolveArgument(argNode.Attributes["name"].Value, argNode.Attributes["value"].Value);
                                }
                            }

                            Type impType = Type.GetType(implementName);
                            if (impType == null)
                            {
                                throw new UnityException(string.Format("没有找到 {0} 对应的类型 {1} ", interfaceName, implementName));
                            }

                            Type interfaceType = Type.GetType(interfaceName);
                            if (interfaceType == null)
                            {
                                throw new UnityException(string.Format("没有找到类型 {0} ", interfaceName));
                            }


                            InterfaceImplement impInfo = new InterfaceImplement
                            {
                                ImplementType = impType,
                                IsSingleton   = isSingleton,
                                InitMethod    = (instance) =>
                                {
                                    IUnityResolve resolve = instance as IUnityResolve;
                                    if (resolve != null)
                                    {
                                        resolve.Init(args);
                                    }
                                }
                            };
                            mapper.AddImplement(interfaceType, impInfo);
                        }
                    }
                }
                break;
                }
            }

            return(new UnityConfigContainer(containerName, mapper));
        }
Exemple #4
0
 internal UnityConfigContainer(string name, InterfaceMapper mapper)
 {
     this._name = name;
     _mapper    = mapper;
 }
Exemple #5
0
        public ICollection <GeneratedFile> FromStream(Stream stream, GeneratorSettings settings, out OpenApiDiagnostic diagnostic)
        {
            var reader          = new OpenApiStreamReader();
            var openApiDocument = reader.Read(stream, out diagnostic);

            var models     = new ModelsMapper(settings, diagnostic.SpecificationVersion).Map(openApiDocument.Components.Schemas).ToList();
            var @interface = new InterfaceMapper(settings).Map(openApiDocument);
            var security   = new SecurityMapper(settings).Map(openApiDocument);

            var files = new List <GeneratedFile>
            {
                // Add Interface
                new GeneratedFile
                {
                    Path    = settings.ApiNamespace,
                    Name    = $"{@interface.Name}.cs",
                    Content = new InterfaceBuilder(settings).Build(@interface, security, models.Any())
                }
            };

            var extensions = new ExtensionMethodsBuilder(settings).Build(@interface, @interface.Name);

            if (extensions != null)
            {
                // Add ApiExtension
                files.Add(new GeneratedFile
                {
                    Path    = settings.ApiNamespace,
                    Name    = $"{new string(@interface.Name.Skip(1).ToArray())}Extensions.cs",
                    Content = extensions
                });
            }

            // Add Models
            var modelBuilder = new ModelBuilder(settings);

            files.AddRange(models.Select(model => new GeneratedFile
            {
                Path    = settings.ModelsNamespace,
                Name    = $"{model.ClassName}.cs",
                Content = modelBuilder.Build(model)
            }));

            // Add Inline Models
            files.AddRange(@interface.InlineModels.Select(model => new GeneratedFile
            {
                Path    = settings.ModelsNamespace,
                Name    = $"{model.ClassName}.cs",
                Content = modelBuilder.Build(model)
            }));

            if (settings.SingleFile)
            {
                return(new[] { new GeneratedFile
                               {
                                   Path = string.Empty,
                                   Name = $"{@interface.Name}.cs",
                                   Content = string.Join("\r\n", files.Select(f => f.Content))
                               } });
            }

            return(files);
        }