Compare commits
9 Commits
f2808d090f
...
d87f36898b
| Author | SHA1 | Date | |
|---|---|---|---|
| d87f36898b | |||
| 62e43024a6 | |||
| daa3f4d5be | |||
| 10ff9b9745 | |||
| ea2340974a | |||
| fbf9488c55 | |||
| ddbb70081e | |||
| c538a8df8c | |||
| f500d9d974 |
@@ -12,9 +12,9 @@
|
||||
<PackageIcon>core_icon.png</PackageIcon>
|
||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||
<PackageTags>digital data core application clean architecture abstraction</PackageTags>
|
||||
<Version>1.3.7</Version>
|
||||
<AssemblyVersion>1.3.7</AssemblyVersion>
|
||||
<FileVersion>1.3.7</FileVersion>
|
||||
<Version>1.4.0</Version>
|
||||
<AssemblyVersion>1.4.0</AssemblyVersion>
|
||||
<FileVersion>1.4.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
#if NETFRAMEWORK
|
||||
using System;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Abstraction.Application.Repository
|
||||
{
|
||||
public static class ServiceProviderExtensions
|
||||
{
|
||||
public static IRepository<TEntity> Repository<TEntity>(this IServiceProvider serviceProvider)
|
||||
{
|
||||
return serviceProvider.GetRequiredService<IRepository<TEntity>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,9 @@
|
||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||
<PackAsTool>False</PackAsTool>
|
||||
<PackageIcon>core_icon.png</PackageIcon>
|
||||
<Version>4.1.1</Version>
|
||||
<AssemblyVersion>4.1.1</AssemblyVersion>
|
||||
<FileVersion>4.1.1</FileVersion>
|
||||
<Version>4.2.0</Version>
|
||||
<AssemblyVersion>4.2.0</AssemblyVersion>
|
||||
<FileVersion>4.2.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -42,19 +42,27 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
114
DigitalData.Core.Abstractions/Factory.cs
Normal file
114
DigitalData.Core.Abstractions/Factory.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections;
|
||||
#if NETFRAMEWORK
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Abstractions
|
||||
{
|
||||
public class Factory : IServiceProvider, IServiceCollection
|
||||
{
|
||||
public static readonly Factory Shared = new Factory();
|
||||
|
||||
private readonly IServiceCollection _serviceCollection = new ServiceCollection();
|
||||
private readonly Lazy<IServiceProvider> _lazyServiceProvider;
|
||||
|
||||
private bool IsBuilt => _lazyServiceProvider.IsValueCreated;
|
||||
|
||||
public Factory()
|
||||
{
|
||||
_lazyServiceProvider = new Lazy<IServiceProvider>(() => _serviceCollection.BuildServiceProvider());
|
||||
}
|
||||
|
||||
#region Service Provider
|
||||
public object
|
||||
#if NET
|
||||
?
|
||||
#endif
|
||||
GetService(Type serviceType)
|
||||
{
|
||||
return _lazyServiceProvider.Value.GetService(serviceType);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Service Collection
|
||||
public int Count => _serviceCollection.Count;
|
||||
|
||||
public bool IsReadOnly => _serviceCollection.IsReadOnly;
|
||||
|
||||
public ServiceDescriptor this[int index]
|
||||
{
|
||||
get => _serviceCollection[index];
|
||||
set
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
_serviceCollection[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(ServiceDescriptor item)
|
||||
{
|
||||
return _serviceCollection.IndexOf(item);
|
||||
}
|
||||
|
||||
public void Insert(int index, ServiceDescriptor item)
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
_serviceCollection.Insert(index, item);
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
_serviceCollection.RemoveAt(index);
|
||||
}
|
||||
|
||||
public void Add(ServiceDescriptor item)
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
_serviceCollection.Add(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
_serviceCollection.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(ServiceDescriptor item)
|
||||
{
|
||||
return _serviceCollection.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
|
||||
{
|
||||
_serviceCollection.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(ServiceDescriptor item)
|
||||
{
|
||||
EnsureNotBuilt();
|
||||
return _serviceCollection.Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<ServiceDescriptor> GetEnumerator()
|
||||
{
|
||||
return _serviceCollection.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return (_serviceCollection as IEnumerable).GetEnumerator();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
private void EnsureNotBuilt()
|
||||
{
|
||||
if (IsBuilt)
|
||||
throw new InvalidOperationException("Service provider has already been built. No further service modifications are allowed.");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
173
DigitalData.Core.Tests/Abstractions/FactoryTests.cs
Normal file
173
DigitalData.Core.Tests/Abstractions/FactoryTests.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using DigitalData.Core.Abstractions;
|
||||
|
||||
namespace DigitalData.Core.Tests.Abstractions
|
||||
{
|
||||
[TestFixture]
|
||||
public class FactoryTests
|
||||
{
|
||||
private Factory _factory;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_factory = new Factory();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_ServiceDescriptor_ShouldIncreaseCount()
|
||||
{
|
||||
// Arrange
|
||||
var descriptor = ServiceDescriptor.Singleton(typeof(string), "test");
|
||||
|
||||
// Act
|
||||
_factory.Add(descriptor);
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(1));
|
||||
Assert.That(_factory.Contains(descriptor), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Clear_ShouldRemoveAllServices()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "test"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 42));
|
||||
|
||||
// Act
|
||||
_factory.Clear();
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetService_ShouldReturnRegisteredInstance()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "Hello World"));
|
||||
|
||||
// Act
|
||||
var result = _factory.GetService(typeof(string));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo("Hello World"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetService_UnregisteredType_ShouldReturnNull()
|
||||
{
|
||||
// Act
|
||||
var result = _factory.GetService(typeof(int));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyAfterBuild_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
var _ = _factory.GetService(typeof(string)); // trigger build
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => _factory.Add(ServiceDescriptor.Singleton(typeof(int), 1)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_ShouldWorkBeforeBuild()
|
||||
{
|
||||
// Arrange
|
||||
var descriptor = ServiceDescriptor.Singleton(typeof(string), "Test");
|
||||
_factory.Add(descriptor);
|
||||
|
||||
// Act
|
||||
var removed = _factory.Remove(descriptor);
|
||||
|
||||
// Assert
|
||||
Assert.That(removed, Is.True);
|
||||
Assert.That(_factory.Count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Insert_ShouldInsertAtCorrectIndex()
|
||||
{
|
||||
// Arrange
|
||||
var d1 = ServiceDescriptor.Singleton(typeof(string), "A");
|
||||
var d2 = ServiceDescriptor.Singleton(typeof(int), 1);
|
||||
|
||||
_factory.Add(d1);
|
||||
_factory.Insert(0, d2);
|
||||
|
||||
// Act
|
||||
var first = _factory[0];
|
||||
|
||||
// Assert
|
||||
Assert.That(first.ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enumerator_ShouldIterateOverServices()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "a"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 5));
|
||||
|
||||
// Act
|
||||
var list = _factory.ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(list.Count, Is.EqualTo(2));
|
||||
Assert.That(list.Any(sd => sd.ServiceType == typeof(string)), Is.True);
|
||||
Assert.That(list.Any(sd => sd.ServiceType == typeof(int)), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CopyTo_ShouldCopyElements()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 10));
|
||||
var array = new ServiceDescriptor[2];
|
||||
|
||||
// Act
|
||||
_factory.CopyTo(array, 0);
|
||||
|
||||
// Assert
|
||||
Assert.That(array[0].ServiceType, Is.EqualTo(typeof(string)));
|
||||
Assert.That(array[1].ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAt_ShouldRemoveItemAtIndex()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 5));
|
||||
|
||||
// Act
|
||||
_factory.RemoveAt(0);
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(1));
|
||||
Assert.That(_factory[0].ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Indexer_Set_ShouldReplaceItem()
|
||||
{
|
||||
// Arrange
|
||||
var original = ServiceDescriptor.Singleton(typeof(string), "A");
|
||||
var replacement = ServiceDescriptor.Singleton(typeof(string), "B");
|
||||
_factory.Add(original);
|
||||
|
||||
// Act
|
||||
_factory[0] = replacement;
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory[0], Is.EqualTo(replacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
using DigitalData.Core.Tests.Mock;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Reflection;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure;
|
||||
|
||||
namespace DigitalData.Core.Tests.Infrastructure;
|
||||
|
||||
public class DbRepositoryTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
private IRepository Repo;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var builder = Host.CreateApplicationBuilder();
|
||||
|
||||
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
|
||||
|
||||
builder.Services.AddDbRepository(opt =>
|
||||
{
|
||||
opt.RegisterFromAssembly<MockDbContext>(typeof(User).Assembly);
|
||||
});
|
||||
|
||||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||
|
||||
_host = builder.Build();
|
||||
|
||||
Repo = _host.Services.GetRequiredService<IRepository>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
if (_host is IDisposable disposableHost)
|
||||
disposableHost.Dispose();
|
||||
}
|
||||
|
||||
[TestCase(true, TestName = "WhenGivenMultipleUsers")]
|
||||
[TestCase(false, TestName = "WhenGivenSingleUser")]
|
||||
public void CreateAsync_ShouldNotThrow(bool multiple)
|
||||
{
|
||||
// Arrange
|
||||
var faker = Fake.CreateUserFaker();
|
||||
|
||||
// Act & Assert
|
||||
if (multiple)
|
||||
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
|
||||
else
|
||||
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
|
||||
}
|
||||
|
||||
[TestCase(true, TestName = "WhenDtoUsed")]
|
||||
[TestCase(false, TestName = "WhenEntityUsed")]
|
||||
public async Task ReadAsync_ShouldReturnCreated(bool useDto)
|
||||
{
|
||||
// Act
|
||||
var createdUser = useDto
|
||||
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||
: await Repo.CreateAsync(Fake.User);
|
||||
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readUser, Is.Not.Null);
|
||||
Assert.That(readUser, Is.EqualTo(createdUser));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadAsync_ShouldReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
|
||||
|
||||
// Act
|
||||
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||
var upToDateUser = await Repo.Where<User>(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(upToDateUser, Is.Not.Null);
|
||||
Assert.That(upToDateUser?.FirstName, Is.EqualTo(userUpdateDto.FirstName));
|
||||
Assert.That(upToDateUser?.Email, Is.EqualTo(userUpdateDto.Email));
|
||||
Assert.That(upToDateUser?.Age, Is.EqualTo(userUpdateDto.Age));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadAsync_ShouldNotReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Act
|
||||
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
|
||||
var deletedUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readUser, Is.Not.Null);
|
||||
Assert.That(deletedUser, Is.Null);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
@@ -13,4 +13,4 @@ public class User : UserBase, IEntity
|
||||
public override bool Equals(object? obj)
|
||||
=> (obj is User user && user.GetHashCode() == GetHashCode())
|
||||
|| (obj is UserBase userBase && userBase.GetHashCode() == base.GetHashCode());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
public class UserCreateDto : UserBase, IDto<User>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
public class UserReadDto : UserBase, IDto<User>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
public class UserUpdateDto : UserBase, IDto<User>
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user