Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Repository Pattern with LiteDb / Pattern improvement

$
0
0

I use LiteDb to store data, I have this generic class.

public class LiteDBAccessGeneric<T>
    {
        protected LiteCollection<T> _collection;
        private T _object;
        LiteDbConnection dbConnection = LiteDbConnection.GetLiteDbConnection();

        public LiteDBAccessGeneric()
        {
            var db = dbConnection.LiteDbAccessConnection();
            if (dbConnection.LiteDbAccessIsConnected())
            {
                this._collection = db.GetCollection<T>();
            }
        }

        public async virtual Task<ObservableCollection<T>> GetAllDataJsonAsync()
        {
            ObservableCollection<T> collection; 
            return await Task.Run(() =>
            {
                var all = this._collection.FindAll();
                collection = new ObservableCollection<T>(all);
                return collection;
            });
        }

        public async virtual Task<T> GetAllDataJsonAsync(ObjectId id)
        {
            return await Task.Run(() =>
            {
                this._object = _collection.FindById(id);
                return this._object;
            });
        }


        public async virtual Task AddDataJsonAsync(T item)
        {
            await Task.Run(() =>
            {
                this._collection.Insert(item);
            });
        }

        public async virtual Task UpdateDataJsonAsync(T item)
        {
            await Task.Run(() =>
            {
                this._collection.Update(item);
            });
        }

        public async virtual Task DeleteDataJsonAsync(ObjectId id)
        {
            await Task.Run(() =>
            {
                this._collection.Delete(id);
            });
        }

But I would to avoid have for each model have one class. How I can do this ?

My first idea is to in constructor specific type and name or database.

Thanks


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>