Skip to content

Commit

Permalink
Upgrade to .NET 8 (#111)
Browse files Browse the repository at this point in the history
- Upgrade to .NET 8.
- Resolved minor issues

## Changes in generating dynamic methods

These changes were necessary because in .NET 8 the program was crashing when generating dynamic methods.

- Fixed generating values for decimal and enum types: `ILGeneratorHelper.cs`
- Fixed using local variables in dynamic methods: `DynamicMethodModelFactory.cs`, `RecordModelFactory.cs`
  • Loading branch information
satano authored Dec 18, 2023
1 parent e351165 commit cde62e7
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 142 deletions.
16 changes: 8 additions & 8 deletions src/CommandGenerator/CommandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public IEnumerable<DbCommand> GetDeleteCommands(IEnumerable ids)
AddDeleteCommandParameter(cmd, paramterName, id);
if (iterationCount > 1)
{
deleteQueryText.Append(",");
deleteQueryText.Append(',');
}
deleteQueryText.Append(paramterName);

Expand Down Expand Up @@ -249,17 +249,17 @@ public void FillCommand(DbCommand command, T item, ValueGenerated valueGenerated
}
}

private void AddDeleteCommandParameter(DbCommand cmd, string parameterName, object value)
private static void AddDeleteCommandParameter(DbCommand cmd, string parameterName, object value)
{
DbParameter newParameter = cmd.CreateParameter();
newParameter.ParameterName = parameterName;
newParameter.Value = value;
cmd.Parameters.Add(newParameter);
}

