Skip to content

Commit

Permalink
Refactor code for CleanBaseUnitOfWork. (no longer need to Implement A…
Browse files Browse the repository at this point in the history
…bstract methods for Create Instance from Command and Query UnitOfWorks).

Nuget Version 4.!.1
  • Loading branch information
arashazimi0032 committed Mar 12, 2024
1 parent d5488f5 commit e1a4f6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
6 changes: 2 additions & 4 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>CleanTemplate</PackageId>
<Title>CleanTemplate</Title>
<Version>4.1.0</Version>
<Version>4.1.1</Version>
<Authors>arashazimi0032</Authors>
<Product>CleanTemplate</Product>
<Description>A Clean Architecture Base Template comprising all Baseic and Abstract and Contract types for initiating and structuring a .Net project.</Description>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/arashazimi0032/Core.git</RepositoryUrl>
<PackageTags>CleanTemplate;CleanArchitecture;Contracts;Abstraction</PackageTags>
<PackageReleaseNotes>- ICleanBaseMinimalApi Renamed to ICleanBaseEndPoint
- MapCleanTemplate Extension removed. just use of UseCleanTemplate for minimal APIs (Endpoints).
- Bug Fix in ICleanBaseEntityConfiguration for Add-Migration.</PackageReleaseNotes>
<PackageReleaseNotes>- Refactor code for CleanBaseUnitOfWork. (no longer need to Implement Abstract methods for Create Instance from Command and Query UnitOfWorks).</PackageReleaseNotes>
<NeutralLanguage>en</NeutralLanguage>
<PackAsTool>False</PackAsTool>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
16 changes: 12 additions & 4 deletions Core/Infrastructure/Repositories/UnitOfWork/CleanBaseUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Core.Application.ServiceLifeTimes;
using Core.Domain.IRepositories.UnitOfWork;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace Core.Infrastructure.Repositories.UnitOfWork;

Expand All @@ -13,13 +14,12 @@ public abstract class CleanBaseUnitOfWork<TContext, TCommand, TQuery> : ICleanBa
protected CleanBaseUnitOfWork(TContext context)
{
_context = context;
Commands = CreateCommandInstance(context);
Queries = CreateQueryInstance(context);

Commands = CreateInstance<TCommand>();
Queries = CreateInstance<TQuery>();
}
public TCommand Commands { get; protected set; }
public TQuery Queries { get; protected set; }
protected abstract TCommand CreateCommandInstance(TContext context);
protected abstract TQuery CreateQueryInstance(TContext context);

public DbContext GetDbContext()
{
Expand All @@ -30,4 +30,12 @@ public virtual async Task<bool> SaveChangesAsync(CancellationToken cancellationT
{
return await _context.SaveChangesAsync(cancellationToken) > 0;
}

#region Private
private TType CreateInstance<TType>()
{
ConstructorInfo commandConstructor = typeof(TType).GetConstructor(new Type[] { typeof(TContext) })!;
return (TType)commandConstructor!.Invoke(new object[] { _context });
}
#endregion
}
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,6 @@ public class UnitOfWork : CleanBaseUnitOfWork<ApplicationDbContext, CommandUnitO
public UnitOfWork(ApplicationDbContext context) : base(context)
{
}

protected override CommandUnitOfWork CreateCommandInstance(ApplicationDbContext context)
{
return new CommandUnitOfWork(context);
}

protected override QueryUnitOfWork CreateQueryInstance(ApplicationDbContext context)
{
return new QueryUnitOfWork(context);
}
}
```

Expand Down

0 comments on commit e1a4f6c

Please sign in to comment.