Last active
November 11, 2023 09:25
-
-
Save MarcBruins/ea2941555ea0185203ec1b489c57e669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq.Expressions; | |
using System.Threading.Tasks; | |
using MvvmCross.Plugins.Sqlite; | |
namespace ****.Repositories | |
{ | |
public interface IRepository<T> | |
{ | |
Task<bool> InsertItem(T item); | |
Task<bool> InsertItems(IEnumerable<T> items); | |
Task<IEnumerable<T>> GetAllItems(); | |
Task<T> GetItem(string pk); | |
Task<T> GetItem(Expression<Func<T, bool>> predicate); | |
Task<IEnumerable<T>> FindItems(Expression<Func<T, bool>> predicate); | |
Task<bool> AddItem(T item); | |
Task<bool> UpdateItem(T item); | |
Task<bool> DeleteItem(T item); | |
} | |
public class BaseRepository<T> : IRepository<T> where T : class, new() | |
{ | |
private readonly IMvxSqliteConnectionFactory _sqliteConnectionFactory; | |
private readonly string _databaseName; | |
public BaseRepository(IMvxSqliteConnectionFactory sqliteConnectionFactory, string databaseName) | |
{ | |
_sqliteConnectionFactory = sqliteConnectionFactory; | |
_databaseName = databaseName; | |
using (var con = _sqliteConnectionFactory.GetConnection(_databaseName)) | |
{ | |
con.CreateTable<T>(); | |
} | |
} | |
public async Task<bool> AddItem(T item) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return isSucces(await connection.InsertAsync(item)); | |
} | |
public async Task<bool> DeleteItem(T item) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return isSucces(await connection.DeleteAsync(item)); | |
} | |
public async Task<IEnumerable<T>> FindItems(Expression<Func<T, bool>> predicate) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return await connection.Table<T>().Where(predicate).ToListAsync(); | |
} | |
public async Task<IEnumerable<T>> GetAllItems() | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return await connection.Table<T>().ToListAsync(); | |
} | |
public async Task<T> GetItem(string pk) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return await connection.GetAsync<T>(pk); | |
} | |
public async Task<T> GetItem(Expression<Func<T, bool>> predicate) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return await connection.GetAsync<T>(predicate); | |
} | |
public async Task<bool> InsertItem(T item) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return isSucces(await connection.InsertAsync(item)); | |
} | |
public async Task<bool> InsertItems(IEnumerable<T> items) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
foreach (T item in items) | |
{ | |
await connection.InsertAsync(item); | |
} | |
return true; | |
} | |
public async Task<bool> UpdateItem(T item) | |
{ | |
var connection = _sqliteConnectionFactory.GetAsyncConnection(_databaseName); | |
return isSucces(await connection.InsertOrReplaceAsync(item)); | |
} | |
private bool isSucces(int rowsAffected) | |
{ | |
if (rowsAffected > 0) | |
return true; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i use for composite primary key table?