Skip to content

Add Nullable to Automapping #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System.Linq;
using System.Reflection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace FluentNHibernate.Automapping.Alterations;
#if USE_NULLABLE
#nullable enable
#endif
namespace FluentNHibernate.Automapping.Alterations;

/// <summary>
/// Provides a mechanism for altering an AutoPersistenceModel prior to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace FluentNHibernate.Automapping.Alterations;
#if USE_NULLABLE
#nullable enable
#endif
namespace FluentNHibernate.Automapping.Alterations;

/// <summary>
/// A mapping override for an auto mapped entity.
Expand Down
5 changes: 4 additions & 1 deletion src/FluentNHibernate/Automapping/AutoJoinPart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
#if USE_NULLABLE
#nullable enable
#endif
using System.Collections.Generic;
using FluentNHibernate.Mapping;

namespace FluentNHibernate.Automapping;
Expand Down
15 changes: 9 additions & 6 deletions src/FluentNHibernate/Automapping/AutoJoinedSubClassPart.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections.Generic;
using FluentNHibernate.Mapping;
Expand Down Expand Up @@ -44,7 +47,7 @@ internal override void OnMemberMapped(Member member)
public void JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
{
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass));
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn);
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn)!;

action(joinedclass);

Expand All @@ -54,7 +57,7 @@ public void JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClas
public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
{
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(type);
var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn);
var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn)!;

providers.Subclasses[type] = joinedclass;

Expand All @@ -64,7 +67,7 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
public void SubClass<TSubclass>(string discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
{
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, discriminatorValue);
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, discriminatorValue)!;

action(subclass);

Expand All @@ -74,19 +77,19 @@ public void SubClass<TSubclass>(string discriminatorValue, Action<AutoSubClassPa
public IAutoClasslike SubClass(Type type, string discriminatorValue)
{
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(type);
var subclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, discriminatorValue);
var subclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, discriminatorValue)!;

providers.Subclasses[type] = subclass;

return (IAutoClasslike)subclass;
}

public ClassMapping GetClassMapping()
public ClassMapping? GetClassMapping()
{
return null;
}

