/// <summary> /// Maps a property to be serialized to JSON when saving to the database, /// or deserialized from JSON when loading from the database. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TProp"></typeparam> /// <param name="mapBase"></param> /// <param name="memberExpression"></param> /// <returns></returns> public static PropertyPart MapJson <T, TProp>(this ClasslikeMapBase <T> mapBase, Expression <Func <T, TProp> > memberExpression) { var expr = Expression.Lambda <Func <T, object> >( Expression.Convert(memberExpression.Body, typeof(object)), memberExpression.Parameters); return(mapBase.Map(expr) .Length(8096) .CustomType <JsonMappableType <TProp> >()); }
private static void AutoMap <T>(ClasslikeMapBase <T> mapping, PropertyInfo property) { var ownershipAttribute = (OwnershipAttribute)property.GetCustomAttributes(typeof(OwnershipAttribute), true).FirstOrDefault(); Ownership ownership = ownershipAttribute == null ? Ownership.None : ownershipAttribute.Ownership; var expression = ExpressionBuilder.Create <T>(property.ToMember()); if (property.PropertyType.IsGenericType && typeof(IEnumerable <EntityBase>).IsAssignableFrom(property.PropertyType)) { var childType = property.PropertyType.GetGenericArguments()[0]; if (ownership == Ownership.Shared) { MapManyToMany <T>(mapping, property, childType); } else { MapOneToMany <T>(mapping, property, ownership, childType); } } else if (property.PropertyType.IsSubclassOf(typeof(EntityBase))) { MapReference <T>(mapping, property, ownership); } else if (property.PropertyType.Name.EndsWith("Component") || property.PropertyType.Namespace.Contains("Components")) { //TODO: AutoMap components //MapComponent<T>(mapping, property, ownership) or some such. } else if (property.PropertyType.IsInterface) { //TODO: AutoMap anys } else if (IsEnum(property)) { mapping.Map(CreatePropertyExpression <T>(property)).CustomType(property.PropertyType.AssemblyQualifiedName); } else { mapping.Map(CreatePropertyExpression <T>(property)).Column("[" + property.Name + "]"); } }
//protected string TableName = typeof(T).Name.Split(".".ToCharArray()).Last(); //protected IList<PropertyInfo> Properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public) // .Where(p => p.GetMethod.IsVirtual && p.GetMethod.IsAbstract == false) // .ToList(); public static void MapPrimitiveTypes <T>(this ClasslikeMapBase <T> dynamicMap, IList <string> primitiveColumns, IEnumerable <PropertyInfo> properties) where T : DynamicClass { foreach (var column in primitiveColumns) { if (column == "CanDelete" || column == "Id") { continue; } var propertyType = properties.Where(p => p.Name == column).Single().PropertyType; if (propertyType == typeof(byte[])) { //if (DataStore.ProviderName.Contains("MySql")) //{ // dynamicMap.Map(FluentNHibernate.Reveal.Member<T>(column)).Not.Nullable().CustomSqlType("LONGBLOB").Length(int.MaxValue); //} if (DataStore.SetCustomSqlTypes == true) { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Not.Nullable().CustomSqlType("varbinary(max)").Length(int.MaxValue); } else { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Not.Nullable().Length(int.MaxValue); } } else if (propertyType == typeof(LongString)) { //if (DataStore.ProviderName.Contains("MySql")) //{ // dynamicMap.Map(FluentNHibernate.Reveal.Member<T>(column)).Nullable().CustomType<LongString>().CustomSqlType("LONGTEXT").Length(int.MaxValue); //} if (DataStore.SetCustomSqlTypes == true) { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Nullable().CustomType <LongString>().CustomSqlType("nvarchar(max)").Length(int.MaxValue); } else { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Nullable().CustomType <LongString>().Length(int.MaxValue); } } else if (IsNullable(propertyType)) { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Nullable(); } else { dynamicMap.Map(FluentNHibernate.Reveal.Member <T>(column)).Not.Nullable(); } } }
private void BuildBasicColumn <TChild>(Expression <Func <TChild, object> > exp, IBasicColumn clmn, ClasslikeMapBase <TChild> factory) { var name = clmn.Name; //if(name==null) // return; var mapResult = factory.Map(exp); if (clmn.NotNull) { mapResult.Not.Nullable(); } if (!string.IsNullOrEmpty(clmn.Name)) { mapResult.Column(clmn.Name); } if (!string.IsNullOrEmpty(clmn.CustomSqlType)) { mapResult.CustomSqlType(clmn.CustomSqlType); } if (!string.IsNullOrEmpty(clmn.Default)) { mapResult.Default(clmn.Default); } if (clmn.Length > 0) { mapResult.Length((int)clmn.Length); } if (clmn.IsIndex) { mapResult.Index(name); } if (clmn.NotEdit) { mapResult.Not.Insert().Not.Update().Generated.Never(); } mapResult.Not.LazyLoad(); }