Skip to content

Commit a7af6a0

Browse files
committed
here we go again
1 parent 2cef1ae commit a7af6a0

22 files changed

+769
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Deploy CodeChavez Repository Pattern NuGet
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
build-and-push:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Setup .NET (use global.json)
17+
uses: actions/setup-dotnet@v4
18+
19+
- name: ls
20+
run: ls
21+
22+
- name: ls src
23+
run: ls src
24+
25+
# - name: Restore dependencies
26+
# run: dotnet restore **/src/CodeChavez.Repository.Pattern/CodeChavez.Repository.Pattern.csproj --configfile nuget.config --no-cache
27+
28+
# - name: Build project
29+
# run: dotnet build **/codechavez.repository.pattern/CodeChavez.Repository.Pattern.csproj --configuration Release
30+
31+
# - name: Package Common
32+
# run: dotnet pack **/codechavez.repository.pattern/CodeChavez.Repository.Pattern.csproj --configuration Release --no-build --output ./artifacts1
33+
34+
# - name: Push NuGet package
35+
# run: dotnet nuget push "./artifacts1/*.nupkg" --api-key $NUGET_AUTH_TOKEN --source https://api.nuget.org/v3/index.json
36+
# env:
37+
# NUGET_AUTH_TOKEN: ${{ secrets.NUGET_API_KEY }}

