44 lines
1.0 KiB
C#
44 lines
1.0 KiB
C#
using MongoDB.Driver;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace JiShe.CollectBus.MongoDB
|
||
{
|
||
public class UnitOfWork : IUnitOfWork
|
||
{
|
||
private readonly IMongoContext _context;
|
||
|
||
public UnitOfWork(IMongoContext context)
|
||
{
|
||
_context = context;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 提交保存更改
|
||
/// </summary>
|
||
/// <param name="session">MongoDB 会话(session)对象</param>
|
||
/// <returns></returns>
|
||
public async Task<bool> Commit(IClientSessionHandle session)
|
||
{
|
||
return await _context.SaveChangesAsync(session) > 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化MongoDB会话对象session
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public async Task<IClientSessionHandle> InitTransaction()
|
||
{
|
||
return await _context.StartSessionAsync();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_context.Dispose();
|
||
}
|
||
}
|
||
}
|