/// <summary>
        ///     Maps a concrete type to its interface.
        /// </summary>
        /// <param name="type">
        ///     The type that is being injected.
        /// </param>
        /// <param name="type">
        ///     The runtime type to be mapped
        /// </param>
        /// <returns>
        ///     The interface to which the type is mapped.
        /// </returns>
        internal static Type MapType(Type injectedType, Type type)
        {
            Boolean covariant = type.Name.Contains("LocalCollection");
            Boolean isFactory = injectedType.Name.Contains("Factory");

            // Return generic parameters without mapping.
            if (type.IsGenericParameter)
            {
                return(type);
            }

            // Return the type name for the type if the type is not part of the injected assmebly.
            if (type.Assembly != injectedType.Assembly)
            {
                return(type);
            }

            // Get the interfaces that are defined on the type (not the base types)
            Type[] baseTypes = GeneratedRuntimeType.EnumerateBaseTypes(type).ToArray();
            Type[] baseIface = baseTypes.SelectMany(b => b.GetInterfaces()).ToArray();
            Type[] typeIface = type.GetInterfaces().Except(baseIface).ToArray();

            // Return the first interface, excluding inferred interfaces.
            Type[] inference = typeIface.SelectMany(i => i.GetInterfaces()).ToArray();
            Type[] exclusion = typeIface.Except(baseIface).ToArray();
            return(exclusion.FirstOrDefault() ?? type);
        }
        /// <summary>
        ///     Enumerates all types in the inheritence chain.
        /// </summary>
        internal static IEnumerable <Type> EnumerateBaseTypes(Type runtimeType)
        {
            if (runtimeType.BaseType != null)
            {
                yield return(runtimeType.BaseType);

                foreach (Type type in GeneratedRuntimeType.EnumerateBaseTypes(runtimeType.BaseType))
                {
                    yield return(type);
                }
            }
        }
