简体   繁体   English

将SQliteAsyncConnection与UnitOfWork和存储库模式一起使用

[英]Using SQliteAsyncConnection With UnitOfWork & Repository Pattern

i need to implement a fully async architecture for a windows 8 App, i use SQLite For winRT & SQLite.Net in the data layer. 我需要为Windows 8应用程序实现完全异步的体系结构,我在数据层中使用SQLite for winRT&SQLite.Net。

What i have done so far : 到目前为止我做了什么:

DAL DAL

Context : 内容:

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;
        }
    }

Generic Repository : 通用存储库:

    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 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);
        }
    }

i have some questions : 我有一些疑问 :

  1. Is it possible to implement UnitOfWork pattern With SQliteAsyncConnection. 是否可以使用SQliteAsyncConnection实现UnitOfWork模式。
  2. How To perform Commit & RollBack within Repository. 如何在存储库中执行提交和回滚。
  3. Anything i should Improve ? 我应该改善什么?

Thanks In Advance . 提前致谢 。

Is it possible to implement UnitOfWork pattern With SQliteAsyncConnection. 是否可以使用SQliteAsyncConnection实现UnitOfWork模式。

I guess it heavily depends on your business tasks. 我想这很大程度上取决于您的业务任务。 You cannot invent just abstract architecture magically covering all possible cases. 您不能神奇地发明涵盖所有可能情况的抽象体系结构。 Asynchronous data processing is not a toy in itself. 异步数据处理本身并不是玩具。 Combined with database it can quickly become an unmanageable mess of code. 与数据库结合使用,它很快就会变成难以处理的代码混乱。

How To perform Commit & RollBack within Repository. 如何在存储库中执行提交和回滚。

Well, first you need to stop access to Repository with some locking mechanism. 好吧,首先,您需要使用某种锁定机制停止对存储库的访问。 Then you need to wait until all your asynchronous operations complete. 然后,您需要等待所有异步操作完成。 And then you commit / rollback and unlock. 然后您提交/回滚并解锁。 This is generic of course and may not suite your workflow. 当然这是通用的,可能不适合您的工作流程。

Anything i should Improve ? 我应该改善什么?

You tell ;) 你告诉;)

Actually, I would heavily recommend not to use generic async database access. 实际上,我强烈建议不要使用通用异步数据库访问。 Use async database operations ONLY if you really need them to be async. 仅在确实需要异步数据库操作时才使用异步数据库操作。 Fetching one record by primary key does not need to be async. 通过主键获取一条记录不需要是异步的。 It's blazing fast enough. 它燃烧得足够快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM