Skip to content

Add option to load internal IAutoMappingOverrides #486

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src" ],
"sdk": {
"version": "2.1.700"
"version": "2.1.812"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Automapping.TestFixtures;
using FluentNHibernate.Testing.Automapping;
using FluentNHibernate.Testing.Fixtures.AutoMappingAlterations.Model;
using NUnit.Framework;

namespace FluentNHibernate.Testing.AutoMapping.Overrides
{
[TestFixture]
public class InternalAutoMappingOverrideAlterationTests
{
private AutoMappingOverrideAlteration alteration;

[SetUp]
public void CreateOverride()
{
alteration = new AutoMappingOverrideAlteration(typeof(ExampleClass).Assembly, true);
}

[Test]
public void OverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace)
.Alterations(x => x.Add(alteration));

new AutoMappingTester<Baz>(model)
.Element("class").HasAttribute("batch-size", "10");
}

[Test]
public void InternalOverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace)
.Alterations(x => x.Add(alteration));

new AutoMappingTester<Baz>(model)
.Element("class").HasAttribute("table", "InternalBaz");
}

[Test]
public void RegularAutoMappingsStillWorkWhenOverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace);

alteration.Alter(model);

new AutoMappingTester<Baz>(model)
.Element("class/property[@name='Name']").Exists();
}

[Test]
public void OverridesCanBeAbstract()
{
var model = AutoMap.AssemblyOf<Qux>()
.Where(t => t.Namespace == typeof(Qux).Namespace);

alteration.Alter(model);

new AutoMappingTester<Qux>(model)
.Element("class").HasAttribute("batch-size", "10");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Automapping.TestFixtures;
using FluentNHibernate.Testing.Automapping;
using FluentNHibernate.Testing.Fixtures.AutoMappingAlterations.Model;
using NUnit.Framework;

namespace FluentNHibernate.Testing.AutoMapping.Overrides
{
[TestFixture]
public class AutoMappingOverrideAlterationTests
{
private AutoMappingOverrideAlteration alteration;

[SetUp]
public void CreateOverride()
{
alteration = new AutoMappingOverrideAlteration(typeof(ExampleClass).Assembly);
}

[Test]
public void OverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace)
.Alterations(x => x.Add(alteration));

new AutoMappingTester<Baz>(model)
.Element("class").HasAttribute("batch-size", "10");
}

[Test]
public void RegularAutoMappingsStillWorkWhenOverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace);

alteration.Alter(model);

new AutoMappingTester<Baz>(model)
.Element("class/property[@name='Name']").Exists();
}

[Test]
public void OverridesCanBeAbstract()
{
var model = AutoMap.AssemblyOf<Qux>()
.Where(t => t.Namespace == typeof(Qux).Namespace);

alteration.Alter(model);

new AutoMappingTester<Qux>(model)
.Element("class").HasAttribute("batch-size", "10");
}
}
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Automapping.TestFixtures;
using FluentNHibernate.Testing.Automapping;
using FluentNHibernate.Testing.Fixtures.AutoMappingAlterations.Model;
using NUnit.Framework;

namespace FluentNHibernate.Testing.AutoMapping.Overrides
{
[TestFixture]
public class PublicAutoMappingOverrideAlterationTests
{
private AutoMappingOverrideAlteration alteration;

[SetUp]
public void CreateOverride()
{
alteration = new AutoMappingOverrideAlteration(typeof(ExampleClass).Assembly, false);
}

[Test]
public void OverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace)
.Alterations(x => x.Add(alteration));

new AutoMappingTester<Baz>(model)
.Element("class").HasAttribute("batch-size", "10");
}

[Test]
public void InternalOverridesNotApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace)
.Alterations(x => x.Add(alteration));

new AutoMappingTester<Baz>(model)
.Element("class").HasAttribute("table", "`Baz`");
}

