使用UnitOfWork和Repository模式的SQliteAsyncConnection

3
我需要为Windows 8应用程序实现完全异步的架构,我在数据层使用SQLite for WinRT和SQLite.Net。
到目前为止,我的工作如下:
DAL
上下文:
public interface IContext
    {
        Task InitializeDatabase();
        SQLiteAsyncConnection GetAsyncConnection();
    }

public class SQLiteAsyncContext : IContext
    {
        private SQLiteAsyncConnection _context;
        private String  _dBPath;

        public SQLiteAsyncContext()
        {
            this._dBPath = Path.Combine(
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, "DBName");
            this._context = new SQLiteAsyncConnection(_dBPath);
        }

        public async Task InitializeDatabase()
        {
            await _context.CreateTableAsync<User>();
        }

        public SQLiteAsyncConnection GetAsyncConnection()
        {
            return _context;
        }
    }

通用仓储库:
    public interface IAsyncRepository<T> where T : class
        {
            Task<int> AddAsync(T entity);
            Task<int> UpdateAsync(T entity);
            Task<int> RemoveAsync(T entity);
            Task<IList<T>> GetAllAsync();
            Task<IList<T>> GetBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate);
            Task SaveChanges();

        }

    public class AsyncRepository<T> : IAsyncRepository<T> where T : class, new()
        {
            private SQLiteAsyncConnection _context;

            public AsyncRepository(IContext _context)
            {
                this._context = _context.GetAsyncConnection();
            }

            public async Task<int> AddAsync(T entity)
            {
                return await _context.InsertAsync(entity);
            }

            public async Task<int> UpdateAsync(T entity)
            {
                return await _context.UpdateAsync(entity);
            }

            public async Task<int> RemoveAsync(T entity)
            {
                return await _context.DeleteAsync(entity);
            }

            public async Task<IList<T>> GetAllAsync()
            {
                return await _context.Table<T>().ToListAsync();
            }

            public async Task<IList<T>> GetBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
            {
                return await _context.Table<T>().Where(predicate).ToListAsync();
            }
            public Task SaveChanges()
            {
            throw new NotImplementedException();
            }
        }

BL

    public interface IAsyncServices<T> where T : class
    {
        Task<int> AddAsync(T entity);
        Task<int> UpdateAsync(T entity);
        Task<int> RemoveAsync(T entity);
        Task<IList<T>> GetAllAsync();
        Task<IList<T>> GetBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate);
    }

public class AsyncUserService : IAsyncServices<User>
    {
        private readonly IAsyncRepository<User> _asyncUserRepository;

        public AsyncUserService(IAsyncRepository<User> _asyncUserRepository)
        {
            this._asyncUserRepository = _asyncUserRepository;
        }

        public async Task<int> AddAsync(User entity)
        {
            return await _asyncUserRepository.AddAsync(entity);
        }

        public async Task<int> UpdateAsync(User entity)
        {
            return await _asyncUserRepository.UpdateAsync(entity);
        }

        public async Task<int> RemoveAsync(User entity)
        {
            return await _asyncUserRepository.RemoveAsync(entity);
        }

        public async Task<IList<User>> GetAllAsync()
        {
            return await _asyncUserRepository.GetAllAsync();
        }

        public async Task<IList<User>> GetBy(Expression<Func<User, bool>> predicate)
        {
            return await _asyncUserRepository.GetBy(predicate);
        }
    }

我有一些问题:

  1. 是否可以使用SQliteAsyncConnection实现UnitOfWork模式。
  2. 如何在Repository中执行Commit和RollBack。
  3. 有什么需要改进的吗?

提前感谢您的帮助。

1个回答

1

是否可以使用SQLiteAsyncConnection实现UnitOfWork模式。

我想这在很大程度上取决于您的业务任务。您不能神奇地发明一个覆盖所有可能情况的抽象架构。异步数据处理本身并不是一种玩具。结合数据库,它很快就会变成一堆难以管理的代码。

如何在存储库中执行Commit和RollBack。

首先,您需要通过某种锁定机制停止对存储库的访问。然后等待所有异步操作完成。然后提交/回滚并解锁。当然,这是通用的,可能并不适合您的工作流程。

我应该改进什么?

你说吧;)

实际上,我强烈建议不要使用通用的异步数据库访问。仅在确实需要异步访问时才使用异步数据库操作。通过主键获取一条记录不需要异步操作。它足够快了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接