private DbCommand FinishDeleteCommand(DbCommand cmd, StringBuilder deleteQueryText)
private static DbCommand FinishDeleteCommand(DbCommand cmd, StringBuilder deleteQueryText)
{
deleteQueryText.Append(")");
deleteQueryText.Append(')');
cmd.CommandText = deleteQueryText.ToString();
return cmd;
}
Expand All @@ -272,7 +272,7 @@ private DbCommand FinishDeleteCommand(DbCommand cmd, StringBuilder deleteQueryTe
public IEnumerable<ColumnInfo> GetQueryColumns(ValueGenerated valueGenerated)
=> GetQueryColumns().Where(c => c.ValueGenerator == null || c.ValueGenerated.HasFlag(valueGenerated));

private IEnumerable<ColumnInfo> GetQueryColumns()
private List<ColumnInfo> GetQueryColumns()
{
if (_columnsInfo == null)
{
Expand Down Expand Up @@ -317,7 +317,7 @@ public object GetColumnValue(ColumnInfo columnInfo, T item, ValueGenerated value
return value;
}

internal void SetColumnValueFromValueGenerator(ColumnInfo columnInfo, T item, ValueGenerated valueGenerated)
internal static void SetColumnValueFromValueGenerator(ColumnInfo columnInfo, T item, ValueGenerated valueGenerated)
{
if ((!HasValueGeneratorOnInsert(columnInfo) || valueGenerated != ValueGenerated.OnUpdate) &&
TryGetValueFromValueGenerators(columnInfo, valueGenerated, out object generatorValue))
Expand All @@ -326,10 +326,10 @@ internal void SetColumnValueFromValueGenerator(ColumnInfo columnInfo, T item, Va
}
}

private bool HasValueGeneratorOnInsert(ColumnInfo columnInfo)
private static bool HasValueGeneratorOnInsert(ColumnInfo columnInfo)
=> (columnInfo.ValueGenerator != null && columnInfo.ValueGenerated == ValueGenerated.OnInsert);

private bool TryGetValueFromValueGenerators(ColumnInfo columnInfo, ValueGenerated valueGenerated, out object value)
private static bool TryGetValueFromValueGenerators(ColumnInfo columnInfo, ValueGenerated valueGenerated, out object value)
{
value = null;

Expand Down
70 changes: 37 additions & 33 deletions src/Data/DataRowReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace Kros.KORM.Data
[ExcludeFromCodeCoverage()]
internal class DataRowReader : DbDataReader
{
private DataTable _dataTable;
private DataRow _dataRow;
private readonly DataTable _dataTable;
private readonly DataRow _dataRow;
private bool _isOpen = true;
private bool _reachEORows = false;
private DataTable schemaTable;
private DataTable _schemaTable;

/// <summary>
/// Initializes a new instance of the <see cref="DataRowReader"/> class.
Expand Down Expand Up @@ -72,7 +72,7 @@ public override object this[int ordinal]
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}
throw new InvalidOperationException("Cannot process deleted row.");
Expand Down Expand Up @@ -180,7 +180,7 @@ public override bool GetBoolean(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -200,7 +200,7 @@ public override byte GetByte(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -226,19 +226,21 @@ public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int b
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
if (buffer == null)
{
return (long)numArray.Length;
}
int num1 = (int)dataOffset;
int num2 = Math.Min(numArray.Length - num1, length);
if (num1 < 0)
{
throw new ArgumentOutOfRangeException("dataOffset");
throw new ArgumentOutOfRangeException(nameof(dataOffset));
}
if (bufferOffset < 0 || bufferOffset > 0 && bufferOffset >= buffer.Length)
{
throw new ArgumentOutOfRangeException("bufferOffset");
throw new ArgumentOutOfRangeException(nameof(bufferOffset));
}
if (0 < num2)
{
Expand All @@ -248,7 +250,7 @@ public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int b
{
if (length < 0)
{
throw new ArgumentOutOfRangeException("length");
throw new ArgumentOutOfRangeException(nameof(length));
}
num2 = 0;
}
Expand All @@ -271,7 +273,7 @@ public override char GetChar(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -296,19 +298,21 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
if (buffer == null)
{
return (long)chArray.Length;
}
int num1 = (int)dataOffset;
int num2 = Math.Min(chArray.Length - num1, length);
if (num1 < 0)
{
throw new ArgumentOutOfRangeException("dataOffset");
throw new ArgumentOutOfRangeException(nameof(dataOffset));
}
if (bufferOffset < 0 || bufferOffset > 0 && bufferOffset >= buffer.Length)
{
throw new ArgumentOutOfRangeException("bufferOffset");
throw new ArgumentOutOfRangeException(nameof(bufferOffset));
}
if (0 < num2)
{
Expand All @@ -318,7 +322,7 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b
{
if (length < 0)
{
throw new ArgumentOutOfRangeException("length");
throw new ArgumentOutOfRangeException(nameof(length));
}
num2 = 0;
}
Expand Down Expand Up @@ -354,7 +358,7 @@ public override DateTime GetDateTime(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -374,7 +378,7 @@ public override decimal GetDecimal(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -394,7 +398,7 @@ public override double GetDouble(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand Down Expand Up @@ -426,7 +430,7 @@ public override Type GetFieldType(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -446,7 +450,7 @@ public override float GetFloat(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -466,7 +470,7 @@ public override Guid GetGuid(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -486,7 +490,7 @@ public override short GetInt16(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -506,7 +510,7 @@ public override int GetInt32(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -526,7 +530,7 @@ public override long GetInt64(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -546,7 +550,7 @@ public override string GetName(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand Down Expand Up @@ -575,11 +579,11 @@ public override int GetOrdinal(string name)
/// </exception>
public override DataTable GetSchemaTable()
{
if (this.schemaTable == null)
if (_schemaTable == null)
{
this.schemaTable = DataRowReader.GetSchemaTableFromDataTable(_dataTable);
_schemaTable = DataRowReader.GetSchemaTableFromDataTable(_dataTable);
}
return this.schemaTable;
return _schemaTable;
}

/// <summary>
Expand All @@ -598,7 +602,7 @@ public override string GetString(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -618,7 +622,7 @@ public override object GetValue(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand All @@ -634,7 +638,7 @@ public override int GetValues(object[] values)
{
if (values == null)
{
throw new ArgumentNullException("values");
throw new ArgumentNullException(nameof(values));
}
Array.Copy((Array)_dataRow.ItemArray,
(Array)values, _dataRow.ItemArray.Length > values.Length ? values.Length : _dataRow.ItemArray.Length);
Expand Down Expand Up @@ -662,7 +666,7 @@ public override bool IsDBNull(int ordinal)
}
catch (IndexOutOfRangeException)
{
throw new ArgumentOutOfRangeException("ordinal");
throw new ArgumentOutOfRangeException(nameof(ordinal));
}
}

Expand Down Expand Up @@ -693,7 +697,7 @@ internal static DataTable GetSchemaTableFromDataTable(DataTable table)
{
if (table == null)
{
throw new ArgumentNullException("DataTable");
throw new ArgumentNullException(nameof(table));
}
return new DataTableReader(table).GetSchemaTable();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Kros.KORM.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<Version>6.2.0</Version>
<TargetFrameworks>net8.0</TargetFrameworks>
<Version>7.0.0</Version>
<Authors>KROS a. s.</Authors>
<Company>KROS a. s.</Company>
<Description>KORM is fast, easy to use, micro ORM tool (Kros Object Relation Mapper).</Description>
Expand Down Expand Up @@ -47,8 +47,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Kros.Utils" Version="2.1.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Kros.Utils" Version="3.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Materializer/DynamicMethodModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ private Func<IDataReader, T> CreateFactoryForPropertySetters<T>(
DynamicMethod dynamicMethod = new(GetFactoryName, type, new Type[] { typeof(IDataReader) }, true);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

ilGenerator.DeclareLocal(typeof(T));
LocalBuilder localResult = ilGenerator.DeclareLocal(typeof(T));
ilGenerator.LogAndEmit(OpCodes.Newobj, ctor);
ilGenerator.LogAndEmit(OpCodes.Stloc_0);
ilGenerator.LogAndEmit(OpCodes.Stloc_S, localResult.LocalIndex);
EmitReaderFields(reader, tableInfo, ilGenerator, injector);
ilGenerator.CallOnAfterMaterialize(tableInfo);
ilGenerator.LogAndEmit(OpCodes.Ldloc_0);
ilGenerator.LogAndEmit(OpCodes.Ldloc, localResult.LocalIndex);
ilGenerator.LogAndEmit(OpCodes.Ret);

return dynamicMethod.CreateDelegate(Expression.GetFuncType(typeof(IDataReader), type)) as Func<IDataReader, T>;
Expand Down
21 changes: 17 additions & 4 deletions src/Materializer/ILGeneratorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,25 @@ private static void EmitSetDefaultValueForPrimitiveTypes(this ILGenerator ilGene
}
}

private static readonly FieldInfo _zeroDecimal = typeof(decimal).GetField(nameof(decimal.Zero));

private static void EmitSetDefaultValueForValueTypes(this ILGenerator ilGenerator, Type propertyType)
{
LocalBuilder local = ilGenerator.DeclareLocal(propertyType);
ilGenerator.LogAndEmit(OpCodes.Ldloca_S, local.LocalIndex);
ilGenerator.LogAndEmit(OpCodes.Initobj, local.LocalType);
ilGenerator.LogAndEmit(OpCodes.Ldloc_S, local.LocalIndex);
if (propertyType == typeof(decimal))
{
ilGenerator.LogAndEmit(OpCodes.Ldsfld, _zeroDecimal);
}
else if (propertyType.IsEnum)
{
ilGenerator.EmitSetDefaultValueForPrimitiveTypes(propertyType.GetEnumUnderlyingType());
}
else
{
LocalBuilder local = ilGenerator.DeclareLocal(propertyType);
ilGenerator.LogAndEmit(OpCodes.Ldloca_S, local.LocalIndex);
ilGenerator.LogAndEmit(OpCodes.Initobj, local.LocalType);
ilGenerator.LogAndEmit(OpCodes.Ldloc, local.LocalIndex);
}
}

private static Dictionary<string, MethodInfo> InitReaderValueGetters()
Expand Down
Loading

0 comments on commit cde62e7

Please sign in to comment.