[Test]
public void RegularAutoMappingsStillWorkWhenOverridesApplied()
{
var model = AutoMap.AssemblyOf<Baz>()
.Where(t => t.Namespace == typeof(Baz).Namespace);

alteration.Alter(model);

new AutoMappingTester<Baz>(model)
.Element("class/property[@name='Name']").Exists();
}

[Test]
public void OverridesCanBeAbstract()
{
var model = AutoMap.AssemblyOf<Qux>()
.Where(t => t.Namespace == typeof(Qux).Namespace);

alteration.Alter(model);

new AutoMappingTester<Qux>(model)
.Element("class").HasAttribute("batch-size", "10");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Testing.Fixtures.AutoMappingAlterations.Model;

namespace FluentNHibernate.Testing.Fixtures.AutoMappingAlterations
{
internal class InternalOverride : IAutoMappingOverride<Baz>
{
public void Override(AutoMapping<Baz> mapping)
{
mapping.Table("InternalBaz");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Automapping.Alterations
{
@@ -12,14 +9,17 @@ namespace FluentNHibernate.Automapping.Alterations
public class AutoMappingOverrideAlteration : IAutoMappingAlteration
{
private readonly Assembly assembly;
private readonly bool includeInternal;

/// <summary>
/// Constructor for AutoMappingOverrideAlteration.
/// </summary>
/// <param name="overrideAssembly">Assembly to load overrides from.</param>
public AutoMappingOverrideAlteration(Assembly overrideAssembly)
/// <param name="includeInternal">Should internal IAutoMappingOverrides be included.</param>
public AutoMappingOverrideAlteration(Assembly overrideAssembly, bool includeInternal)
{
assembly = overrideAssembly;
this.includeInternal = includeInternal;
}

/// <summary>
@@ -32,8 +32,9 @@ public AutoMappingOverrideAlteration(Assembly overrideAssembly)
/// <param name="model">AutoPersistenceModel instance to alter</param>
public void Alter(AutoPersistenceModel model)
{
var assemblyTypes = includeInternal ? assembly.GetTypes() : assembly.GetExportedTypes();
// find all types deriving from IAutoMappingOverride<T>
var types = from type in assembly.GetExportedTypes()
var types = from type in assemblyTypes
where !type.IsAbstract
let entity = (from interfaceType in type.GetInterfaces()
where interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IAutoMappingOverride<>)
9 changes: 5 additions & 4 deletions src/FluentNHibernate/Automapping/AutoPersistenceModel.cs
Original file line number Diff line number Diff line change
@@ -78,19 +78,20 @@ public AutoPersistenceModel Alterations(Action<AutoMappingAlterationCollection>
/// </summary>
/// <typeparam name="T">Type to get assembly from</typeparam>
/// <returns>AutoPersistenceModel</returns>
public AutoPersistenceModel UseOverridesFromAssemblyOf<T>()
public AutoPersistenceModel UseOverridesFromAssemblyOf<T>(bool includeInternal = false)
{
return UseOverridesFromAssembly(typeof(T).Assembly);
return UseOverridesFromAssembly(typeof(T).Assembly, includeInternal);
}

/// <summary>
/// Use auto mapping overrides defined in the assembly of T.
/// </summary>
/// <param name="assembly">Assembly to scan</param>
/// <param name="includeInternal">Should internal IAutoMappingOverrides be included.</param>
/// <returns>AutoPersistenceModel</returns>
public AutoPersistenceModel UseOverridesFromAssembly(Assembly assembly)
public AutoPersistenceModel UseOverridesFromAssembly(Assembly assembly, bool includeInternal = false)
{
alterations.Add(new AutoMappingOverrideAlteration(assembly));
alterations.Add(new AutoMappingOverrideAlteration(assembly, includeInternal));
return this;
}

2 changes: 1 addition & 1 deletion src/FluentNHibernate/FluentNHibernate.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<NoWarn>1591</NoWarn>
<PlatformTarget>AnyCpu</PlatformTarget>
<OutputType>Library</OutputType>