Skip to content

Commit

Permalink
Use C#12 primary constructors (#659)
Browse files Browse the repository at this point in the history
+semver:patch
  • Loading branch information
hazzik authored Mar 12, 2024
1 parent 550b06c commit 366ab36
Show file tree
Hide file tree
Showing 246 changed files with 520 additions and 1,977 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ indent_size = 2
indent_style = space
indent_size = 4
insert_final_newline = true
max_line_length = off

csharp_style_namespace_declarations = file_scoped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,31 @@ public static XmlElementTester Element(this XmlDocument doc, string path)
return new XmlElementTester(doc, path);
}

public class XmlElementTester
public class XmlElementTester(XmlDocument doc, string path)
{
readonly XmlDocument doc;
string currentPath;
XmlElement currentElement;

public XmlElementTester(XmlDocument doc, string path)
{
currentElement = (XmlElement)doc.DocumentElement.SelectSingleNode(path);
this.doc = doc;
currentPath = path;
}
readonly XmlDocument doc = doc;
XmlElement currentElement = (XmlElement)doc.DocumentElement.SelectSingleNode(path);

public XmlElementTester ShouldExist()
{
if (currentElement is null)
throw new SpecificationException(string.Format("Should exist at {0} but does not.", currentPath));
throw new SpecificationException(string.Format("Should exist at {0} but does not.", path));

return this;
}

public XmlElementTester ShouldNotExist()
{
if (currentElement is not null)
throw new SpecificationException(string.Format("Should not exist at {0} but does.", currentPath));
throw new SpecificationException(string.Format("Should not exist at {0} but does.", path));

return this;
}

public XmlElementTester HasAttribute(string name)
{
if (!currentElement.HasAttribute(name))
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} but does not.", name, currentPath));
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} but does not.", name, path));

return this;
}
Expand All @@ -81,7 +73,7 @@ public XmlElementTester HasAttribute(string name, string value)
var actual = currentElement.GetAttribute(name);

if (!actual.Equals(value))
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value of {2} but was {3}", name, currentPath, value, actual));
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value of {2} but was {3}", name, path, value, actual));

return this;
}
Expand All @@ -94,7 +86,7 @@ public XmlElementTester HasAttribute(string name, Func<string, bool> predicate)
var actual = currentElement.GetAttribute(name);

if (!predicate(actual))
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value matching predicate but does not.", name, currentPath));
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value matching predicate but does not.", name, path));

return this;
}
Expand All @@ -104,7 +96,7 @@ public XmlElementTester DoesntHaveAttribute(string name)
ShouldExist();

if (currentElement.HasAttribute(name))
throw new SpecificationException(string.Format("Should not have attribute named {0} at {1} but does.", name, currentPath));
throw new SpecificationException(string.Format("Should not have attribute named {0} at {1} but does.", name, path));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@

namespace FluentNHibernate.Specs.Automapping.Fixtures;

