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>
{

}
7
  • i would use Dapper as an ORM: github.com/StackExchange/dapper-dot-net A lot of what you desire is already created. No sense re-inventing the wheel. Commented Jan 11, 2017 at 18:17
  • I'm very restricted to use this way since It's not my own project :(, but Thanks for the suggestion! :D Commented Jan 11, 2017 at 18:22
  • codeproject.com/Articles/814768/… Commented Jan 11, 2017 at 18:31
  • 2
    Or this is better actually: stackoverflow.com/questions/8766794/… Commented Jan 11, 2017 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! Commented Jan 11, 2017 at 18:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.