mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-10 05:11:07 +00:00
Migrate to new tribufu project structure
This commit is contained in:
parent
be25ee8fe3
commit
6ef2762a64
114 changed files with 35 additions and 26 deletions
43
Source/Tribufu.EntityFrameworkCore/IRepository.cs
Normal file
43
Source/Tribufu.EntityFrameworkCore/IRepository.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tribufu.EntityFrameworkCore
|
||||
{
|
||||
public interface IRepository<T, K> where T : class
|
||||
{
|
||||
void Seed();
|
||||
|
||||
Task SeedAsync();
|
||||
|
||||
IList<T> List();
|
||||
|
||||
Task<IList<T>> ListAsync();
|
||||
|
||||
IList<T> List(uint page, uint limit);
|
||||
|
||||
Task<IList<T>> ListAsync(uint page, uint limit);
|
||||
|
||||
T? Find(K key);
|
||||
|
||||
Task<T?> FindAsync(K key);
|
||||
|
||||
T? Create(T entity);
|
||||
|
||||
Task<T?> CreateAsync(T entity);
|
||||
|
||||
T? Update(T entity);
|
||||
|
||||
Task<T?> UpdateAsync(T entity);
|
||||
|
||||
void Delete(K key);
|
||||
|
||||
Task DeleteAsync(K key);
|
||||
|
||||
void Delete(T entity);
|
||||
|
||||
Task DeleteAsync(T entity);
|
||||
}
|
||||
}
|
||||
1
Source/Tribufu.EntityFrameworkCore/README.md
Normal file
1
Source/Tribufu.EntityFrameworkCore/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# Tribufu EntityFrameworkCore
|
||||
120
Source/Tribufu.EntityFrameworkCore/Repository.cs
Normal file
120
Source/Tribufu.EntityFrameworkCore/Repository.cs
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tribufu.EntityFrameworkCore
|
||||
{
|
||||
public class Repository<C, T, K> : IRepository<T, K> where C : DbContext where T : class
|
||||
{
|
||||
protected readonly C _dbContext;
|
||||
|
||||
protected readonly DbSet<T> _dbSet;
|
||||
|
||||
public Repository(C dbContext)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_dbSet = dbContext.Set<T>();
|
||||
}
|
||||
|
||||
public virtual void Seed()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual async Task SeedAsync()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual IList<T> List()
|
||||
{
|
||||
return [.. _dbSet];
|
||||
}
|
||||
|
||||
public virtual async Task<IList<T>> ListAsync()
|
||||
{
|
||||
return await _dbSet.ToListAsync();
|
||||
}
|
||||
|
||||
public virtual IList<T> List(uint page, uint limit)
|
||||
{
|
||||
return _dbSet.Skip((int)((page < 1 ? 0 : page - 1) * limit)).Take((int)limit).ToList();
|
||||
}
|
||||
|
||||
public virtual async Task<IList<T>> ListAsync(uint page, uint limit)
|
||||
{
|
||||
return await _dbSet.Skip((int)((page < 1 ? 0 : page - 1) * limit)).Take((int)limit).ToListAsync();
|
||||
}
|
||||
|
||||
public virtual T? Find(K key)
|
||||
{
|
||||
return _dbSet.Find(key);
|
||||
}
|
||||
|
||||
public virtual async Task<T?> FindAsync(K key)
|
||||
{
|
||||
return await _dbSet.FindAsync(key);
|
||||
}
|
||||
|
||||
public virtual T? Create(T entity)
|
||||
{
|
||||
_dbSet.Add(entity);
|
||||
var result = _dbContext.SaveChanges();
|
||||
return result > 0 ? entity : null;
|
||||
}
|
||||
|
||||
public virtual async Task<T?> CreateAsync(T entity)
|
||||
{
|
||||
await _dbSet.AddAsync(entity);
|
||||
var result = await _dbContext.SaveChangesAsync();
|
||||
return result > 0 ? entity : null;
|
||||
}
|
||||
|
||||
public virtual T? Update(T entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
var result = _dbContext.SaveChanges();
|
||||
return result > 0 ? entity : null;
|
||||
}
|
||||
|
||||
public virtual async Task<T?> UpdateAsync(T entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
var result = await _dbContext.SaveChangesAsync();
|
||||
return result > 0 ? entity : null;
|
||||
}
|
||||
|
||||
public virtual void Delete(K key)
|
||||
{
|
||||
var entity = _dbSet.Find(key);
|
||||
if (entity != null)
|
||||
{
|
||||
Delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(K key)
|
||||
{
|
||||
var entity = await _dbSet.FindAsync(key);
|
||||
if (entity != null)
|
||||
{
|
||||
await DeleteAsync(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Delete(T entity)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T entity)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<PackageId>Tribufu.EntityFrameworkCore</PackageId>
|
||||
<Description>Tribufu EntityFrameworkCore</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<IsPublishable>false</IsPublishable>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Tribufu.Configuration\Tribufu.Configuration.csproj" />
|
||||
<ProjectReference Include="..\Tribufu.Logging\Tribufu.Logging.csproj" />
|
||||
<ProjectReference Include="..\Tribufu.Platform\Tribufu.Platform.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue