2

I'm developin an App using Xamarin.Android in C#, and I'm using Sqlite-net to manage the database, for now I'm using a model class (Animal) to define the table fields:

[Table("Animais")]
public class Animal
{
    [PrimaryKey, MaxLength(20)]
    public long AnimalId { get; set; }

    [MaxLength(100)]
    public string AnimalName {get; set; }
}

And I'm using this to Select all the Animals in the Database:

public List<Animal> SelectAll()
{
    try
    {
        using (var conn = new SQLiteConnection(System.IO.Path.Combine(Database.PATH, Database.DB_NAME)))
        {
            return conn.Table<Animal>().ToList();
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info(TAG, "Houve um erro ao listar os animais: " + ex.Message);
        return new List<Animal>();
    }
}

But I have many others table in my database, and I dont want to create a single method to every Select, Insert or Update to each table, what can I do to create a generic SelectAll() where I can pass any model to query? For the Insert and Update operations I'm using object type, I don't know if this is the best approach but here it is:

public bool Insert(object b)
{
    try
    {
        using (var conn = new SQLiteConnection(System.IO.Path.Combine(Database.PATH, Database.DB_NAME)))
        {
            conn.Insert(b);
            return true;
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info(TAG, "Houve um erro ao adicionar um novo registro no banco de dados: " + ex.Message);
        return false;
    }
}

Can you guys help me with this? Improvements will be very welcome since I'm new to C#... Thank you very much!

SOLUTION EDIT:

Base class
class BaseDao<T> where T : new()
{
    public bool Insert(T entity)
    {
        try
        {
            using (var conn = new SQLiteConnection(System.IO.Path.Combine(Database.PATH, Database.DB_NAME)))
            {
                conn.Insert(entity);
                return true;
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info(TAG, "Houve um erro ao adicionar um novo registro no banco de dados: " + ex.Message);
            return false;
        }
    }

    public List<T> SelectAll()
    {
        try
        {
            using (var conn = new SQLiteConnection(System.IO.Path.Combine(Database.PATH, Database.DB_NAME)))
            {
                return conn.Table<T>().ToList();
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info(TAG, "Houve um erro ao listar os registros: " + ex.Message);
            return new List<T>();
        }
    }
}

Child class:

class AnimalDao : BaseDao<Animal>
{

}
user3159043
  • 197
  • 1
  • 1
  • 16
  • i would use Dapper as an ORM: https://github.com/StackExchange/dapper-dot-net A lot of what you desire is already created. No sense re-inventing the wheel. – jon.r Jan 11 '17 at 18:17
  • I'm very restricted to use this way since It's not my own project :(, but Thanks for the suggestion! :D – user3159043 Jan 11 '17 at 18:22
  • https://www.codeproject.com/Articles/814768/CRUD-Operations-Using-the-Generic-Repository-Patte – jon.r Jan 11 '17 at 18:31
  • 2
    Or this is better actually: http://stackoverflow.com/questions/8766794/how-to-create-generic-data-access-object-dao-crud-methods-with-linq-to-sql – jon.r Jan 11 '17 at 18:32
  • This is great!, I did some changes and made a Edit, could you please check if this is the correct way? Thanks my friend! – user3159043 Jan 11 '17 at 18:46
  • Where is the Edit method? I don't see it. – jon.r Jan 11 '17 at 19:39
  • I made a Edit on this topic with the BaseDao and a Child class example – user3159043 Jan 11 '17 at 19:43

0 Answers0