Skip to content

Commit

Permalink
Moved asp.net stuff in separate project to prevent unneeded dependenc…
Browse files Browse the repository at this point in the history
…y on asp.net.
  • Loading branch information
Felix-CodingClimber committed Jan 19, 2024
1 parent 8edfd03 commit b35f4ca
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 171 deletions.
8 changes: 7 additions & 1 deletion DotNetElements.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetElements.CrudExample"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetElements.Core", "src\DotNetElements.Core\DotNetElements.Core.csproj", "{1CEE4FCD-1A35-4365-A6A1-6F05937B4E3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetElements.Core.Test", "test\DotNetElements.Core.Test\DotNetElements.Core.Test.csproj", "{9BA47821-EBE3-4290-877B-FE75340AE33E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetElements.Core.Test", "test\DotNetElements.Core.Test\DotNetElements.Core.Test.csproj", "{9BA47821-EBE3-4290-877B-FE75340AE33E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetElements.AspNetCore", "src\DotNetElements.AspNetCore\DotNetElements.AspNetCore.csproj", "{83C7E815-7B9F-42A3-AA58-703234B6A4DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,6 +29,10 @@ Global
{9BA47821-EBE3-4290-877B-FE75340AE33E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BA47821-EBE3-4290-877B-FE75340AE33E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BA47821-EBE3-4290-877B-FE75340AE33E}.Release|Any CPU.Build.0 = Release|Any CPU
{83C7E815-7B9F-42A3-AA58-703234B6A4DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83C7E815-7B9F-42A3-AA58-703234B6A4DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83C7E815-7B9F-42A3-AA58-703234B6A4DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83C7E815-7B9F-42A3-AA58-703234B6A4DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -18,16 +18,17 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MudBlazor" Version="6.12.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DotNetElements.AspNetCore\DotNetElements.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\DotNetElements.Core\DotNetElements.Core.csproj" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion samples/DotNetElements.CrudExample/GlobalUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
global using Microsoft.Extensions.Options;

global using DotNetElements.Core;
global using DotNetElements.Core.Extensions;
global using DotNetElements.Core.Extensions;
global using DotNetElements.AspNetCore;
global using DotNetElements.AspNetCore.Extensions;
20 changes: 20 additions & 0 deletions src/DotNetElements.AspNetCore/CurrentUserProviderWeb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DotNetElements.Core;
using Microsoft.AspNetCore.Http;

namespace DotNetElements.AspNetCore;

public class CurrentUserProviderWeb : ICurrentUserProvider
{
private readonly IHttpContextAccessor contextAccessor;

public CurrentUserProviderWeb(IHttpContextAccessor contextAccessor)
{
this.contextAccessor = contextAccessor;
}

// todo
public Guid GetCurrentUserId()
{
return new Guid("FF4F759C-0916-4611-9B66-306543A51B2A");
}
}
7 changes: 7 additions & 0 deletions src/DotNetElements.AspNetCore/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<PropertyGroup>
<!-- ToDo remove if issue https://github.com/dotnet/sdk/issues/36666 is resolved in newer SDK release -->
<EnableSourceControlManagerQueries>true</EnableSourceControlManagerQueries>
<UseArtifactsOutput>true</UseArtifactsOutput>
</PropertyGroup>
</Project>
21 changes: 21 additions & 0 deletions src/DotNetElements.AspNetCore/DotNetElements.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetElements.Core\DotNetElements.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using DotNetElements.Core;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetElements.AspNetCore.Extensions;

public static class ServiceCollectionExtensions
{
public static IReadOnlyList<IModule>? RegisteredModules { get; private set; }

public static IServiceCollection RegisterModules(this IServiceCollection services, Assembly moduleAssembly)
{
IEnumerable<IModule> modules = DiscoverModules(moduleAssembly);
List<IModule> registeredModules = [];

foreach (IModule module in modules)
{
module.RegisterModules(services);
registeredModules.Add(module);
}

RegisteredModules = registeredModules;

return services;
}

// todo replace with source generated version
private static IEnumerable<IModule> DiscoverModules(Assembly moduleAssembly)
{
return moduleAssembly.GetTypes()
.Where(p => p.IsAssignableTo(typeof(IModule)) && p.IsClass && !p.IsAbstract)
.Select(Activator.CreateInstance)
.Cast<IModule>();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Builder;
using DotNetElements.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetElements.Core.Extensions;
namespace DotNetElements.AspNetCore.Extensions;

public static class WebApplicationBuilderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetElements.Core.Extensions;
namespace DotNetElements.AspNetCore.Extensions;

public static class WebApplicationExtensions
{
Expand Down
1 change: 1 addition & 0 deletions src/DotNetElements.AspNetCore/GlobalUsing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using DotNetElements.Core;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetElements.Core;
namespace DotNetElements.AspNetCore;

public interface IModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Http;
using IHttpResult = Microsoft.AspNetCore.Http.IResult;

namespace DotNetElements.Core;
namespace DotNetElements.AspNetCore;

public static class CrudResultExtensions
{
Expand Down
19 changes: 0 additions & 19 deletions src/DotNetElements.Core/Core/CurrentUserProviderWeb.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ namespace DotNetElements.Core.Extensions;

public static class ServiceCollectionExtensions
{
public static IReadOnlyList<IModule>? RegisteredModules { get; private set; }

public static IServiceCollection RegisterModules(this IServiceCollection services, Assembly moduleAssembly)
{
IEnumerable<IModule> modules = DiscoverModules(moduleAssembly);
List<IModule> registeredModules = [];

foreach (IModule module in modules)
{
module.RegisterModules(services);
registeredModules.Add(module);
}

RegisteredModules = registeredModules;

return services;
}

public static IServiceCollection AddManagedRepository<TManagedRepository, TRepository, TEntity, TKey>(this IServiceCollection services)
where TEntity : Entity<TKey>
where TKey : notnull, IEquatable<TKey>
Expand All @@ -42,13 +24,4 @@ public static IServiceCollection AddDatabaseMigrationService<TDbContext>(this IS

return services;
}

// todo replace with source generated version
private static IEnumerable<IModule> DiscoverModules(Assembly moduleAssembly)
{
return moduleAssembly.GetTypes()
.Where(p => p.IsAssignableTo(typeof(IModule)) && p.IsClass && !p.IsAbstract)
.Select(Activator.CreateInstance)
.Cast<IModule>();
}
}
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Task<CrudResult<TSelf>> CreateOrUpdateAsync<TSelf>(TKey id, TSelf entity, Expres
where TSelf : Entity<TKey>, IUpdatable<TSelf>;

Task<CrudResult> DeleteAsync<TEntityToDelete>(TEntityToDelete entityToDelete)
where TEntityToDelete : IHasKey<TKey>, IHasVersionReadOnly;
where TEntityToDelete : IHasKey<TKey>;

Task<CrudResult<TUpdatableEntity>> UpdateAsync<TUpdatableEntity, TFrom>(TKey id, TFrom from)
where TUpdatableEntity : Entity<TKey>, IUpdatable<TFrom>
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IScopedRepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface IScopedRepositoryFactory<TRepository, TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull, IEquatable<TKey>
where TRepository : IRepository<TEntity, TKey>
where TRepository : IReadOnlyRepository<TEntity, TKey>
{
ScopedRepository<TRepository, TEntity, TKey> Create();
}
107 changes: 107 additions & 0 deletions src/DotNetElements.Core/Core/ManagedReadOnlyRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.Linq.Expressions;

namespace DotNetElements.Core;

public abstract class ManagedReadOnlyRepository<TRepository, TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull, IEquatable<TKey>
where TRepository : IReadOnlyRepository<TEntity, TKey>
{
protected readonly IScopedRepositoryFactory<TRepository, TEntity, TKey> RepositoryFactory;

public ManagedReadOnlyRepository(IScopedRepositoryFactory<TRepository, TEntity, TKey> repositoryFactory)
{
this.RepositoryFactory = repositoryFactory;
}

public Task<IReadOnlyList<TEntity>> GetAllAsync(CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAllAsync(cancellationToken);
}

public Task<IReadOnlyList<TEntity>> GetAllFilteredAsync(
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAllFilteredAsync(filter, orderBy, descending, cancellationToken);
}

public Task<IPagedList<TEntity>> GetAllPagedAsync(
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue,
CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAllPagedAsync(filter, orderBy, descending, page, pageSize, cancellationToken);
}

public Task<IPagedList<TProjection>> GetAllPagedWithProjectionAsync<TProjection>(
Expression<Func<IQueryable<TEntity>, IQueryable<TProjection>>> selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue,
CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAllPagedWithProjectionAsync(selector, filter, orderBy, descending, page, pageSize, cancellationToken);
}

public Task<IReadOnlyList<TProjection>> GetAllWithProjectionAsync<TProjection>(
Expression<Func<IQueryable<TEntity>,
IQueryable<TProjection>>> selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAllWithProjectionAsync(selector, filter, orderBy, descending, cancellationToken);
}

public Task<CrudResult<TEntity>> GetByIdAsync(TKey id, CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetByIdAsync(id, cancellationToken);
}

public Task<CrudResult<TEntity>> GetByIdFilteredAsync(TKey id, Expression<Func<TEntity, bool>>? filter = null, CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetByIdFilteredAsync(id, filter, cancellationToken);
}

public Task<CrudResult<TProjection>> GetByIdWithProjectionAsync<TProjection>(
TKey id,
Expression<Func<IQueryable<TEntity>, IQueryable<TProjection>>> selector,
Expression<Func<TEntity, bool>>? filter = null,
CancellationToken cancellationToken = default)
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetByIdWithProjectionAsync(id, selector, filter, cancellationToken);
}

public Task<CrudResult<AuditedModelDetails>> GetAuditedModelDetailsByIdAsync<TAuditedEntity>(TKey id, CancellationToken cancellationToken = default)
where TAuditedEntity : AuditedEntity<TKey>
{
using var repository = RepositoryFactory.Create();

return repository.Inner.GetAuditedModelDetailsByIdAsync<TAuditedEntity>(id, cancellationToken);
}
}
Loading

0 comments on commit b35f4ca

Please sign in to comment.