Ejemplo n.º 1
0
        internal static void AddHasTitleProperty(TypeUsageInfo ihastitle, EntityInfo entity, TableContent tableContent)
        {
            var prop = new PropertyInfo("EntityTitle", new FieldInfo("EntityTitle", (typeof(string)).ToUsageInfo()),
                                        new PropertyInvokerInfo(string.Format("return {0}.ToString();", tableContent.TitleFieldName)),
                                        null);

            prop.ExplicitInterface = ihastitle;
            entity.AddProperty(prop);
        }
Ejemplo n.º 2
0
        public virtual TypeUsageInfo GetSimplePropertyTypeInfo(IPropertyMetadata <TTable, TTableContent, TColumn, TColumnContent> data)
        {
            var column = data.Column;

            if (column.TypeInfo.IsValueType && !column.IsRequired)
            {
                return(TypeUsageInfo.CreateNullable(column.TypeInfo));
            }
            return(column.TypeInfo);
        }
Ejemplo n.º 3
0
        public List <EntityInfo> Load(IEnumerable <MSSQLTable <TableContent, ColumnContent> > tables)
        {
            var entities = _entityLoader.Load(tables);

            //TODO: Remove it after implement interfaces in entities
            var iremovable      = TypeUsageInfo.CreateInterface("IRemovable", "ProfiCraftsman.Contracts");
            var ihasid          = TypeUsageInfo.CreateInterface("IHasId", "ProfiCraftsman.Contracts", new[] { typeof(int).ToUsageInfo() });
            var iintervalfields = TypeUsageInfo.CreateInterface("IIntervalFields", "ProfiCraftsman.Contracts");
            var isystemfields   = TypeUsageInfo.CreateInterface("ISystemFields", "ProfiCraftsman.Contracts");
            var ihastitle       = TypeUsageInfo.CreateInterface("IHasTitle", "ProfiCraftsman.Contracts", new[] { typeof(int).ToUsageInfo() });

            foreach (var entity in entities)
            {
                entity.InheritsFrom(iremovable);
                entity.InheritsFrom(ihasid);
                #region IntervalFields
                {
                    var fromDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "FromDate");
                    var toDate   = entity.SimpleProperties.FirstOrDefault(p => p.Name == "ToDate");
                    if (fromDate != null && toDate != null)
                    {
                        entity.InheritsFrom(iintervalfields);

                        AddExplicitProperty(fromDate, iintervalfields, entity);
                        AddExplicitProperty(toDate, iintervalfields, entity);
                    }
                }
                #endregion

                var tableContent = tables.
                                   First(table => table.Name == entity.TableName && table.Schema == entity.TableSchemaName).Content;

                if (!string.IsNullOrEmpty(tableContent.TitleFieldName))
                {
                    entity.InheritsFrom(ihastitle);
                    AddHasTitleProperty(ihastitle, entity, tableContent);
                }


                #region SystemFields
                {
                    var createDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "CreateDate");
                    var changeDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "ChangeDate");
                    if (createDate != null && changeDate != null)
                    {
                        entity.InheritsFrom(isystemfields);

                        AddCreateDateProperty(createDate, isystemfields, entity);
                        AddChangeDateProperty(createDate, changeDate, isystemfields, entity);
                    }
                }
                #endregion
            }
            return(entities);
        }
Ejemplo n.º 4
0
        public List <ModelInfo> Generate(IEnumerable <ITable <TableContent, ColumnContent> > tables, IEnumerable <EntityInfo> entities)
        {
            var result     = new List <ModelInfo>();
            var tableArray = tables.ToArray();

            var baseModel       = TypeUsageInfo.CreateClass("BaseModel", string.Empty);
            var iintervalfields = TypeUsageInfo.CreateInterface("IIntervalModelFields", string.Empty);

            foreach (var entity in entities)
            {
                var table        = tableArray.First(t => t.Name == entity.TableName && t.Schema == entity.TableSchemaName);
                var modelColumns = table.Columns.Where(c => c.Content.InModel).ToArray();
                if (modelColumns.Length == 0)
                {
                    continue;
                }

                var properties = modelColumns.Select(c => new PropertyModelInfo(entity.SimpleProperties.First(p => p.ColumnName == c.Name), c)).ToList();
                var model      = new ModelInfo(entity, table, properties);

                model.Attributes.Add(AttributeDictionary.DataContract);
                model.InheritsFrom(baseModel);

                #region IntervalFields
                {
                    var fromDate = properties.FirstOrDefault(p => p.Name == "fromDate");
                    var toDate   = properties.FirstOrDefault(p => p.Name == "toDate");
                    if (fromDate != null && toDate != null)
                    {
                        entity.InheritsFrom(iintervalfields);

                        EntitiesManager.AddExplicitProperty(fromDate, iintervalfields, entity);
                        EntitiesManager.AddExplicitProperty(toDate, iintervalfields, entity);
                    }
                }
                #endregion

                foreach (var property in model.ModelProperties)
                {
                    if (property.Content.IsModelRequired)
                    {
                        property.Attributes.Add(AttributeDictionary.Required);
                    }
                    property.Attributes.Add(AttributeDictionary.DataMember);
                    property.Description = string.Format("Model property for <see cref=\"{0}.{1}\"/> entity", entity.Name, property.Property.Name);
                }

                result.Add(model);
            }

            return(result);
        }