示例#3
0
        private IEnumerable <CSharpMember> GetFactoryMethods()
        {
            MethodInfo[] methods = this.RuntimeFactory.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(method => (method.DeclaringType == this.RuntimeFactory || this.RuntimeFactory.GetInterfaces().Any(i => method.DeclaringType == i)) && !method.IsSpecialName).ToArray();
            foreach (MethodInfo method in methods)
            {
                String            methodType;
                String[]          methodCode;
                CSharpParameter[] methodParams = method.GetParameters().Select(param => new CSharpParameter()
                {
                    ParameterName      = param.Name,
                    ParameterType      = GeneratedRuntimeType.MapTypeName(this, param.ParameterType),
                    ParameterReference = param.IsOut ? CSharpReference.Out : param.IsRetval ? CSharpReference.Ref : Attribute.IsDefined(param, typeof(ParamArrayAttribute)) ? CSharpReference.Params : CSharpReference.None,
                    XmlSummary         = this.GenerateDocumentation(this.RuntimeDocument, param, "summary")
                }).ToArray();
                IHaveEntity iHaveEntity = this.WrappedType as IHaveEntity;

                // Generate the site-less factory method
                Type returnType = method.ReturnType == typeof(void) ? null : GeneratedRuntimeType.MapType(this.RuntimeModel, method.ReturnType);
                if (returnType == null)
                {
                    methodType = null;
                    methodCode = new String[]
                    {
                        "ConsensusSite site = ConsensusDomain.Configuration[ConsensusDomain.Configuration.DefaultSite];",
                        this.WrappedType.DeclarationName + "." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", new String[] { "site" }.Concat(method.GetParameters().Select(param => param.Name))) + ");"
                    };
                }
                else if (returnType.GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(returnType, false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(returnType, true);
                    methodType = usableTypeName;
                    methodCode = new String[]
                    {
                        "ConsensusSite site = ConsensusDomain.Configuration[ConsensusDomain.Configuration.DefaultSite];",
                        "return " + this.WrappedType.DeclarationName + "." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", new String[] { "site" }.Concat(method.GetParameters().Select(param => param.Name))) + ");"
                    };
                }
                else if (returnType.IsGenericType && String.Equals(returnType.FullName.Split('`')[0], "System.Collections.Generic.IEnumerable", StringComparison.Ordinal) && returnType.GetGenericArguments()[0].GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), true);
                    methodType = "IEnumerable<" + usableTypeName + ">";
                    methodCode = new String[]
                    {
                        "ConsensusSite site = ConsensusDomain.Configuration[ConsensusDomain.Configuration.DefaultSite];",
                        "return " + this.WrappedType.DeclarationName + "." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", new String[] { "site" }.Concat(method.GetParameters().Select(param => param.Name))) + ");"
                    };
                }
                else
                {
                    methodType = GeneratedRuntimeType.GetTypeName(returnType, false);
                    methodCode = new String[]
                    {
                        "ConsensusSite site = ConsensusDomain.Configuration[ConsensusDomain.Configuration.DefaultSite];",
                        "return " + this.WrappedType.DeclarationName + "." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", new String[] { "site" }.Concat(method.GetParameters().Select(param => param.Name))) + ");"
                    };
                }
                yield return(GetFactoryMethod(method, methodType, methodParams, methodCode));

                // Generate the sited factory method
                methodParams = new CSharpParameter[] { new CSharpParameter()
                                                       {
                                                           ParameterName = "site", ParameterType = "ConsensusSite"
                                                       } }.Concat(methodParams).ToArray();
                if (returnType == null)
                {
                    methodType = null;
                    methodCode = new String[]
                    {
                        "IBusinessProvider provider = site.GetService<IBusinessProvider>();",
                        "provider." + iHaveEntity.Entity.Container.ContainerName + "." + iHaveEntity.Entity.EntityName + "." + method.Name + (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") + "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");"
                    };
                }
                else if (returnType.GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(returnType, false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(returnType, true);
                    methodType = usableTypeName;
                    methodCode = new String[]
                    {
                        "IBusinessProvider provider = site.GetService<IBusinessProvider>();",
                        returnTypeName + " model = provider." + iHaveEntity.Entity.Container.ContainerName + "." + iHaveEntity.Entity.EntityName + "." + method.Name + (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") + "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");",
                        "return model == null ? null : new " + methodType + "(model);"
                    };
                }
                else if (returnType.IsGenericType && String.Equals(returnType.FullName.Split('`')[0], "System.Collections.Generic.IEnumerable", StringComparison.Ordinal) && returnType.GetGenericArguments()[0].GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), true);
                    methodType = "IEnumerable<" + usableTypeName + ">";
                    methodCode = new String[]
                    {
                        "IBusinessProvider provider = site.GetService<IBusinessProvider>();",
                        "IEnumerable<" + returnTypeName + "> collection = provider." + iHaveEntity.Entity.Container.ContainerName + "." + iHaveEntity.Entity.EntityName + "." + method.Name + (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") + "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");",
                        "return collection.Select(model => new " + usableTypeName + "(model));"
                    };
                }
                else
                {
                    methodType = GeneratedRuntimeType.GetTypeName(returnType, false);
                    methodCode = new String[]
                    {
                        "IBusinessProvider provider = site.GetService<IBusinessProvider>();",
                        "return provider." + iHaveEntity.Entity.Container.ContainerName + "." + iHaveEntity.Entity.EntityName + "." + method.Name + (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") + "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");"
                    };
                }
                yield return(GetFactoryMethod(method, methodType, methodParams, methodCode));
            }
        }
示例#4
0
        private IEnumerable <CSharpMember> GetModelMethods()
        {
            MethodInfo[] methods = this.RuntimeModel.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(method => (method.DeclaringType == this.RuntimeModel || this.RuntimeModel.GetInterfaces().Any(i => method.DeclaringType == i)) && !method.IsSpecialName).ToArray();
            foreach (MethodInfo method in methods)
            {
                Boolean isHasPermissionMethod = method.Name == "HasPermission";

                String   methodType = null;
                String[] methodCode = null;
                Type     returnType = method.ReturnType == typeof(void) ? null : GeneratedRuntimeType.MapType(this.RuntimeModel, method.ReturnType);
                if (returnType == null)
                {
                    methodCode = new String[]
                    {
                        "this.Model." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");"
                    };
                }
                else if (returnType.GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(returnType, false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(returnType, true);
                    methodType = usableTypeName;
                    methodCode = new String[]
                    {
                        returnTypeName + " model = this.Model." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ")" + ");",
                        "return model == null ? null : new " + methodType + "(model);"
                    };
                }
                else if (returnType.IsGenericType && String.Equals(returnType.FullName.Split('`')[0], "System.Collections.Generic.IEnumerable", StringComparison.Ordinal) && returnType.GetGenericArguments()[0].GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    String returnTypeName = GeneratedRuntimeType.GetTypeName(returnType, false);
                    String usableTypeName = GeneratedRuntimeType.GetTypeName(returnType, true);
                    returnTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), false);
                    methodType     = "IEnumerable<" + usableTypeName + ">";
                    methodCode     = new String[]
                    {
                        "return this.Model." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ")" +
                        ".Select(model => new " + methodType + "(model));"
                    };
                }
                else
                {
                    methodType = GeneratedRuntimeType.GetTypeName(returnType, false);
                    methodCode = new String[]
                    {
                        "return this.Model." + method.Name +
                        (method.GetGenericArguments().Length == 0 ? "" : "<" + String.Join(", ", method.GetGenericArguments().Select(gen => GeneratedRuntimeType.GetTypeName(gen, false))) + ">") +
                        "(" + String.Join(",", method.GetParameters().Select(param => param.Name)) + ");"
                    };
                }

                yield return(new CSharpMethod(this)
                {
                    Name = method.Name,
                    Attributes = method.GetCustomAttributes().Select(attr => attr.GetType().Name).ToArray(),
                    MethodType = methodType,
                    MethodGenerics = method.GetGenericArguments().Select(generic => new CSharpGeneric()
                    {
                        GenericName = generic.Name,
                        XmlSummary = this.GenerateDocumentation(this.RuntimeDocument, generic, "summary"),
                        GenericConstraints = generic.GetGenericParameterConstraints().Select(con => con.Name).ToArray()
                    }).ToArray(),
                    MethodParameters = method.GetParameters().Select(param => new CSharpParameter()
                    {
                        ParameterName = param.Name,
                        ParameterType = GeneratedRuntimeType.MapTypeName(this, param.ParameterType),
                        ParameterReference = param.IsOut ? CSharpReference.Out : param.IsRetval ? CSharpReference.Ref : Attribute.IsDefined(param, typeof(ParamArrayAttribute)) ? CSharpReference.Params : CSharpReference.None,
                        XmlSummary = this.GenerateDocumentation(this.RuntimeDocument, param, "summary")
                    }).ToArray(),
                    MethodCode = methodCode
                });
            }
        }
