简体   繁体   中英

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.

What i have done so far :

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

    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.
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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