CodeChavez.Repository.Pattern.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36301.6
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeChavez.Repository.Pattern", "src\CodeChavez.Repository.Pattern\CodeChavez.Repository.Pattern.csproj", "{16F00C24-5EE1-4250-8EDA-3BFA637E6F51}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeChavez.Repository.Pattern.Postgres", "src\CodeChavez.Repository.Pattern.Postgres\CodeChavez.Repository.Pattern.Postgres.csproj", "{B0284CED-7A82-417B-BFFD-E1B3FC354FD9}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeChavez.Repository.Pattern.MongoDB", "src\CodeChavez.Repository.Pattern.MongoDB\CodeChavez.Repository.Pattern.MongoDB.csproj", "{87DBC4E1-BEFA-4137-9CCF-A3BF452BBEB6}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{16F00C24-5EE1-4250-8EDA-3BFA637E6F51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{16F00C24-5EE1-4250-8EDA-3BFA637E6F51}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{16F00C24-5EE1-4250-8EDA-3BFA637E6F51}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{16F00C24-5EE1-4250-8EDA-3BFA637E6F51}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{B0284CED-7A82-417B-BFFD-E1B3FC354FD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{B0284CED-7A82-417B-BFFD-E1B3FC354FD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{B0284CED-7A82-417B-BFFD-E1B3FC354FD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{B0284CED-7A82-417B-BFFD-E1B3FC354FD9}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{87DBC4E1-BEFA-4137-9CCF-A3BF452BBEB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{87DBC4E1-BEFA-4137-9CCF-A3BF452BBEB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{87DBC4E1-BEFA-4137-9CCF-A3BF452BBEB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{87DBC4E1-BEFA-4137-9CCF-A3BF452BBEB6}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {760CF65A-7C43-4CAB-B8C0-2F463CEFAE43}
36+
EndGlobalSection
37+
EndGlobal

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright [2020-2025] [Victor A Chavez]
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

global.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.305",
4+
"rollForward": "latestFeature"
5+
}
6+
}

nuget.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
6+
</packageSources>
7+
</configuration>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8+
<PackageId>CodeChavez.Repository.Pattern.MongoDB</PackageId>
9+
<Title>CodeChavez Repository Pattern MongoDB Library</Title>
10+
<Authors>Victor A Chavez</Authors>
11+
<Product>CodeChavez.Repository.Pattern.MongoDB</Product>
12+
<Version>9.0.0</Version>
13+
<Copyright>2020-2025</Copyright>
14+
<Description>Has some useful extension and utilities that allow for every day development</Description>
15+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
16+
<PackageReadmeFile>README.md</PackageReadmeFile>
17+
<PackageProjectUrl>https://github.com/codechavez/codechavez.repository.pattern</PackageProjectUrl>
18+
<RepositoryUrl>https://github.com/codechavez/codechavez.repository.pattern</RepositoryUrl>
19+
<PackageLicenseFile></PackageLicenseFile>
20+
<PackageTags>CodeChavez,Repository,Pattern,MongoDB,Common,Helpers,Utilities,.net</PackageTags>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.9" />
25+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
26+
<PrivateAssets>all</PrivateAssets>
27+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
28+
</PackageReference>
29+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.9" />
30+
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="9.0.1" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
35+
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<ProjectReference Include="..\codechavez.repository.pattern\CodeChavez.Repository.Pattern.csproj" />
40+
</ItemGroup>
41+
42+
</Project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using CodeChavez.Common.Extensions;
2+
using CodeChavez.Repository.Pattern.Extensions;
3+
using CodeChavez.Repository.Pattern.Interfaces;
4+
using MediatR;
5+
using Microsoft.EntityFrameworkCore;
6+
using MongoDB.Bson;
7+
using System.Linq.Expressions;
8+
9+
namespace CodeChavez.Repository.Pattern.MongoDB;
10+
11+
public class MongoRepository<TEntity> : INoSQLRepository<TEntity> where TEntity : DomainDocument
12+
{
13+
private readonly DbContext _dbContext;
14+
private readonly IMediator _mediator;
15+
private readonly IQueryableCacheService _cache;
16+
17+
public MongoRepository(
18+
DbContext dbContext,
19+
IQueryableCacheService cache,
20+
IMediator mediator)
21+
{
22+
_dbContext = dbContext;
23+
_mediator = mediator;
24+
_cache = cache;
25+
}
26+
27+
public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
28+
{
29+
_ = await _dbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
30+
_dbContext.Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;
31+
await _dbContext.SaveChangesAsync();
32+
33+
if (_mediator.IsNotNull())
34+
await _mediator.DispatchDomainDocumentEventsAsync(entity);
35+
36+
return entity;
37+
}
38+
39+
public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
40+
{
41+
throw new NotImplementedException();
42+
}
43+
44+
public Task DeleteAsync<TKey>(TKey id, CancellationToken cancellationToken = default)
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
public async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate,
50+
bool readOnly = true, bool cache = true,
51+
CancellationToken cancellationToken = default)
52+
{
53+
if (readOnly && cache)
54+
return await _dbContext.Set<TEntity>()
55+
.AsNoTracking()
56+
.Where(predicate)
57+
.FromCacheAsync(_cache, cancellationToken);
58+
else if (readOnly && cache == false)
59+
return await _dbContext.Set<TEntity>()
60+
.AsNoTracking()
61+
.Where(predicate)
62+
.ToListAsync(cancellationToken);
63+
else
64+
return await _dbContext.Set<TEntity>()
65+
.Where(predicate)
66+
.FromCacheAsync(_cache, cancellationToken);
67+
}
68+
69+
public async Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, bool readOnly = true, bool cache = true, CancellationToken cancellationToken = default)
70+
{
71+
if (readOnly && cache)
72+
return await _dbContext.Set<TEntity>().AsNoTracking()
73+
.FromCacheSingleOrDefaultAsync(predicate, _cache, cancellationToken);
74+
else if (readOnly && cache == false)
75+
return await _dbContext.Set<TEntity>().AsNoTracking()
76+
.FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken);
77+
else
78+
return await _dbContext.Set<TEntity>()
79+
.FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken);
80+
}
81+
82+
public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
83+
{
84+
var existing = await _dbContext.Set<TEntity>().FindAsync(entity.Id);
85+
86+
if (existing == null)
87+
throw new KeyNotFoundException("Not found");
88+
89+
if (existing != null)
90+
_dbContext.Set<TEntity>().Entry(existing).State = EntityState.Detached;
91+
92+
_dbContext.Set<TEntity>().Attach(entity);
93+
94+
_dbContext.Update(entity);
95+
await _dbContext.SaveChangesAsync();
96+
97+
if (_mediator.IsNotNull())
98+
await _mediator.DispatchDomainDocumentEventsAsync(entity);
99+
100+
return entity;
101+
}
102+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8+
<PackageId>CodeChavez.Repository.Pattern.Postgres</PackageId>
9+
<Title>CodeChavez Repository Pattern Postgres Library</Title>
10+
<Authors>Victor A Chavez</Authors>
11+
<Product>CodeChavez.Repository.Pattern.Postgres</Product>
12+
<Version>9.0.0</Version>
13+
<Copyright>2020-2025</Copyright>
14+
<Description>Has some useful extension and utilities that allow for every day development</Description>
15+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
16+
<PackageReadmeFile>README.md</PackageReadmeFile>
17+
<PackageProjectUrl>https://github.com/codechavez/codechavez.repository.pattern</PackageProjectUrl>
18+
<RepositoryUrl>https://github.com/codechavez/codechavez.repository.pattern</RepositoryUrl>
19+
<PackageLicenseFile></PackageLicenseFile>
20+
<PackageTags>CodeChavez,Repository,Pattern,Postgres,Common,Helpers,Utilities,.net</PackageTags>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.9" />
25+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
26+
<PrivateAssets>all</PrivateAssets>
27+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
28+
</PackageReference>
29+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.9" />
30+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9">
31+
<PrivateAssets>all</PrivateAssets>
32+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
33+
</PackageReference>
34+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
35+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="9.0.4" />
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
40+
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<ProjectReference Include="..\codechavez.repository.pattern\CodeChavez.Repository.Pattern.csproj" />
45+
</ItemGroup>
46+
47+
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using CodeChavez.Common.Extensions;
2+
using CodeChavez.Repository.Pattern.Extensions;
3+
using CodeChavez.Repository.Pattern.Interfaces;
4+
using MediatR;
5+
using Microsoft.EntityFrameworkCore;
6+
using System.Linq.Expressions;
7+
8+
namespace CodeChavez.Repository.Pattern.Postgres;
9+
10+
public class Repository<TEntity>(DbContext context) : IRepository<TEntity> where TEntity : DomainEntity
11+
{
12+
private readonly DbContext _dbcontext;
13+
private readonly IMediator _mediator;
14+
private readonly IQueryableCacheService _cache;
15+
16+
public Repository(
17+
DbContext context,
18+
IQueryableCacheService cache,
19+
IMediator mediator) : this(context)
20+
{
21+
_dbcontext = context;
22+
_mediator = mediator;
23+
_cache = cache;
24+
}
25+
26+
public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
27+
{
28+
_ = await _dbcontext.Set<TEntity>().AddAsync(entity, cancellationToken);
29+
_ = await _dbcontext.SaveChangesAsync(cancellationToken);
30+
31+
if (_mediator.IsNotNull())
32+
await _mediator.DispatchDomainEntityEventsAsync(entity);
33+
34+
return entity;
35+
}
36+
37+
public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
38+
{
39+
return await _dbcontext.Set<TEntity>()
40+
.AsNoTracking()
41+
.AnyAsync(predicate, cancellationToken);
42+
}
43+
44+
public async Task DeleteAsync<TKey>(TKey id, CancellationToken cancellationToken = default)
45+
{
46+
var found = await _dbcontext.Set<TEntity>()
47+
.FindAsync(id, cancellationToken) ?? throw new InvalidOperationException("Entity not found");
48+
_ = _dbcontext.Set<TEntity>().Remove(found);
49+
_ = await _dbcontext.SaveChangesAsync(cancellationToken);
50+
}
51+
52+
public async Task<IEnumerable<TEntity>> FindAsync(
53+
Expression<Func<TEntity, bool>> predicate,
54+
bool readOnly = true,
55+
bool cache = true,
56+
CancellationToken cancellationToken = default)
57+
{
58+
if (readOnly && cache)
59+
return await _dbcontext.Set<TEntity>()
60+
.AsNoTracking()
61+
.Where(predicate)
62+
.FromCacheAsync(_cache,cancellationToken);
63+
else if (readOnly && cache == false)
64+
return await _dbcontext.Set<TEntity>()
65+
.AsNoTracking()
66+
.Where(predicate)
67+
.ToListAsync(cancellationToken);
68+
else
69+
return await _dbcontext.Set<TEntity>()
70+
.Where(predicate)
71+
.FromCacheAsync(_cache,cancellationToken);
72+
}
73+
74+
public async Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, bool readOnly = true, bool cache = true, CancellationToken cancellationToken = default)
75+
{
76+
if (readOnly && cache)
77+
return await _dbcontext.Set<TEntity>().AsNoTracking()
78+
.FromCacheSingleOrDefaultAsync(predicate, _cache, cancellationToken);
79+
else if (readOnly && cache == false)
80+
return await _dbcontext.Set<TEntity>().AsNoTracking()
81+
.FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken);
82+
else
83+
return await _dbcontext.Set<TEntity>()
84+
.FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken);
85+
}
86+
87+
public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
88+
{
89+
_ = _dbcontext.Set<TEntity>().Update(entity);
90+
_ = await _dbcontext.SaveChangesAsync(cancellationToken);
91+
92+
if (_mediator.IsNotNull())
93+
await _mediator.DispatchDomainEntityEventsAsync(entity);
94+
95+
return entity;
96+
}
97+
}

0 commit comments

Comments
 (0)