Ejemplo n.º 5
0
        internal static void AddExplicitProperty(PropertyInfo property, TypeUsageInfo iintervalfields, EntityInfo entity)
        {
            if (property.Type.IsNullable)
            {
                return;
            }
            var prop = new PropertyInfo(property.Name, TypeUsageInfo.CreateNullable(property.Type),
                                        new PropertyInvokerInfo(string.Format("return {0};", property.Name)),
                                        new PropertyInvokerInfo(string.Format("if(value.HasValue){0} = value.Value; else throw new ArgumentNullException(\"value\");", property.Name)));

            prop.ExplicitInterface = iintervalfields;
            entity.AddProperty(prop);
        }
Ejemplo n.º 6
0
        internal MSSQLTypeDesc(short typeIndex, NativeTypes baseType, string name, TypeOptions options,
            TypeUsageInfo typeInfo)
        {
            Contract.Requires(typeIndex > 256);
            Contract.Requires(typeInfo != null);
            
            Id = typeIndex;
            BaseType = baseType;
            IsDerived = Id != (short) BaseType;

            Name = name;
            Options = options;
            TypeInfo = typeInfo;
        }
Ejemplo n.º 7
0
        internal static void AddCreateDateProperty(PropertyInfo property, TypeUsageInfo iintervalfields, EntityInfo entity)
        {
            var getter = string.Format("return {0};", property.Name);

            if (property.Type.IsNullable)
            {
                getter = string.Format("if({0}.HasValue) return {0}.Value; else return DateTime.Now;", property.Name);
            }

            var prop = new PropertyInfo(property.Name, (typeof(DateTime)).ToUsageInfo(),
                                        new PropertyInvokerInfo(getter),
                                        new PropertyInvokerInfo(string.Format("{0} = value;", property.Name)));

            prop.ExplicitInterface = iintervalfields;
            entity.AddProperty(prop);
        }
Ejemplo n.º 8
0
        private static string ResolveTypeArgument(TypeUsageInfo argument)
        {
            var modifier = string.Empty;

            if (argument.IsTypeArgument)
            {
                switch (argument.TypeArgumentConfiguration.Modifier)
                {
                case TypeArgumentModifier.In:
                    modifier = "in";
                    break;

                case TypeArgumentModifier.Out:
                    modifier = "out";
                    break;
                }
            }
            return(modifier.Postfix() + argument.CodeName());
        }
Ejemplo n.º 9
0
 public SimplePropertyEntityInfo(string name, TypeUsageInfo type)
     : base(name, type)
 {
     IncludeToShallowCopy = true;
 }
Ejemplo n.º 10
0
 private static void AddType(this ITypeDictionary dictionary, short typeIndex, NativeTypes baseType, string searchName, TypeOptions config, TypeUsageInfo info, params string[] aliases)
 {
     AddToDictionary(dictionary, new MSSQLTypeDesc(typeIndex, baseType, searchName, config, info), aliases);
 }
Ejemplo n.º 11
0
 private static void AddType(this ITypeDictionary dictionary, short typeIndex, string searchName, TypeOptions config, TypeUsageInfo info, params string[] aliases)
 {
     AddType(dictionary, typeIndex, (NativeTypes)typeIndex, searchName, config, info, aliases);
 }
