private void Join <SubType>(ClasslikeMapBase <T> map, Action <ClasslikeMapBase <SubType> > action, string tableName) where SubType : EntityBase { var t = typeof(T); var st = typeof(SubType); tableName = string.IsNullOrEmpty(tableName) ? st.Name : tableName; var subclassMap = new SubclassMap <SubType>(); subclassMap.Extends <T>(); using (_mapper.OverrideJoinContext("[" + tableName + "]", tableName + "ID")) { if (subclassMap.AnythingToMap()) { subclassMap.Join(_mapper.JoinContext.TableName, x => { x.KeyColumn(_mapper.JoinContext.KeyColumnName); x.AutoMap(); if (action != null) { action(x); } }); } else if (action != null) { action(subclassMap); } } _mapper.Config.FluentMappings.Add(subclassMap); }
private static void MapManyToMany <T>(ClasslikeMapBase <T> mapping, PropertyInfo property, Type childType) { //Make generic method for MapManyToManyCollection<T,TChild> MethodInfo method = _mapManyToManyMethod.MakeGenericMethod(typeof(T), childType); method.Invoke(null, new object[] { mapping, property }); }
public static void JoinContext <T>(this ClasslikeMapBase <T> mapping, Type joinContext, Action <ClasslikeMapBase <T> > action) { using (AutomaticMapper.Current.OverrideJoinContext("[" + joinContext.Name + "]", joinContext.Name + "ID")) { if (action != null) { action(mapping); } } }
public static void ChangeAuditInfo <T>(this ClasslikeMapBase <T> map) where T : AuditedPersistentObject { map.Component(x => x.ChangeAuditInfo, cp => { cp.Map(x => x.Created); cp.Map(x => x.Updated); cp.Map(x => x.CreatedBy).Column("CreatedBy"); cp.Map(x => x.UpdatedBy).Column("UpdatedBy"); }); }
/// <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 AutoMapInterfaces <T>(ClasslikeMapBase <T> mapping) { Type t = typeof(T); foreach (var @interface in MappableInterfaces(t)) { MethodInfo mapInterfaceMethod = _interfaceMappings[@interface].MakeGenericMethod(t); mapInterfaceMethod.Invoke(null, new object[] { mapping }); } }
/// <summary> /// Implementation for late binding generic call. Shouldn't be called directly Called through reflection. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TChild"></typeparam> /// <param name="mapping"></param> /// <param name="property"></param> /// <param name="ownership"></param> private static void MapManyToManyImplementation <T, TChild>(ClasslikeMapBase <T> mapping, PropertyInfo property) { var part = mapping.HasManyToMany <TChild>(CreateCollectionExpression <T, TChild>(property)); string parentName = typeof(T).Name; string childName = typeof(TChild).Name; part.Table(parentName + childName + "Xref") .ParentKeyColumn(parentName + "ID") .ChildKeyColumn(childName + "ID") .Cascade.SaveUpdate(); }
//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(); } } }
public static bool AnythingToMap <T>(this ClasslikeMapBase <T> mapping) { Type t = typeof(T); if (GetMappableProperties(t).Any()) { return(true); } if (MappableInterfaces(t).Any()) { return(true); } return(false); }
public static void AutoMap <T>(this ClasslikeMapBase <T> mapping) { Type t = typeof(T); var mappableProperties = GetMappableProperties(t); if (mappableProperties.Any()) { foreach (var property in mappableProperties) { if (!property.GetGetMethod().IsAbstract) { AutoMap <T>(mapping, property); } } AutoMapInterfaces <T>(mapping); } }
public static void MapNonPrimitiveTypes <T>(this ClasslikeMapBase <T> dynamicMap, string tableName, IList <PropertyInfo> nonPrimitiveColumns, bool nullableReference) where T : DynamicClass { foreach (var column in nonPrimitiveColumns) { var types = new List <Type>(); types.Add(typeof(T)); types.Add(column.PropertyType); var method = typeof(FluentNHibernate.Reveal).GetMethods().Where(m => m.Name == "Member").Last(); var generic = method.MakeGenericMethod(typeof(T), column.PropertyType); dynamic tmp = generic.Invoke(dynamicMap, new object[] { column.Name }); if (nullableReference == false) // this is always false at the moment. { if (DynamicClass.SetIdsToBeAssigned == true) //means we are doing a backup { dynamicMap.References(tmp) .Nullable() .NotFound.Ignore() .ForeignKey("FK_" + tableName + "_" + column.Name) // Will prevent deleting child object without deleting parent object .LazyLoad(Laziness.False); } else { dynamicMap.References(tmp) .Not.Nullable() .NotFound.Exception() .ForeignKey("FK_" + tableName + "_" + column.Name) // Will prevent deleting child object without deleting parent object .LazyLoad(Laziness.False); } } else { //TODO: Should this be nullable? Need to find examples of when it comes here. dynamicMap.References(tmp) //.Not.Nullable() .Nullable() .Cascade.None() .NotFound.Ignore() .LazyLoad(Laziness.False); } } }
/// <summary> /// Implementation for late binding generic call. Shouldn't be called directly Called through reflection. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TChild"></typeparam> /// <param name="mapping"></param> /// <param name="property"></param> /// <param name="ownership"></param> private static void MapReferenceImplementation <T, TChild>(ClasslikeMapBase <T> mapping, PropertyInfo property, Ownership ownership) { var part = mapping.References(CreateReferenceExpression <T, TChild>(property)); if (ownership == Ownership.None) { part.Cascade.None(); } else if (ownership == Ownership.Exclusive) { part.Cascade.All(); } else if (ownership == Ownership.Shared) { part.Cascade.SaveUpdate(); } part.Column(property.Name + "ID"); //part.ForeignKey("FK_" + typeof(T).Name + "_" + typeof(TChild).Name); }
private void Subclass <TSubType>(ClasslikeMapBase <T> mapping, Action <ClasslikeMapBase <TSubType> > action) where TSubType : EntityBase { var t = typeof(T); var st = typeof(TSubType); var subclassMap = new SubclassMap <TSubType>(); subclassMap.Extends <T>(); if (_mapper.JoinContext != null) { if (subclassMap.AnythingToMap()) { subclassMap.Join(_mapper.JoinContext.TableName, x => { x.KeyColumn(_mapper.JoinContext.KeyColumnName); x.AutoMap(); if (action != null) { action(x); } }); } else if (action != null) { action(subclassMap); } } else { if (subclassMap.AnythingToMap()) { subclassMap.AutoMap(); } if (action != null) { action(subclassMap); } } _mapper.Config.FluentMappings.Add(subclassMap); }
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 + "]"); } }
public static void MapLists <T>(this ClasslikeMapBase <T> dynamicMap, IList <PropertyInfo> listColumns) where T : DynamicClass { var tableName = typeof(T).Name.Split(".".ToCharArray()).Last(); foreach (var column in listColumns) { var method = typeof(FluentNHibernate.Reveal).GetMethods().Where(m => m.Name == "Member").Last(); var genericType = column.PropertyType.GenericTypeArguments.First(); var listType = typeof(IEnumerable <>).MakeGenericType(new[] { genericType }); var generic = method.MakeGenericMethod(typeof(T), listType); dynamic tmp = generic.Invoke(dynamicMap, new object[] { column.Name }); //HasMany<object>(x => x.Id).KeyColumn("").Inverse().AsSet(); // for intellisense dynamicMap.HasMany(tmp).KeyColumn(tableName + "_id").Inverse().AsSet().Not.LazyLoad();//.Cascade.None(); } }
/// <summary> /// /// </summary> /// <param name="map"></param> /// <param name="hierarchyTableName"></param> /// <typeparam name="T"></typeparam> public static void TreeMap <T>(this ClasslikeMapBase <T> map, string hierarchyTableName) where T : TreeNode <T> { map.References(x => x.Parent) .LazyLoad() .Nullable() .Column("PARENT_ID"); map.HasMany(x => x.Children) .Access.ReadOnlyPropertyThroughCamelCaseField() .Cascade.All() .Inverse() .AsSet() .LazyLoad() .BatchSize(250) .KeyColumn("PARENT_ID"); map.HasManyToMany(x => x.Ancestors) .Access.ReadOnlyPropertyThroughCamelCaseField() .Cascade.None() .AsSet() .LazyLoad() .BatchSize(250) .Table(hierarchyTableName) .ParentKeyColumn("CHILD_ID") .ChildKeyColumn("PARENT_ID") .ForeignKeyConstraintNames(string.Format("FK_{0}_CHILD", hierarchyTableName), null); map.HasManyToMany(x => x.Descendants) .Access.ReadOnlyPropertyThroughCamelCaseField() .Cascade.All() .Inverse() .AsSet() .LazyLoad() .BatchSize(250) .Table(hierarchyTableName) .ParentKeyColumn("PARENT_ID") .ChildKeyColumn("CHILD_ID") .ForeignKeyConstraintNames(string.Format("FK_{0}_PARENT", hierarchyTableName), null); }
/// <summary> /// Implementation for late binding generic call. Shouldn't be called directly Called through reflection. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TChild"></typeparam> /// <param name="mapping"></param> /// <param name="property"></param> /// <param name="ownership"></param> private static void MapOneToManyImplementation <T, TChild>(ClasslikeMapBase <T> mapping, PropertyInfo property, Ownership ownership) { var part = mapping.HasMany <TChild>(CreateCollectionExpression <T, TChild>(property)); part.KeyColumn(typeof(T).Name + "ID"); switch (ownership) { case Ownership.Exclusive: part.Cascade.All(); break; case Ownership.None: part.Cascade.None(); break; case Ownership.Shared: default: part.Cascade.AllDeleteOrphan(); break; } //part.ForeignKeyConstraintName("FK_" + typeof(T).Name + "_" + typeof(TChild).Name); }
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(); }
public static JoinBuilder <T> JoinTo <T>(this ClasslikeMapBase <T> map) where T : EntityBase { return(new JoinBuilder <T>(map, AutomaticMapper.Current)); }
public static SubclassBuilder <T> AddSubclass <T>(this ClasslikeMapBase <T> mapping) where T : EntityBase { return(new SubclassBuilder <T>(mapping, AutomaticMapper.Current)); }
private static void MapReference <T>(ClasslikeMapBase <T> mapping, PropertyInfo property, Ownership ownership) { MethodInfo method = _mapReferenceMethod.MakeGenericMethod(typeof(T), property.PropertyType); method.Invoke(null, new object[] { mapping, property, ownership }); }
private static void MapOneToMany <T>(ClasslikeMapBase <T> mapping, PropertyInfo property, Ownership ownership, Type childType) { MethodInfo method = _mapOneToManyMethod.MakeGenericMethod(typeof(T), childType); method.Invoke(null, new object[] { mapping, property, ownership }); }
public SubclassBuilder(ClasslikeMapBase <T> mapping, AutomaticMapper mapper) { _parentMapping = mapping; _mapper = mapper; }
public static ManyToOnePart <TOther> ReferenceIndex <T, TOther>(this ClasslikeMapBase <T> map, Expression <Func <T, TOther> > memberExpression) { return(map.References(memberExpression, $"{ExpressionHelper.GetPropertyName(memberExpression)}Id").Index($"Idx{ExpressionHelper.GetPropertyName(memberExpression)}")); }
public JoinBuilder(ClasslikeMapBase <T> mapping, AutomaticMapper mapper) { _mapper = mapper; _parentMapping = mapping; }