Generic Repository for SQLite-Net in Xamarin Project

后端 未结 2 880
梦如初夏
梦如初夏 2020-12-07 16:46

I am wondering if there is a way to write a generic repository for my Xamarin project versus writing a different Repository for each entity in my object. The Xamarin Tasky P

2条回答
  •  不思量自难忘°
    2020-12-07 17:36

    My implementation with the help of unity IOC is given below, My project includes the PCL, Xamarin Android & Xamarin iOS projects

    Define a base model with primary key

    public class BaseModel
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
    }
    

    Define a generic base repository as shown below

    public interface IBaseRepository : IDisposable
        where T :BaseModel, new()
        {
            List GetItems();
    
            T GetItem(int id);
    
            int GetItemsCount();
    
            int SaveItem(T item);
    
            int SaveAllItem(IEnumerable items);
        }
    
    
    public class BaseRepository : BaseRepository where T : BaseModel, new()
        {
            private static readonly object locker = new object();
            protected SQLiteConnection DatabaseConnection;
            public BaseRepository(string dbPath)
            {
                DatabaseConnection = new SQLiteConnection(dbPath);
                DatabaseConnection.CreateTable();
            }
    
            public List GetItems()
            {
                lock (locker)
                {
                    return DatabaseConnection.Table().ToList();
                }
            }
    
            public int GetItemsCount()
            {
                lock (locker)
                {
                    return DatabaseConnection.Table().Count();
                }
            }
    
            public T GetItem(int id)
            {
                lock (locker)
                {
                    return DatabaseConnection.Table().Where(i => i.Id == id).FirstOrDefault();
                }
            }
    
            public int SaveItem(T item)
            {
                lock (locker)
                {
                    if (item.Id != 0)
                    {
                        return DatabaseConnection.Update(item);
                    }
                    else
                    {
                        return DatabaseConnection.Insert(item);
                    }
                }
            }
    
    
        }
    

    Define two sample classes which are inherited from the BaseModel

    public class Entity1 : BaseModel
        {
            public int ItemName
            {
                get;
                set;
            }
        }
    
    
    public class Entity2 : BaseModel
    {
        public int Description
        {
            get;
            set;
        }
    }
    
    
    public static UnityContainer Container { get; private set; }
    
        public static void InitializeUnityContainer()
        {
            if (Container == null)
                Container = new UnityContainer();
        }
    

    Register

    Container.RegisterInstance>(new BaseRepository(DatabasePath));
        Container.RegisterInstance>(new BaseRepository(DatabasePath));
    

    resolve like this

    using (var repo1 = App.Container.Resolve>())
    {
    
    }
    

提交回复
热议问题