Ejemplo n.º 12
0
        private static MSSQLTypeDescDictionary Initialize()
        {
            //NOTE: For type mappings see https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx
            var dictionary = new MSSQLTypeDescDictionary();

            dictionary.AddType <byte[]>(TypeIndexes.Image, "image", TypeOptions.IsLob);
            dictionary.AddType <string>(TypeIndexes.Text, "text", TypeOptions.HasCollation | TypeOptions.IsLob);
            dictionary.AddType <Guid>(TypeIndexes.UniqueIdentifier, "uniqueidentifier", TypeOptions.None);
            dictionary.AddType <byte>(TypeIndexes.TinyInt, "tinyint", TypeOptions.None);
            dictionary.AddType <short>(TypeIndexes.SmallInt, "smallint", TypeOptions.None);
            dictionary.AddType <int>(TypeIndexes.Int, "int", TypeOptions.None, "integer");
            dictionary.AddType <DateTime>(TypeIndexes.SmallDateTime, NativeTypes.SmallDateTime, "smalldatetime", TypeOptions.None);
            dictionary.AddType <float>(TypeIndexes.Real, NativeTypes.Real, "real", TypeOptions.None);
            dictionary.AddType <decimal>(TypeIndexes.Money, NativeTypes.Money, "money", TypeOptions.None);
            dictionary.AddType <DateTime>(TypeIndexes.DateTime, "datetime", TypeOptions.None);

            dictionary.AddType <double>(TypeIndexes.Float, "float", TypeOptions.HasPrecision, "Double precision");
            dictionary.AddType <string>(TypeIndexes.NText, "ntext", TypeOptions.HasCollation | TypeOptions.IsLob | TypeOptions.IsUnicode);
            dictionary.AddType <bool>(TypeIndexes.Bit, "bit", TypeOptions.None);
            dictionary.AddType <decimal>(TypeIndexes.Decimal, "decimal", TypeOptions.HasPrecision | TypeOptions.HasScale, "dec");
            dictionary.AddType <decimal>(TypeIndexes.Numeric, "numeric", TypeOptions.HasPrecision | TypeOptions.HasScale);
            dictionary.AddType <decimal>(TypeIndexes.SmallMoney, "smallmoney", TypeOptions.None);
            dictionary.AddType <long>(TypeIndexes.BigInt, "bigint", TypeOptions.None);
            dictionary.AddType <byte[]>(
                TypeIndexes.Varbinary, NativeTypes.Varbinary, "varbinary", TypeOptions.HasLength | TypeOptions.IsLobWithMax, "binary varying");
            dictionary.AddType <string>(TypeIndexes.Varchar,
                                        "varchar", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsLobWithMax, "char varying", "character varying");
            dictionary.AddType <byte[]>(TypeIndexes.Binary, "binary", TypeOptions.HasLength | TypeOptions.IsFixedLength);
            dictionary.AddType <string>(TypeIndexes.Char, "char", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsFixedLength);
            dictionary.AddType <byte[]>(TypeIndexes.TimeStamp, "timestamp", TypeOptions.None, "rowversion");
            dictionary.AddType <string>(
                TypeIndexes.NVarchar,
                "nvarchar",
                TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsLobWithMax | TypeOptions.IsUnicode,
                "nchar varying", "ncharacter varying", "national char varying", "national character varying");
            dictionary.AddType <string>(
                TypeIndexes.NChar,
                "nchar", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsUnicode | TypeOptions.IsFixedLength, "ncharacter", "national character");

            dictionary.AddType <string>(TypeIndexes.Sysname, NativeTypes.NVarchar, "sysname", TypeOptions.None);
            dictionary.AddType <string>(TypeIndexes.Xml, "xml", TypeOptions.IsLob);
            dictionary.AddType <DateTime>(TypeIndexes.Date, "date", TypeOptions.None);
            dictionary.AddType <DateTime>(TypeIndexes.Time, "time", TypeOptions.HasScale);
            dictionary.AddType <DateTime>(TypeIndexes.DateTime2, "datetime2", TypeOptions.HasScale);
            dictionary.AddType <DateTimeOffset>(TypeIndexes.DateTimeOffset, "datetimeoffset", TypeOptions.HasScale);


            dictionary.AddType(TypeIndexes.Geometry, NativeTypes.Clr, "geometry", TypeOptions.None, TypeUsageInfo.CreateClass("DbGeometry", "System.Data.Spatial"));
            dictionary.AddType(TypeIndexes.Geography, NativeTypes.Clr, "geography", TypeOptions.None, TypeUsageInfo.CreateClass("DbGeography", "System.Data.Spatial"));
            //Check this conversation for support http://entityframework.codeplex.com/discussions/415185
            dictionary.AddType(TypeIndexes.HierarchyId, NativeTypes.Clr, "hierarchyid", TypeOptions.None, TypeUsageInfo.CreateClass("SqlHierarchyId", "Microsoft.SqlServer.Types"));

            dictionary.AddType <object>(TypeIndexes.SqlVariant, "sql_variant", TypeOptions.None);
            dictionary.AddType <object>(TypeIndexes.Table, TypeOptions.IsVirtual);
            dictionary.AddType <object>(TypeIndexes.Clr, TypeOptions.IsVirtual);

            return(dictionary);
        }
Ejemplo n.º 13
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
 /// </summary>
 public FieldInfo(string name, TypeUsageInfo type)
 {
     Name = name;
     Type = type;
 }
Ejemplo n.º 14
0
 public TypeUsageInfo GetTypeUsage(params TypeUsageInfo[] typeArguments)
 {
     return(TypeUsageInfo.Create(Name, Namespace, TypeConfiguration, typeArguments: typeArguments));
 }
Ejemplo n.º 15
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
 /// </summary>
 public FieldInfo(string name, TypeUsageInfo type)
 {
     Name = name;
     Type = type;
 }
Ejemplo n.º 16
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
 /// </summary>
 private NavigationPropertyEntityInfo(string name, TypeUsageInfo type, TypeUsageInfo initializeType)
     : base(name, type, false)
 {
     IsVirtual      = true;
     InitializeType = initializeType;
 }