63 lines
1.4 KiB
C#
63 lines
1.4 KiB
C#
using System.Linq.Expressions;
|
|
using NHibernate;
|
|
using NHibernate.Linq;
|
|
|
|
namespace Tiger.Storage;
|
|
|
|
public class Repository<T> : IRepository<T> where T : class
|
|
{
|
|
private readonly ISession _session;
|
|
|
|
public Repository(ISession session)
|
|
{
|
|
_session = session;
|
|
}
|
|
|
|
public async Task<T?> FindAsync(object id)
|
|
{
|
|
return await _session.GetAsync<T>(id);
|
|
}
|
|
|
|
public async Task SaveAsync(T entity)
|
|
{
|
|
await _session.SaveOrUpdateAsync(entity);
|
|
await _session.FlushAsync();
|
|
}
|
|
|
|
public async Task SaveManyAsync(params T[] entities)
|
|
{
|
|
foreach (var entity in entities)
|
|
{
|
|
await _session.SaveOrUpdateAsync(entity);
|
|
}
|
|
|
|
await _session.FlushAsync();
|
|
}
|
|
|
|
public async Task SaveManyAsync(IEnumerable<T> entities)
|
|
{
|
|
foreach (var entity in entities)
|
|
{
|
|
await _session.SaveOrUpdateAsync(entity);
|
|
}
|
|
|
|
await _session.FlushAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<T>> FindByAsync(Expression<Func<T, bool>>? expression)
|
|
{
|
|
var query = _session.Query<T>();
|
|
|
|
if (expression != null)
|
|
{
|
|
query = query.Where(expression);
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
|
|
public async Task<T?> FindOneByAsync(Expression<Func<T, bool>> expression)
|
|
{
|
|
return await _session.Query<T>().FirstOrDefaultAsync(expression);
|
|
}
|
|
} |