internal class StubTypeSource : ITypeSource
internal class StubTypeSource(params Type[] types) : ITypeSource
{
private readonly IEnumerable<Type> types;

public StubTypeSource(params Type[] types)
{
this.types = types;
}

public IEnumerable<Type> GetTypes()
{
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,8 @@ class ConventionA : IConvention {}
class ConventionB : IConvention {}
class NotAConvention {}

class StubListener : IDiagnosticListener
class StubListener(Action<DiagnosticResults> receiver) : IDiagnosticListener
{
readonly Action<DiagnosticResults> receiver;

public StubListener(Action<DiagnosticResults> receiver)
{
this.receiver = receiver;
}

public void Receive(DiagnosticResults results)
{
receiver(results);
Expand Down
10 changes: 2 additions & 8 deletions src/FluentNHibernate.Testing/DomainModel/Music.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ public class Album
public int ID { get; set; }
public string Title { get; set;}
public Artist Artist { get; set; }
public ISet<Track> Tracks { get; set; }
public ISet<Tag> Tags { get; set; }

public Album()
{
Tracks = new HashSet<Track>();
Tags = new HashSet<Tag>();
}
public ISet<Track> Tracks { get; set; } = new HashSet<Track>();
public ISet<Tag> Tags { get; set; } = new HashSet<Tag>();
}

public class Track
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

namespace FluentNHibernate.Testing.FluentInterfaceTests;

public class ModelTester<TFluentClass, TModel>
public class ModelTester<TFluentClass, TModel>(Func<TFluentClass> instantiatePart, Func<TFluentClass, TModel> getModel)
{
private readonly Func<TFluentClass> instantiatePart;
private readonly Func<TFluentClass, TModel> getModel;
private TFluentClass fluentClass;

public ModelTester(Func<TFluentClass> instantiatePart, Func<TFluentClass, TModel> getModel)
{
this.instantiatePart = instantiatePart;
this.getModel = getModel;
}

public ModelTester<TFluentClass, TModel> Mapping(Action<TFluentClass> action)
{
fluentClass = instantiatePart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ public class Cat
public long Id { get; set; }
public string Name { get; set; }
public Kitten FirstKitten { get; set; }
public IList<Kitten> AllKittens { get; set; }
public IList<Kitten> AllKittens { get; set; } = new List<Kitten>();
public Bitmap Picture { get; set; }

public Cat()
{
AllKittens = new List<Kitten>();
}

public IEnumerable<Kitten> EnumerableOfKittens => AllKittens;

public void AddKitten(Kitten kitten)
Expand Down
32 changes: 9 additions & 23 deletions src/FluentNHibernate.Testing/Testing/Values/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@ namespace FluentNHibernate.Testing.Testing.Values;

public class ListEntity
{
private readonly IList<string> backingField;
private readonly IList<string> backingField = new List<string>();

public ListEntity()
{
backingField = new List<string>();
GetterAndSetter = new List<string>();
GetterAndPrivateSetter = new List<string>();

TypedSet = new HashSet<string>();
//Set = new HashedSet();
Collection = new StringCollection();
List = new List<string>();
}
//Set = new HashedSet();

public IEnumerable<string> GetterAndSetter { get; set; }
public IEnumerable<string> GetterAndPrivateSetter { get; private set; }
public IEnumerable<string> GetterAndSetter { get; set; } = new List<string>();
public IEnumerable<string> GetterAndPrivateSetter { get; private set; } = new List<string>();
public IEnumerable<string> BackingField => backingField;

public ISet<string> TypedSet { get; set; }
public ISet<string> TypedSet { get; set; } = new HashSet<string>();

//public ISet Set { get; set; }
public ICollection Collection { get; set; }
public ICollection Collection { get; set; } = new StringCollection();
public string[] Array { get; set; }
public IList<string> List { get; set; }
public IList<string> List { get; set; } = new List<string>();

public void AddListItem(string value)
{
Expand All @@ -38,13 +29,8 @@ public void AddListItem(string value)

public class ReferenceEntity
{
public ReferenceEntity()
{
ReferenceList = new List<OtherEntity>();
}

public OtherEntity Reference { get; set; }
public IEnumerable<OtherEntity> ReferenceList { get; set; }
public IEnumerable<OtherEntity> ReferenceList { get; set; } = new List<OtherEntity>();

public void SetReference(OtherEntity value)
{
Expand Down
30 changes: 8 additions & 22 deletions src/FluentNHibernate.Testing/Testing/XmlWriterTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ namespace FluentNHibernate.Testing.Testing;
public class XmlWriterTestHelper<TMappingType>
where TMappingType : IMapping
{
readonly IList<XmlTest> tests;
readonly IList<XmlTest> tests = new List<XmlTest>();
Func<TMappingType> constructor;

public XmlWriterTestHelper()
{
tests = new List<XmlTest>();
}

public XmlTest Check(Expression<Func<TMappingType, object>> sourceProperty, object value)
{
var test = new XmlTest(sourceProperty, value);
Expand Down Expand Up @@ -54,20 +49,11 @@ public void VerifyAll(IXmlWriter<TMappingType> writer)
}
}

public class XmlTest
public class XmlTest(Expression<Func<TMappingType, object>> sourceProperty, object value)
{
readonly IDictionary<string, object> checks;
readonly Accessor sourceProperty;
readonly object sourceValue;
readonly Member member;

public XmlTest(Expression<Func<TMappingType, object>> sourceProperty, object value)
{
checks = new Dictionary<string, object>();
member = sourceProperty.ToMember();
this.sourceProperty = ReflectionHelper.GetAccessor(sourceProperty);
sourceValue = value;
}
readonly IDictionary<string, object> checks = new Dictionary<string, object>();
readonly Accessor sourceProperty = ReflectionHelper.GetAccessor(sourceProperty);
readonly Member member = sourceProperty.ToMember();

public XmlTest MapsToAttribute(string attributeName, object value)
{
Expand All @@ -77,13 +63,13 @@ public XmlTest MapsToAttribute(string attributeName, object value)

public XmlTest MapsToAttribute(string attributeName)
{
checks[attributeName] = sourceValue;
checks[attributeName] = value;
return this;
}

internal void ApplyToSource(TMappingType mapping)
{
mapping.Set(member.Name, Layer.Defaults, sourceValue);
mapping.Set(member.Name, Layer.Defaults, value);
}

internal void Check(XmlDocument document)
Expand All @@ -99,7 +85,7 @@ internal void Check(XmlDocument document)
document.OutputXmlToConsole();

Assert.That(areEqual,
$"Property '{sourceProperty.InnerMember.MemberInfo.ReflectedType?.Name}.{sourceProperty.Name}' was set to '{sourceValue}' " +
$"Property '{sourceProperty.InnerMember.MemberInfo.ReflectedType?.Name}.{sourceProperty.Name}' was set to '{value}' " +
$"and was expected to be written to attribute '{check.Key}' with value '{check.Value}'. " +
$"The value was instead '{attributeValue}'");
//string.Equals()
Expand Down
1 change: 1 addition & 0 deletions src/FluentNHibernate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{83F176
FluentKey.snk = FluentKey.snk
..\global.json = ..\global.json
..\appveyor.yml = ..\appveyor.yml
..\.editorconfig = ..\.editorconfig
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{3D285AE0-E5CF-4923-B543-0D70E5C6DE18}"
Expand Down
9 changes: 2 additions & 7 deletions src/FluentNHibernate/AssemblyTypeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ namespace FluentNHibernate;
/// <summary>
/// Facade over an assembly for retrieving type instances.
/// </summary>
public class AssemblyTypeSource : ITypeSource
public class AssemblyTypeSource(Assembly source) : ITypeSource
{
readonly Assembly source;

public AssemblyTypeSource(Assembly source)
{
this.source = source ?? throw new ArgumentNullException(nameof(source));
}
readonly Assembly source = source ?? throw new ArgumentNullException(nameof(source));

#region ITypeSource Members

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,9 @@ namespace FluentNHibernate.Automapping.Alterations;
/// <summary>
/// Built-in alteration for altering an AutoPersistenceModel with instance of IAutoMappingOverride&lt;T&gt;.
/// </summary>
public class AutoMappingOverrideAlteration : IAutoMappingAlteration
/// <param name="overrideAssembly">Assembly to load overrides from.</param>
public class AutoMappingOverrideAlteration(Assembly overrideAssembly) : IAutoMappingAlteration
{
private readonly Assembly assembly;

/// <summary>
/// Constructor for AutoMappingOverrideAlteration.
/// </summary>
/// <param name="overrideAssembly">Assembly to load overrides from.</param>
public AutoMappingOverrideAlteration(Assembly overrideAssembly)
{
assembly = overrideAssembly;
}

/// <summary>
/// Alter the model
/// </summary>
Expand All @@ -30,7 +20,7 @@ public AutoMappingOverrideAlteration(Assembly overrideAssembly)
public void Alter(AutoPersistenceModel model)
{
// find all types deriving from IAutoMappingOverride<T>
var types = from type in assembly.GetExportedTypes()
var types = from type in overrideAssembly.GetExportedTypes()
where !type.IsAbstract
let entity = (from interfaceType in type.GetInterfaces()
where interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IAutoMappingOverride<>)
Expand Down
10 changes: 1 addition & 9 deletions src/FluentNHibernate/Automapping/AutoJoinPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@

namespace FluentNHibernate.Automapping;

public class AutoJoinPart<T> : JoinPart<T>
public class AutoJoinPart<T>(IList<Member> mappedMembers, string tableName) : JoinPart<T>(tableName)
{
readonly IList<Member> mappedMembers;

public AutoJoinPart(IList<Member> mappedMembers, string tableName)
: base(tableName)
{
this.mappedMembers = mappedMembers;
}

internal override void OnMemberMapped(Member member)
{
mappedMembers.Add(member);
Expand Down
9 changes: 2 additions & 7 deletions src/FluentNHibernate/Automapping/AutoMapType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace FluentNHibernate.Automapping;

public class AutoMapType
public class AutoMapType(Type type)
{
public AutoMapType(Type type)
{
Type = type;
}

public Type Type { get; set;}
public Type Type { get; set;} = type;
public bool IsMapped { get; set; }
}
Loading

0 comments on commit 366ab36

Please sign in to comment.