示例#5
0
        private IEnumerable <CSharpMember> GetProperties()
        {
            PropertyInfo[] properties = this.RuntimeModel.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(property => !property.DeclaringType.IsAbstract || this.RuntimeModel.GetInterfaces().Any(i => property.DeclaringType == i)).ToArray();
            foreach (PropertyInfo property in properties)
            {
                Type   returnType     = GeneratedRuntimeType.MapType(this.RuntimeModel, property.PropertyType);
                String returnTypeName = GeneratedRuntimeType.GetTypeName(returnType, false);
                String usableTypeName = GeneratedRuntimeType.GetTypeName(returnType, true);
                String fieldName      = "_" + property.Name.Substring(0, 1).ToLower() + property.Name.Substring(1);

                // Encapsulate model instance properties
                if (returnType.GetInterfaces().Any(iface => String.Equals(iface.Name, "IModel", StringComparison.Ordinal)))
                {
                    yield return(new CSharpField(this)
                    {
                        XmlSummary = "The client-consumable proxy instance for the <see cref=\"" + property.Name + "\" /> member.",
                        FieldType = usableTypeName,
                        FieldModifier = CSharpModifiers.Private,
                        Name = fieldName
                    });

                    yield return(new CSharpProperty(this)
                    {
                        Name = property.Name,
                        Attributes = property.GetCustomAttributes().Select(attr => attr.GetType().Name).ToArray(),
                        PropertyGetter = !property.CanRead ? null : new CSharpAccessor()
                        {
                            AccessorCode = new String[]
                            {
                                "if (" + fieldName + " == null && this.Model." + property.Name + " != null)",
                                "    " + fieldName + " = new " + usableTypeName + "(this.Model." + property.Name + ");",
                                "if (" + fieldName + " == null)",
                                "    " + fieldName + " = " + usableTypeName + ".Create();",
                                "return " + fieldName + ";"
                            }
                        },
                        PropertySetter = !property.CanWrite ? null : new CSharpAccessor()
                        {
                            AccessorCode = new String[]
                            {
                                fieldName + " = value;",
                                "this.Model." + property.Name + " = value.Model;"
                            }
                        },
                        PropertyType = usableTypeName,
                        XmlSummary = this.GenerateDocumentation(this.RuntimeDocument, property, "summary"),
                        XmlRemarks = this.GenerateDocumentation(this.RuntimeDocument, property, "remarks")
                    });
                }

                // Encapsulate model collection properties
                else if (returnType.IsGenericType && String.Equals(returnType.FullName.Split('`')[0], "Consensus.Collection", StringComparison.Ordinal))
                {
                    returnTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), false);
                    usableTypeName = GeneratedRuntimeType.GetTypeName(GeneratedRuntimeType.MapType(this.RuntimeModel, returnType.GetGenericArguments().First()), true);
                    yield return(new CSharpField(this)
                    {
                        XmlSummary = "The client-consumable proxy collection for the <see cref=\"" + property.Name + "\" /> member.",
                        FieldType = "ProxyCollection<" + usableTypeName + ", " + returnTypeName + ">",
                        FieldModifier = CSharpModifiers.Private,
                        Name = fieldName
                    });

                    yield return(new CSharpProperty(this)
                    {
                        Name = property.Name,
                        Attributes = property.GetCustomAttributes().Select(attr => attr.GetType().Name).ToArray(),
                        PropertyGetter = !property.CanRead ? null : new CSharpAccessor()
                        {
                            AccessorCode = new String[]
                            {
                                "if (" + fieldName + " == null)",
                                "    " + fieldName + " = new ProxyCollection<" + usableTypeName + ", " + returnTypeName + ">(this.Model." + property.Name + ", model => new " + usableTypeName + "(model));",
                                "return " + fieldName + ";"
                            }
                        },
                        PropertyType = "Collection<" + usableTypeName + ">",
                        XmlSummary = this.GenerateDocumentation(this.RuntimeDocument, property, "summary"),
                        XmlRemarks = this.GenerateDocumentation(this.RuntimeDocument, property, "remarks")
                    });
                }

                // Encapsulate other properties
                else
                {
                    yield return(new CSharpProperty(this)
                    {
                        Name = property.Name,
                        Attributes = property.GetCustomAttributes().Select(attr => attr.GetType().Name).ToArray(),
                        PropertyGetter = !property.CanRead ? null : new CSharpAccessor()
                        {
                            AccessorCode = new String[] { "return this.Model." + property.Name + ";" }
                        },
                        PropertySetter = !property.CanWrite ? null : new CSharpAccessor()
                        {
                            AccessorCode = new String[] { "this.Model." + property.Name + " = value;" }
                        },
                        PropertyType = returnTypeName,
                        XmlSummary = this.GenerateDocumentation(this.RuntimeDocument, property, "summary"),
                        XmlRemarks = this.GenerateDocumentation(this.RuntimeDocument, property, "remarks")
                    });
                }
            }
        }
        /// <summary>
        ///     Gets the declaration name for a runtime type.
        /// </summary>
        private static String GetTypeName(Type injectedType, Type type)
        {
            // Get the true name of the type
            String typeName = type.Name;

            if (type.GetGenericArguments().Length != 0)
            {
                typeName = typeName.Split('`').First() + "<" + String.Join(", ", type.GetGenericArguments().Select(gen => GeneratedRuntimeType.MapTypeName(injectedType, gen))) + ">";
            }

            // Prefix the type with its namespace and suffix array types with square brackets
            if (type.IsGenericParameter)
            {
                typeName = type.Name;
            }
            else
            {
                typeName = type.Namespace + "." + typeName;
            }

            if (type.IsArray && !typeName.EndsWith("[]"))
            {
                typeName += "[]";
            }

            return(typeName);
        }
 /// <summary>
 ///     Maps a concrete type to its interface.
 /// </summary>
 /// <param name="type">
 ///     The runtime type to be mapped
 /// </param>
 /// <returns>
 ///     The interface to which the type is mapped.
 /// </returns>
 public static String MapTypeName(GeneratedEncapsulatorClass injector, Type type)
 {
     return(GeneratedRuntimeType.MapTypeName(injector.RuntimeModel, type));
 }
 /// <summary>
 ///     Maps a concrete type to its interface.
 /// </summary>
 /// <param name="type">
 ///     The runtime type to be mapped
 /// </param>
 /// <returns>
 ///     The interface to which the type is mapped.
 /// </returns>
 public static String MapTypeName(GeneratedInjectorClass injector, Type type)
 {
     return(GeneratedRuntimeType.MapTypeName(injector.RuntimeType, type));
 }
 /// <summary>
 ///     Maps a concrete type to its interface.
 /// </summary>
 /// <param name="type">
 ///     The type that is being injected.
 /// </param>
 /// <param name="type">
 ///     The runtime type to be mapped
 /// </param>
 /// <returns>
 ///     The interface to which the type is mapped.
 /// </returns>
 private static String MapTypeName(Type injectedType, Type type)
 {
     return(GeneratedRuntimeType.GetTypeName(injectedType, GeneratedRuntimeType.MapType(injectedType, type)));
 }
 /// <summary>
 ///     Gets the declaration name for a runtime type.
 /// </summary>
 public static String GetTypeName(Type type)
 {
     return(GeneratedRuntimeType.GetTypeName(type, false));
 }