diff --git a/src/FluentNHibernate/Automapping/Alterations/AutoMappingOverrideAlteration.cs b/src/FluentNHibernate/Automapping/Alterations/AutoMappingOverrideAlteration.cs index 58d6e093a..faf429a04 100644 --- a/src/FluentNHibernate/Automapping/Alterations/AutoMappingOverrideAlteration.cs +++ b/src/FluentNHibernate/Automapping/Alterations/AutoMappingOverrideAlteration.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System.Linq; using System.Reflection; diff --git a/src/FluentNHibernate/Automapping/Alterations/IAutoMappingAlteration.cs b/src/FluentNHibernate/Automapping/Alterations/IAutoMappingAlteration.cs index 69f7f7ab8..c35509988 100644 --- a/src/FluentNHibernate/Automapping/Alterations/IAutoMappingAlteration.cs +++ b/src/FluentNHibernate/Automapping/Alterations/IAutoMappingAlteration.cs @@ -1,4 +1,7 @@ -namespace FluentNHibernate.Automapping.Alterations; +#if USE_NULLABLE +#nullable enable +#endif +namespace FluentNHibernate.Automapping.Alterations; /// /// Provides a mechanism for altering an AutoPersistenceModel prior to diff --git a/src/FluentNHibernate/Automapping/Alterations/IAutoMappingOverride.cs b/src/FluentNHibernate/Automapping/Alterations/IAutoMappingOverride.cs index 693d4e47c..a70246a10 100644 --- a/src/FluentNHibernate/Automapping/Alterations/IAutoMappingOverride.cs +++ b/src/FluentNHibernate/Automapping/Alterations/IAutoMappingOverride.cs @@ -1,4 +1,7 @@ -namespace FluentNHibernate.Automapping.Alterations; +#if USE_NULLABLE +#nullable enable +#endif +namespace FluentNHibernate.Automapping.Alterations; /// /// A mapping override for an auto mapped entity. diff --git a/src/FluentNHibernate/Automapping/AutoJoinPart.cs b/src/FluentNHibernate/Automapping/AutoJoinPart.cs index 5d8a98dc0..589b34d06 100644 --- a/src/FluentNHibernate/Automapping/AutoJoinPart.cs +++ b/src/FluentNHibernate/Automapping/AutoJoinPart.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +#if USE_NULLABLE +#nullable enable +#endif +using System.Collections.Generic; using FluentNHibernate.Mapping; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/Automapping/AutoJoinedSubClassPart.cs b/src/FluentNHibernate/Automapping/AutoJoinedSubClassPart.cs index 10af890bc..52cc360ae 100644 --- a/src/FluentNHibernate/Automapping/AutoJoinedSubClassPart.cs +++ b/src/FluentNHibernate/Automapping/AutoJoinedSubClassPart.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using FluentNHibernate.Mapping; @@ -44,7 +47,7 @@ internal override void OnMemberMapped(Member member) public void JoinedSubClass(string keyColumn, Action> action) { var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn); + var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn)!; action(joinedclass); @@ -54,7 +57,7 @@ public void JoinedSubClass(string keyColumn, Action).MakeGenericType(type); - var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn); + var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn)!; providers.Subclasses[type] = joinedclass; @@ -64,7 +67,7 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn) public void SubClass(string discriminatorValue, Action> action) { var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, discriminatorValue); + var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, discriminatorValue)!; action(subclass); @@ -74,19 +77,19 @@ public void SubClass(string discriminatorValue, Action).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; } diff --git a/src/FluentNHibernate/Automapping/AutoMap.cs b/src/FluentNHibernate/Automapping/AutoMap.cs index 9a7ff0631..6f980a144 100644 --- a/src/FluentNHibernate/Automapping/AutoMap.cs +++ b/src/FluentNHibernate/Automapping/AutoMap.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using System.Linq; diff --git a/src/FluentNHibernate/Automapping/AutoMapType.cs b/src/FluentNHibernate/Automapping/AutoMapType.cs index fccc87135..715e1de57 100644 --- a/src/FluentNHibernate/Automapping/AutoMapType.cs +++ b/src/FluentNHibernate/Automapping/AutoMapType.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/Automapping/AutoMapper.cs b/src/FluentNHibernate/Automapping/AutoMapper.cs index 32dd161fe..fd3072193 100644 --- a/src/FluentNHibernate/Automapping/AutoMapper.cs +++ b/src/FluentNHibernate/Automapping/AutoMapper.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using System.Linq; @@ -14,12 +17,12 @@ public class AutoMapper( IConventionFinder conventionFinder, IEnumerable inlineOverrides) { - List mappingTypes; + List? mappingTypes; void ApplyOverrides(Type classType, IList 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)) @@ -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 mappedMembers) + void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList mappedMembers, List 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 @@ -72,7 +74,7 @@ void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList 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); @@ -102,7 +104,7 @@ void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList } } - static bool HasDiscriminator(ClassMappingBase mapping) + static bool HasDiscriminator(ClassMappingBase? mapping) { if (mapping is ClassMapping && ((ClassMapping) mapping).Discriminator is not null) return true; @@ -110,27 +112,25 @@ static bool HasDiscriminator(ClassMappingBase mapping) return false; } - Dictionary GetMappingTypesWithLogicalParents() + Dictionary GetMappingTypesWithLogicalParents(List 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(); + var mappingTypesWithLogicalParents = new Dictionary(); foreach (var type in availableTypes) mappingTypesWithLogicalParents.Add(type.Value, GetLogicalParent(type.Key, availableTypes)); return mappingTypesWithLogicalParents; } - static AutoMapType GetLogicalParent(Type type, IDictionary availableTypes) + static AutoMapType? GetLogicalParent(Type type, IDictionary 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); @@ -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); } } diff --git a/src/FluentNHibernate/Automapping/AutoMapping.cs b/src/FluentNHibernate/Automapping/AutoMapping.cs index 7afe006d1..021331a93 100644 --- a/src/FluentNHibernate/Automapping/AutoMapping.cs +++ b/src/FluentNHibernate/Automapping/AutoMapping.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using System.Linq; @@ -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()).Concat(new[] { first }).ToArray(); @@ -127,11 +130,11 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func 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 JoinedSubClass(string keyColumn, Action> action) + public AutoJoinedSubClassPart JoinedSubClass(string keyColumn, Action>? action) where TSubclass : T { var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn); + var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn)!; action?.Invoke(joinedclass); @@ -144,7 +147,7 @@ public AutoJoinedSubClassPart JoinedSubClass(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; @@ -160,11 +163,11 @@ public AutoJoinedSubClassPart JoinedSubClass(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 SubClass(object discriminatorValue, Action> action) + public AutoSubClassPart SubClass(object discriminatorValue, Action>? action) where TSubclass : T { var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, null, discriminatorValue); + var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, null, discriminatorValue)!; action?.Invoke(subclass); @@ -185,7 +188,7 @@ public AutoSubClassPart SubClass(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; @@ -194,7 +197,7 @@ public IAutoClasslike SubClass(Type type, string discriminatorValue) } // hide the base one D: - new void Join(string table, Action> action) + new void Join(string? table, Action>? action) {} public void Join(string table, Action> action) @@ -209,7 +212,7 @@ public void Join(string table, Action> 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() + public new ImportPart? ImportType() { return null; } diff --git a/src/FluentNHibernate/Automapping/AutoMappingAlterationCollection.cs b/src/FluentNHibernate/Automapping/AutoMappingAlterationCollection.cs index 77ddd13ff..790059b88 100644 --- a/src/FluentNHibernate/Automapping/AutoMappingAlterationCollection.cs +++ b/src/FluentNHibernate/Automapping/AutoMappingAlterationCollection.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections; using System.Collections.Generic; @@ -16,7 +19,7 @@ public class AutoMappingAlterationCollection : IEnumerableType of an IAutoMappingAlteration void Add(Type type) { - Add((IAutoMappingAlteration)Activator.CreateInstance(type)); + Add((IAutoMappingAlteration)Activator.CreateInstance(type)!); } /// diff --git a/src/FluentNHibernate/Automapping/AutoMappingException.cs b/src/FluentNHibernate/Automapping/AutoMappingException.cs index 0878aade1..ac4cbe85d 100644 --- a/src/FluentNHibernate/Automapping/AutoMappingException.cs +++ b/src/FluentNHibernate/Automapping/AutoMappingException.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Runtime.Serialization; diff --git a/src/FluentNHibernate/Automapping/AutoMappingExpressions.cs b/src/FluentNHibernate/Automapping/AutoMappingExpressions.cs index 12df6fa3a..6e0cf9dde 100644 --- a/src/FluentNHibernate/Automapping/AutoMappingExpressions.cs +++ b/src/FluentNHibernate/Automapping/AutoMappingExpressions.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; namespace FluentNHibernate.Automapping; @@ -8,51 +11,51 @@ public class AutoMappingExpressions /// Determines whether a member is to be automapped. /// [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override ShouldMap(Member)")] - public Func FindMembers; + public Func? FindMembers; /// /// Determines whether a member is the identity of an entity. /// [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsId")] - public Func FindIdentity; + public Func? FindIdentity; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetParentSideForManyToMany")] - public Func GetParentSideForManyToMany; + public Func? GetParentSideForManyToMany; [Obsolete("Use IgnoreBase or IgnoreBase(Type): AutoMap.AssemblyOf().IgnoreBase(typeof(Parent<>))", true)] - public Func IsBaseType; + public Func? IsBaseType; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsConcreteBaseType")] - public Func IsConcreteBaseType; + public Func? IsConcreteBaseType; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsComponent")] - public Func IsComponentType; + public Func? IsComponentType; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetComponentColumnPrefix")] - public Func GetComponentColumnPrefix; + public Func? GetComponentColumnPrefix; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsDiscriminated")] - public Func IsDiscriminated; + public Func? IsDiscriminated; [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override GetDiscriminatorColumn")] - public Func DiscriminatorColumn; + public Func? DiscriminatorColumn; #pragma warning disable 612,618 [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override IsDiscriminated", true)] - public Func SubclassStrategy; + public Func? SubclassStrategy; #pragma warning restore 612,618 /// /// Determines whether an abstract class is a layer supertype or part of a mapped inheritance hierarchy. /// [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override AbstractClassIsLayerSupertype")] - public Func AbstractClassIsLayerSupertype; + public Func? AbstractClassIsLayerSupertype; /// /// Specifies the value column used in a table of simple types. /// [Obsolete("Use an instance of IAutomappingConfiguration for configuration, and override SimpleTypeCollectionValueColumn")] - public Func SimpleTypeCollectionValueColumn; + public Func? SimpleTypeCollectionValueColumn; } #pragma warning disable 612,618 @@ -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); diff --git a/src/FluentNHibernate/Automapping/AutoPersistenceModel.cs b/src/FluentNHibernate/Automapping/AutoPersistenceModel.cs index c78f0a940..92acafa63 100644 --- a/src/FluentNHibernate/Automapping/AutoPersistenceModel.cs +++ b/src/FluentNHibernate/Automapping/AutoPersistenceModel.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using System.Linq; @@ -17,10 +20,10 @@ namespace FluentNHibernate.Automapping; public class AutoPersistenceModel : PersistenceModel { readonly IAutomappingConfiguration cfg; - readonly AutoMappingExpressions expressions; + readonly AutoMappingExpressions expressions = new(); readonly AutoMapper autoMapper; readonly List sources = new List(); - Func whereClause; + Func? whereClause; readonly List mappingTypes = new List(); bool autoMappingsCreated; readonly AutoMappingAlterationCollection alterations = new AutoMappingAlterationCollection(); @@ -30,7 +33,6 @@ public class AutoPersistenceModel : PersistenceModel public AutoPersistenceModel() { - expressions = new AutoMappingExpressions(); cfg = new ExpressionBasedAutomappingConfiguration(expressions); autoMapper = new AutoMapper(cfg, Conventions, inlineOverrides); @@ -220,17 +222,27 @@ void AddMapping(Type type) Type GetTypeToMap(Type type) { - while (ShouldMapParent(type)) + while (true) { - type = type.BaseType; - } + var parent = GetParentIfMappable(type); + if (parent is null) + { + return type; + } - return type; + type = parent; + } } - bool ShouldMapParent(Type type) + Type? GetParentIfMappable(Type type) { - return ShouldMap(type.BaseType) && !cfg.IsConcreteBaseType(type.BaseType); + var parent = type.BaseType; + if (parent != null && ShouldMap(parent) && !cfg.IsConcreteBaseType(parent)) + { + return parent; + } + + return null; } bool ShouldMap(Type type) @@ -263,14 +275,14 @@ bool ShouldMap(Type type) return true; } - public IMappingProvider FindMapping() + public IMappingProvider? FindMapping() { return FindMapping(typeof(T)); } - public IMappingProvider FindMapping(Type type) + public IMappingProvider? FindMapping(Type? type) { - Func finder = (provider, expectedType) => + Func finder = (provider, expectedType) => { var mappingType = provider.GetType(); if (mappingType.IsGenericType) @@ -278,14 +290,14 @@ public IMappingProvider FindMapping(Type type) // instance of a generic type (probably AutoMapping) return mappingType.GetGenericArguments()[0] == expectedType; } - if (mappingType.BaseType.IsGenericType && + if (mappingType.BaseType?.IsGenericType == true && mappingType.BaseType.GetGenericTypeDefinition() == typeof(ClassMap<>)) { // base type is a generic type of ClassMap, so we've got a XXXMap instance return mappingType.BaseType.GetGenericArguments()[0] == expectedType; } - if (provider is PassThroughMappingProvider) - return provider.GetClassMapping().Type == expectedType; + if (provider is PassThroughMappingProvider passThroughProvider) + return passThroughProvider.GetClassMapping().Type == expectedType; return false; }; @@ -297,9 +309,9 @@ public IMappingProvider FindMapping(Type type) // if we haven't found a map yet then try to find a map of the // base type to merge if not a concrete base type - if (type.BaseType != typeof(object) && !cfg.IsConcreteBaseType(type.BaseType)) + if (type?.BaseType != typeof(object) && !cfg.IsConcreteBaseType(type?.BaseType)) { - return FindMapping(type.BaseType); + return FindMapping(type?.BaseType); } return null; @@ -377,7 +389,7 @@ public void Override(Type overrideType) var overrideInstance = Activator.CreateInstance(overrideType); - MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel) + var overrideHelperMethod = typeof(AutoPersistenceModel) .GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance); if (overrideHelperMethod is null) return; diff --git a/src/FluentNHibernate/Automapping/AutoSubClassPart.cs b/src/FluentNHibernate/Automapping/AutoSubClassPart.cs index c42d76105..820e63459 100644 --- a/src/FluentNHibernate/Automapping/AutoSubClassPart.cs +++ b/src/FluentNHibernate/Automapping/AutoSubClassPart.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using System.Collections.Generic; using FluentNHibernate.Mapping; @@ -48,7 +51,7 @@ internal override void OnMemberMapped(Member member) public void JoinedSubClass(string keyColumn, Action> action) { var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn); + var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn)!; action(joinedclass); @@ -58,7 +61,7 @@ public void JoinedSubClass(string keyColumn, Action).MakeGenericType(type); - var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn); + var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn)!; providers.Subclasses[type] = joinedclass; @@ -68,7 +71,7 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn) public void SubClass(string discriminatorValue, Action> action) { var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass)); - var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, discriminatorValue); + var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, discriminatorValue)!; action?.Invoke(subclass); @@ -78,19 +81,19 @@ public void SubClass(string discriminatorValue, Action).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; } diff --git a/src/FluentNHibernate/Automapping/DefaultAutomappingConfiguration.cs b/src/FluentNHibernate/Automapping/DefaultAutomappingConfiguration.cs index 607ee8246..1ff37e60d 100644 --- a/src/FluentNHibernate/Automapping/DefaultAutomappingConfiguration.cs +++ b/src/FluentNHibernate/Automapping/DefaultAutomappingConfiguration.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using FluentNHibernate.Automapping.Alterations; @@ -37,10 +40,10 @@ public virtual Access GetAccessStrategyForReadOnlyProperty(Member member) public virtual Type GetParentSideForManyToMany(Type left, Type right) { - return left.FullName.CompareTo(right.FullName) < 0 ? left : right; + return string.Compare(left.FullName, right.FullName) < 0 ? left : right; } - public virtual bool IsConcreteBaseType(Type type) + public virtual bool IsConcreteBaseType(Type? type) { return false; } diff --git a/src/FluentNHibernate/Automapping/IAutoClasslike.cs b/src/FluentNHibernate/Automapping/IAutoClasslike.cs index fdb7d3d3c..e630bc3cc 100644 --- a/src/FluentNHibernate/Automapping/IAutoClasslike.cs +++ b/src/FluentNHibernate/Automapping/IAutoClasslike.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; using FluentNHibernate.MappingModel.ClassBased; diff --git a/src/FluentNHibernate/Automapping/IAutomappingConfiguration.cs b/src/FluentNHibernate/Automapping/IAutomappingConfiguration.cs index f027322e5..692bce05a 100644 --- a/src/FluentNHibernate/Automapping/IAutomappingConfiguration.cs +++ b/src/FluentNHibernate/Automapping/IAutomappingConfiguration.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Collections.Generic; using FluentNHibernate.Automapping.Steps; using FluentNHibernate.Conventions; @@ -80,7 +83,7 @@ public interface IAutomappingConfiguration /// /// Type /// Base type is concrete? - bool IsConcreteBaseType(Type type); + bool IsConcreteBaseType(Type? type); /// /// Specifies that a particular type should be mapped as a component rather than diff --git a/src/FluentNHibernate/Automapping/IPropertyIgnorer.cs b/src/FluentNHibernate/Automapping/IPropertyIgnorer.cs index 7179dbeee..a66710654 100644 --- a/src/FluentNHibernate/Automapping/IPropertyIgnorer.cs +++ b/src/FluentNHibernate/Automapping/IPropertyIgnorer.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/Automapping/InlineOverride.cs b/src/FluentNHibernate/Automapping/InlineOverride.cs index d62fe62e0..2429f3d2b 100644 --- a/src/FluentNHibernate/Automapping/InlineOverride.cs +++ b/src/FluentNHibernate/Automapping/InlineOverride.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/Automapping/PrivateAutoPersistenceModel.cs b/src/FluentNHibernate/Automapping/PrivateAutoPersistenceModel.cs index b6583c71d..0771c185a 100644 --- a/src/FluentNHibernate/Automapping/PrivateAutoPersistenceModel.cs +++ b/src/FluentNHibernate/Automapping/PrivateAutoPersistenceModel.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/Automapping/Steps/AutoKeyMapper.cs b/src/FluentNHibernate/Automapping/Steps/AutoKeyMapper.cs index 70061c1fd..35c48add2 100644 --- a/src/FluentNHibernate/Automapping/Steps/AutoKeyMapper.cs +++ b/src/FluentNHibernate/Automapping/Steps/AutoKeyMapper.cs @@ -1,4 +1,7 @@ -using FluentNHibernate.MappingModel; +#if USE_NULLABLE +#nullable enable +#endif +using FluentNHibernate.MappingModel; using FluentNHibernate.MappingModel.ClassBased; using FluentNHibernate.MappingModel.Collections; diff --git a/src/FluentNHibernate/Automapping/Steps/CollectionStep.cs b/src/FluentNHibernate/Automapping/Steps/CollectionStep.cs index 1d32a9c36..759ef89ab 100644 --- a/src/FluentNHibernate/Automapping/Steps/CollectionStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/CollectionStep.cs @@ -1,4 +1,7 @@ -using System.Collections; +#if USE_NULLABLE +#nullable enable +#endif +using System.Collections; using System.Collections.Generic; using FluentNHibernate.Mapping; using FluentNHibernate.MappingModel; diff --git a/src/FluentNHibernate/Automapping/Steps/ComponentStep.cs b/src/FluentNHibernate/Automapping/Steps/ComponentStep.cs index f32622bd4..0314706f1 100644 --- a/src/FluentNHibernate/Automapping/Steps/ComponentStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/ComponentStep.cs @@ -1,4 +1,7 @@ -using FluentNHibernate.MappingModel.ClassBased; +#if USE_NULLABLE +#nullable enable +#endif +using FluentNHibernate.MappingModel.ClassBased; namespace FluentNHibernate.Automapping.Steps; diff --git a/src/FluentNHibernate/Automapping/Steps/HasManyStep.cs b/src/FluentNHibernate/Automapping/Steps/HasManyStep.cs index 0f95bea4f..530c8e0aa 100644 --- a/src/FluentNHibernate/Automapping/Steps/HasManyStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/HasManyStep.cs @@ -1,4 +1,7 @@ -using FluentNHibernate.MappingModel.ClassBased; +#if USE_NULLABLE +#nullable enable +#endif +using FluentNHibernate.MappingModel.ClassBased; namespace FluentNHibernate.Automapping.Steps; diff --git a/src/FluentNHibernate/Automapping/Steps/HasManyToManyStep.cs b/src/FluentNHibernate/Automapping/Steps/HasManyToManyStep.cs index 480905e63..7eb1d530f 100644 --- a/src/FluentNHibernate/Automapping/Steps/HasManyToManyStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/HasManyToManyStep.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -25,7 +28,7 @@ public bool ShouldMap(Member member) return hasInverse; } - static Member GetInverseProperty(Member member) + static Member? GetInverseProperty(Member member) { var type = member.PropertyType; var expectedInversePropertyType = type.GetGenericTypeDefinition() @@ -108,7 +111,7 @@ static void SetKey(Member property, ClassMappingBase classMap, CollectionMapping public void Map(ClassMappingBase classMap, Member member) { - var inverseProperty = GetInverseProperty(member); + var inverseProperty = GetInverseProperty(member) ?? throw new InvalidOperationException($"Could not determine inverse property of {member}"); var parentSide = cfg.GetParentSideForManyToMany(member.DeclaringType, inverseProperty.DeclaringType); var mapping = GetCollection(member); diff --git a/src/FluentNHibernate/Automapping/Steps/IAutomappingStep.cs b/src/FluentNHibernate/Automapping/Steps/IAutomappingStep.cs index b7832d78d..6dc582324 100644 --- a/src/FluentNHibernate/Automapping/Steps/IAutomappingStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/IAutomappingStep.cs @@ -1,4 +1,7 @@ -using FluentNHibernate.MappingModel.ClassBased; +#if USE_NULLABLE +#nullable enable +#endif +using FluentNHibernate.MappingModel.ClassBased; namespace FluentNHibernate.Automapping.Steps; diff --git a/src/FluentNHibernate/Automapping/Steps/IdentityStep.cs b/src/FluentNHibernate/Automapping/Steps/IdentityStep.cs index e91b16d34..a84de8b3f 100644 --- a/src/FluentNHibernate/Automapping/Steps/IdentityStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/IdentityStep.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Collections.Generic; using FluentNHibernate.Mapping; using FluentNHibernate.MappingModel; diff --git a/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs b/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs index 621487447..27c28c67f 100644 --- a/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Linq; using FluentNHibernate.Conventions; using FluentNHibernate.Conventions.AcceptanceCriteria; diff --git a/src/FluentNHibernate/Automapping/Steps/ReferenceStep.cs b/src/FluentNHibernate/Automapping/Steps/ReferenceStep.cs index 64745cbb2..3312899ec 100644 --- a/src/FluentNHibernate/Automapping/Steps/ReferenceStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/ReferenceStep.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using FluentNHibernate.Mapping; using FluentNHibernate.MappingModel; using FluentNHibernate.MappingModel.ClassBased; diff --git a/src/FluentNHibernate/Automapping/Steps/SimpleTypeCollectionStep.cs b/src/FluentNHibernate/Automapping/Steps/SimpleTypeCollectionStep.cs index 7f5ff22d0..4942e688f 100644 --- a/src/FluentNHibernate/Automapping/Steps/SimpleTypeCollectionStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/SimpleTypeCollectionStep.cs @@ -1,4 +1,7 @@ -using System; +#if USE_NULLABLE +#nullable enable +#endif +using System; using System.Collections.Generic; using FluentNHibernate.Mapping; using FluentNHibernate.MappingModel; diff --git a/src/FluentNHibernate/Automapping/Steps/VersionStep.cs b/src/FluentNHibernate/Automapping/Steps/VersionStep.cs index a88268694..c6f83dd77 100644 --- a/src/FluentNHibernate/Automapping/Steps/VersionStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/VersionStep.cs @@ -1,4 +1,7 @@ -using FluentNHibernate.Mapping; +#if USE_NULLABLE +#nullable enable +#endif +using FluentNHibernate.Mapping; using FluentNHibernate.MappingModel; using FluentNHibernate.MappingModel.ClassBased; using FluentNHibernate.Utils; diff --git a/src/FluentNHibernate/Automapping/SubclassStrategy.cs b/src/FluentNHibernate/Automapping/SubclassStrategy.cs index dccbd6dff..a46ddf581 100644 --- a/src/FluentNHibernate/Automapping/SubclassStrategy.cs +++ b/src/FluentNHibernate/Automapping/SubclassStrategy.cs @@ -1,3 +1,6 @@ +#if USE_NULLABLE +#nullable enable +#endif using System; namespace FluentNHibernate.Automapping; diff --git a/src/FluentNHibernate/FluentNHibernate.csproj b/src/FluentNHibernate/FluentNHibernate.csproj index 49cc6df88..dde6a7b09 100644 --- a/src/FluentNHibernate/FluentNHibernate.csproj +++ b/src/FluentNHibernate/FluentNHibernate.csproj @@ -9,6 +9,17 @@ true ../FluentKey.snk + + + + USE_NULLABLE + + + + + $(NoWarn);CS8632 + + 2.0.9