Skip to content

Instantly share code, notes, and snippets.

@MarcBruins
Last active November 11, 2023 09:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MarcBruins/ea2941555ea0185203ec1b489c57e669 to your computer and use it in GitHub Desktop.
Save MarcBruins/ea2941555ea0185203ec1b489c57e669 to your computer and use it in GitHub Desktop.
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;
}
}
}
@koz01
Copy link

koz01 commented Dec 1, 2017

How can i use for composite primary key table?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment