mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
add examples repository_01
This commit is contained in:
58
FreeSql.Repository/BaseRepository.cs
Normal file
58
FreeSql.Repository/BaseRepository.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public abstract class BaseRepository<TEntity> : IRepository<TEntity>
|
||||
where TEntity : class {
|
||||
|
||||
protected IFreeSql _fsql;
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base() {
|
||||
_fsql = fsql;
|
||||
if (_fsql == null) throw new NullReferenceException("fsql 参数不可为空");
|
||||
}
|
||||
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>();
|
||||
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>();
|
||||
|
||||
public void Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrows();
|
||||
|
||||
public void Delete(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrows();
|
||||
|
||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrowsAsync();
|
||||
|
||||
public Task DeleteAsync(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrowsAsync();
|
||||
|
||||
public TEntity Insert(TEntity entity) => _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted().FirstOrDefault();
|
||||
|
||||
async public Task<TEntity> InsertAsync(TEntity entity) => (await _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync()).FirstOrDefault();
|
||||
|
||||
public void Update(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrows();
|
||||
|
||||
public Task UpdateAsync(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base(fsql) {
|
||||
}
|
||||
|
||||
public void Delete(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrows();
|
||||
|
||||
public Task DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrowsAsync();
|
||||
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).ToOne();
|
||||
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).ToOneAsync();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
|
||||
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
|
||||
}
|
||||
}
|
11
FreeSql.Repository/FreeSql.Repository.csproj
Normal file
11
FreeSql.Repository/FreeSql.Repository.csproj
Normal file
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
28
FreeSql.Repository/IBasicRepository.cs
Normal file
28
FreeSql.Repository/IBasicRepository.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
|
||||
where TEntity : class {
|
||||
TEntity Insert(TEntity entity);
|
||||
|
||||
Task<TEntity> InsertAsync(TEntity entity);
|
||||
|
||||
void Update(TEntity entity);
|
||||
|
||||
Task UpdateAsync(TEntity entity);
|
||||
|
||||
IUpdate<TEntity> UpdateDiy { get; }
|
||||
|
||||
void Delete(TEntity entity);
|
||||
|
||||
Task DeleteAsync(TEntity entity);
|
||||
}
|
||||
|
||||
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
void Delete(TKey id);
|
||||
|
||||
Task DeleteAsync(TKey id);
|
||||
}
|
||||
}
|
||||
|
21
FreeSql.Repository/IReadOnlyRepository.cs
Normal file
21
FreeSql.Repository/IReadOnlyRepository.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public interface IReadOnlyRepository<TEntity> : IRepository
|
||||
where TEntity : class {
|
||||
|
||||
ISelect<TEntity> Select { get; }
|
||||
}
|
||||
|
||||
public interface IReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity>
|
||||
where TEntity : class {
|
||||
TEntity Get(TKey id);
|
||||
|
||||
Task<TEntity> GetAsync(TKey id);
|
||||
|
||||
TEntity Find(TKey id);
|
||||
|
||||
Task<TEntity> FindAsync(TKey id);
|
||||
}
|
||||
}
|
21
FreeSql.Repository/IRepository.cs
Normal file
21
FreeSql.Repository/IRepository.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
|
||||
public interface IRepository {
|
||||
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
|
||||
where TEntity : class {
|
||||
void Delete(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user