public HibernateMapping GetHibernateMapping()
public HibernateMapping? GetHibernateMapping()
{
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMap.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
3 changes: 3 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMapType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;

namespace FluentNHibernate.Automapping;
Expand Down
32 changes: 16 additions & 16 deletions src/FluentNHibernate/Automapping/AutoMapper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,12 +17,12 @@ public class AutoMapper(
IConventionFinder conventionFinder,
IEnumerable<InlineOverride> inlineOverrides)
{
List<AutoMapType> mappingTypes;
List<AutoMapType>? mappingTypes;

void ApplyOverrides(Type classType, IList<Member> mappedMembers, ClassMappingBase mapping)
{
var autoMapType = ReflectionHelper.AutomappingTypeForEntityType(classType);
var autoMap = Activator.CreateInstance(autoMapType, mappedMembers);
var autoMap = Activator.CreateInstance(autoMapType, mappedMembers)!;

inlineOverrides
.Where(x => x.CanOverride(classType))
Expand All @@ -37,24 +40,23 @@ public ClassMappingBase MergeMap(Type classType, ClassMappingBase mapping, IList
ProcessClass(mapping, classType, mappedMembers);

if (mappingTypes is not null)
MapInheritanceTree(classType, mapping, mappedMembers);
MapInheritanceTree(classType, mapping, mappedMembers, mappingTypes);

return mapping;
}

void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<Member> mappedMembers)
void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<Member> mappedMembers, List<AutoMapType> mappingTypes)
{
var discriminatorSet = HasDiscriminator(mapping);
var isDiscriminated = cfg.IsDiscriminated(classType) || discriminatorSet;
var mappingTypesWithLogicalParents = GetMappingTypesWithLogicalParents();
var mappingTypesWithLogicalParents = GetMappingTypesWithLogicalParents(mappingTypes);

foreach (var inheritedClass in mappingTypesWithLogicalParents
.Where(x => x.Value is not null && x.Value.Type == classType)
.Select(x => x.Key))
{
var tempMapping = mapping as ClassMapping;
var tempIsNull = tempMapping is null;
if (isDiscriminated && !discriminatorSet && !tempIsNull)
if (isDiscriminated && !discriminatorSet && tempMapping is not null)
{
var discriminatorColumn = cfg.GetDiscriminatorColumn(classType);
var discriminator = new DiscriminatorMapping
Expand All @@ -72,7 +74,7 @@ void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<Member>

SubclassMapping subclassMapping;
var tempSubClassMap = mapping as SubclassMapping;
if(!tempIsNull && tempMapping.IsUnionSubclass || tempSubClassMap is not null && tempSubClassMap.SubclassType == SubclassType.UnionSubclass)
if(tempMapping is not null && tempMapping.IsUnionSubclass || tempSubClassMap is not null && tempSubClassMap.SubclassType == SubclassType.UnionSubclass)
{
subclassMapping = new SubclassMapping(SubclassType.UnionSubclass);
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
Expand Down Expand Up @@ -102,35 +104,33 @@ void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<Member>
}
}

static bool HasDiscriminator(ClassMappingBase mapping)
static bool HasDiscriminator(ClassMappingBase? mapping)
{
if (mapping is ClassMapping && ((ClassMapping) mapping).Discriminator is not null)
return true;

return false;
}

Dictionary<AutoMapType, AutoMapType> GetMappingTypesWithLogicalParents()
Dictionary<AutoMapType, AutoMapType?> GetMappingTypesWithLogicalParents(List<AutoMapType> mappingTypes)
{
var excludedTypes = mappingTypes
.Where(x => cfg.IsConcreteBaseType(x.Type.BaseType))
.ToArray();
var availableTypes = mappingTypes.Except(excludedTypes).ToDictionary(x => x.Type);
var mappingTypesWithLogicalParents = new Dictionary<AutoMapType, AutoMapType>();
var mappingTypesWithLogicalParents = new Dictionary<AutoMapType, AutoMapType?>();

foreach (var type in availableTypes)
mappingTypesWithLogicalParents.Add(type.Value, GetLogicalParent(type.Key, availableTypes));
return mappingTypesWithLogicalParents;
}

static AutoMapType GetLogicalParent(Type type, IDictionary<Type, AutoMapType> availableTypes)
static AutoMapType? GetLogicalParent(Type type, IDictionary<Type, AutoMapType> availableTypes)
{
if (type.BaseType == typeof(object) || type.BaseType is null)
return null;

AutoMapType baseType;

if (availableTypes.TryGetValue(type.BaseType, out baseType))
if (availableTypes.TryGetValue(type.BaseType, out var baseType))
return baseType;

return GetLogicalParent(type.BaseType, availableTypes);
Expand Down Expand Up @@ -205,7 +205,7 @@ static string GetDefaultTableName(Type type)
public void FlagAsMapped(Type type)
{
mappingTypes
.Where(x => x.Type == type)
?.Where(x => x.Type == type)
.Each(x => x.IsMapped = true);
}
}
21 changes: 12 additions & 9 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -107,7 +110,7 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperty(string name)
return this;
}

IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(string first, params string[] others)
IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(string first, params string[]? others)
{
var options = (others ?? Array.Empty<string>()).Concat(new[] { first }).ToArray();

Expand All @@ -127,11 +130,11 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func<Member, bool> predicate)
}

[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>>? action)
where TSubclass : T
{
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass));
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn);
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn)!;

action?.Invoke(joinedclass);

Expand All @@ -144,7 +147,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
{
var genericType = typeof (AutoJoinedSubClassPart<>).MakeGenericType(type);
var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn);
var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn)!;

// remove any mappings for the same type, then re-add
providers.Subclasses[type] = joinedclass;
Expand All @@ -160,11 +163,11 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
}

[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>>? action)
where TSubclass : T
{
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, null, discriminatorValue);
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, null, discriminatorValue)!;

action?.Invoke(subclass);

Expand All @@ -185,7 +188,7 @@ public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue
public IAutoClasslike SubClass(Type type, string discriminatorValue)
{
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(type);
var subclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, null, discriminatorValue);
var subclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, null, discriminatorValue)!;

// remove any mappings for the same type, then re-add
providers.Subclasses[type] = subclass;
Expand All @@ -194,7 +197,7 @@ public IAutoClasslike SubClass(Type type, string discriminatorValue)
}

// hide the base one D:
new void Join(string table, Action<JoinPart<T>> action)
new void Join(string? table, Action<JoinPart<T>>? action)
{}

public void Join(string table, Action<AutoJoinPart<T>> action)
Expand All @@ -209,7 +212,7 @@ public void Join(string table, Action<AutoJoinPart<T>> action)
#pragma warning disable 809
// hide this - imports aren't supported in overrides
[Obsolete("Imports aren't supported in overrides.", true)]
public new ImportPart ImportType<TImport>()
public new ImportPart? ImportType<TImport>()
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -16,7 +19,7 @@ public class AutoMappingAlterationCollection : IEnumerable<IAutoMappingAlteratio
/// <param name="type">Type of an IAutoMappingAlteration</param>
void Add(Type type)
{
Add((IAutoMappingAlteration)Activator.CreateInstance(type));
Add((IAutoMappingAlteration)Activator.CreateInstance(type)!);
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMappingException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Runtime.Serialization;

Expand Down
31 changes: 17 additions & 14 deletions src/FluentNHibernate/Automapping/AutoMappingExpressions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if USE_NULLABLE
#nullable enable
#endif
using System;

namespace FluentNHibernate.Automapping;

Expand All @@ -8,51 +11,51 @@ public class AutoMappingExpressions
/// Determines whether a member is to be automapped.
/// </summary>
[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override ShouldMap(Member)")]
public Func<Member, bool> FindMembers;
public Func<Member, bool>? FindMembers;

/// <summary>
/// Determines whether a member is the identity of an entity.
/// </summary>
[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsId")]
public Func<Member, bool> FindIdentity;
public Func<Member, bool>? FindIdentity;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetParentSideForManyToMany")]
public Func<Type, Type, Type> GetParentSideForManyToMany;
public Func<Type, Type, Type>? GetParentSideForManyToMany;

[Obsolete("Use IgnoreBase<T> or IgnoreBase(Type): AutoMap.AssemblyOf<Entity>().IgnoreBase(typeof(Parent<>))", true)]
public Func<Type, bool> IsBaseType;
public Func<Type, bool>? IsBaseType;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsConcreteBaseType")]
public Func<Type, bool> IsConcreteBaseType;
public Func<Type?, bool>? IsConcreteBaseType;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsComponent")]
public Func<Type, bool> IsComponentType;
public Func<Type, bool>? IsComponentType;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetComponentColumnPrefix")]
public Func<Member, string> GetComponentColumnPrefix;
public Func<Member, string>? GetComponentColumnPrefix;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsDiscriminated")]
public Func<Type, bool> IsDiscriminated;
public Func<Type, bool>? IsDiscriminated;

[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetDiscriminatorColumn")]
public Func<Type, string> DiscriminatorColumn;
public Func<Type, string>? DiscriminatorColumn;

#pragma warning disable 612,618
[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsDiscriminated", true)]
public Func<Type, SubclassStrategy> SubclassStrategy;
public Func<Type, SubclassStrategy>? SubclassStrategy;
#pragma warning restore 612,618

/// <summary>
/// Determines whether an abstract class is a layer supertype or part of a mapped inheritance hierarchy.
/// </summary>
[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override AbstractClassIsLayerSupertype")]
public Func<Type, bool> AbstractClassIsLayerSupertype;
public Func<Type, bool>? AbstractClassIsLayerSupertype;

/// <summary>
/// Specifies the value column used in a table of simple types.
/// </summary>
[Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override SimpleTypeCollectionValueColumn")]
public Func<Member, string> SimpleTypeCollectionValueColumn;
public Func<Member, string>? SimpleTypeCollectionValueColumn;
}

#pragma warning disable 612,618
Expand Down Expand Up @@ -85,7 +88,7 @@ public override Type GetParentSideForManyToMany(Type left, Type right)
return base.GetParentSideForManyToMany(left, right);
}

public override bool IsConcreteBaseType(Type type)
public override bool IsConcreteBaseType(Type? type)
{
if (expressions.IsConcreteBaseType is not null)
return expressions.IsConcreteBaseType(type);
Expand Down
Loading