Skip to content

Commit

Permalink
Upgrade to net8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonny-novikov committed Jan 19, 2024
1 parent 84f9768 commit d070050
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Slant.Entity.Demo/Slant.Entity.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>warnings</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Slant.Entity.Tests/DbContextScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Xunit;

using ObjectIDGenerator = Slant.Entity.Tests.Helpers.ObjectIDGenerator;
namespace Slant.Entity.Tests;

public sealed class DbContextScopeTests : IDisposable
Expand Down Expand Up @@ -448,7 +448,7 @@ public void Multiple_threads_which_create_a_DbContextScope_use_separate_DbContex

lock (lockObject)
{
var dbContextScopeId = idGenerator.GetId(dbContextScope, out bool _);
var dbContextScopeId = idGenerator.GetId(dbContextScope);
dbContextScopeIds.Add(dbContextScopeId);
}

Expand All @@ -457,7 +457,7 @@ public void Multiple_threads_which_create_a_DbContextScope_use_separate_DbContex

lock (lockObject)
{
var dbContextId = idGenerator.GetId(dbContext, out bool _);
var dbContextId = idGenerator.GetId(dbContext);
dbContextIds.Add(dbContextId);
}
});
Expand Down
162 changes: 162 additions & 0 deletions Slant.Entity.Tests/Helpers/ObjectIdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;

namespace Slant.Entity.Tests.Helpers;

/// <summary>
/// A class that acts like a dictionary with weakref'ed keys, meaning,
/// the entry will be removed once its key is garbage-collected.
/// required by ObjectIDGenerator
/// </summary>
internal class WeakKeyDict<TKey, TValue>
{
private struct Pair
{
private readonly WeakReference _wkey;
public TValue val;

public Pair(TKey k, TValue v)
{
_wkey = new WeakReference(k);
val = v;
}

public bool IsAlive()
{
return _wkey.IsAlive;
}
public bool IsEqual(TKey key)
{
return _wkey.Target != null && _wkey.Target.Equals(key);
}
}

private Dictionary<int, List<Pair>> dict;

public WeakKeyDict() : this(8000)
{
}
public WeakKeyDict(int capacity)
{
dict = new Dictionary<int, List<Pair>>(capacity);
}

public void Add(TKey key, TValue val)
{
int hash = key!.GetHashCode();
if (!dict.ContainsKey(hash))
{
var buckets = new List<Pair>();
buckets.Add(new Pair(key, val));
dict.Add(hash, buckets);
}
else
{
var buckets = dict[hash];
bool found = false;
for (int i = buckets.Count - 1; i >= 0; i--)
{
Pair p = buckets[i];
if (!p.IsAlive())
{
buckets.RemoveAt(i);
}
else if (p.IsEqual(key))
{
found = true;
p.val = val;
}
}
if (!found)
{
buckets.Add(new Pair(key, val));
}
}
}

public bool TryGetValue(TKey key, out TValue val)
{
int hash = key.GetHashCode();
List<Pair> buckets;
if (dict.TryGetValue(hash, out buckets))
{
for (int i = buckets.Count - 1; i >= 0; i--)
{
Pair p = buckets[i];
if (!p.IsAlive())
{
buckets.RemoveAt(i);
}
else if (p.IsEqual(key))
{
val = p.val;
return true;
}
}
}
val = default(TValue);
return false;
}

public void Compact()
{
var deadKeys = new List<int>();
foreach (KeyValuePair<int, List<Pair>> item in dict)
{
for (int i = item.Value.Count - 1; i >= 0; i--)
{
Pair p = item.Value[i];
if (!p.IsAlive())
{
item.Value.RemoveAt(i);
}
}
if (item.Value.Count < 1)
{
deadKeys.Add(item.Key);
}
}
foreach (int k in deadKeys)
{
dict.Remove(k);
}
}
}

/// <summary>
/// a class that generates a unique identifier for every object
/// </summary>
internal sealed class ObjectIDGenerator
{
private readonly WeakKeyDict<object, long> _dict;
private long _counter;

public ObjectIDGenerator()
{
_dict = new WeakKeyDict<object, long>();
_counter = 0;
}

public long GetId(object obj)
{
long id;
lock (this)
{
if (_dict.TryGetValue(obj, out id))
{
return id;
}
else
{
_counter += 1;
_dict.Add(obj, _counter);
return _counter;
}
}
}

public void Compact()
{
_dict.Compact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class SqliteMemoryDatabaseLifetimeManager : IDisposable
{
public readonly string ConnectionString = $"DataSource={Guid.NewGuid()};mode=memory;cache=shared";

private DbConnection? _keepAliveConnection;
private DbConnection _keepAliveConnection;

public SqliteMemoryDatabaseLifetimeManager()
{
Expand Down
6 changes: 3 additions & 3 deletions Slant.Entity.Tests/Slant.Entity.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<TargetFrameworks>net8.0</TargetFrameworks>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.5.3" />
Expand Down
12 changes: 6 additions & 6 deletions Slant.Entity/Slant.Entity.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<PackageId>Slant.Entity</PackageId>
<Version>7.0.12</Version>
<Version>8.0.1</Version>
<Authors>Mehdi El Gueddari;Ivan Novikov</Authors>
<PackageTags>DbContextScope;DbContext;EF;EFCore;EntityFramework;EntityFrameworkCore;UnitOfWork;AmbientDbContext;AmbientContext;RepositoryPattern</PackageTags>
<Description>A library for managing the lifetime of Entity Framework Core DbContext instances. This package is based on the original DbContextScope repository by Mehdi El Gueddari, updated for .NET 6+ and EF Core, with a number of additional improvements and bug fixes.</Description>
Expand All @@ -24,9 +24,9 @@
<None Include="LICENSE.txt" Pack="true" PackagePath="" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\icon.png" Pack="true" PackagePath="images\icon.png" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.12" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>

0 comments on commit d070050

Please sign in to comment.