Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

What is the best practices for using Generic Classes and Generic Method?

I am a newbie learning to develop cross platform applications with xamarin forms. Meanwhile, I always want to follow the best practices in programming, especially when using xamarin forms. However, I am a bit confuse and need some basic clarification for using generics. Whiles reading about generic class and generic method, I came across the generic class and generic method codes below for a repository, and I want to know which of the two is more advanced and a best practice.

`namespace Repository
{
public class DatabaseGeneric where T : IObject, new ()
{
private readonly SQLiteAsyncConnection connection;

    private async Task CreateConnection()
    {
        if (connection != null) 
        {
            return; 
        }

        connection = DependencyService.Get<IDatabase>().GetAsyncConnection();
        await connection.CreateTableAsync<Entity1>();
        await connection.CreateTableAsync<Entity2>();
    }

    public event EventHandler<T> OnItemAdded;
    public event EventHandler<T> OnItemUpdated;

    public async Task AddItem(T item)
    {
        await CreateConnection();
        await connection.InsertAsync(item); 
        OnItemAdded?.Invoke(this, item);
    }

    public async Task<T> GetItemAsync(int id)
    {
        await CreateConnection();
        return connection.Table<T>().Where(i => i.ID == id).FirstOrDefaultAsync();
    }

    public async Task<List<T>> GetItemsAsync()
    {
        await CreateConnection();
        return await connection.Table<T>().ToListAsync();
    }

    public async Task<List<T>> FilterItemsAsync(T table, string condition)
    {
        await CreateConnection();
        return await connection.QueryAsync<T>($ " SELECT * FROM { table } WHERE { condition } ");
    }

    public async Task<List<T>> PaginateResult(int currentPage, int recordsPerPage, string condition)
    {
        var data = await FilterItemsAsync(condition);
        return data.OrderBy(d => d.Id).Skip((currentPage - 1) * recordsPerPage).Take(recordsPerPage).ToList();
    }

    public async Task<int> GetCount()
    {
        return await connection.Table<T>().CountAsync();
    }

    public async Task UpdateItem(T item)
    {
        await CreateConnection();
        await connection.UpdateAsync(item); 
        OnItemUpdated?.Invoke(this, item);
    }

    public async Task AddOrUpdate(T item)
    {
        if (item.Id == 0) 
        {
            await AddItem(item);
        }
        else 
        {
            await UpdateItem(item);
        }
    }

    public async Task DeleteItemAsync(T item)
    {
        await CreateConnection();
        await connection.DeleteAsync(item.ID); 
    }

    public async Task<List<T>> DeleteItemsAsync()
    {
        await CreateConnection();
        await connection.DropTable<T>(); 
        await connection.CreateTable<T>();
    }
}

}`

and

`namespace Repository
{
public class DatabaseGeneric
{
private readonly SQLiteAsyncConnection connection;

    private async Task CreateConnection()
    {
        if (connection != null) 
        {
            return; 
        }

        connection = DependencyService.Get<IDatabase>().GetAsyncConnection();
        await connection.CreateTableAsync<Entity1>();
        await connection.CreateTableAsync<Entity2>();
    }

    public event EventHandler<T> OnItemAdded;
    public event EventHandler<T> OnItemUpdated;

    **public async Task AddItem(T item) where T : IObject, new ()**
    {
        await CreateConnection();
        await connection.InsertAsync(item); 
        OnItemAdded?.Invoke(this, item);
    }

    **public async Task<T> GetItemAsync(int id) where T : IObject, new ()**
    {
        await CreateConnection();
        return connection.Table<T>().Where(i => i.ID == id).FirstOrDefaultAsync();
    }

    **public async Task<List<T>> GetItemsAsync() where T : IObject, new ()**
    {
        await CreateConnection();
        return await connection.Table<T>().ToListAsync();
    }

    **public async Task<List<T>> FilterItemsAsync(T table, string condition) where T : IObject, new ()**
    {
        await CreateConnection();
        return await connection.QueryAsync<T>($ " SELECT * FROM { table } WHERE { condition } ");
    }

    **public async Task<List<T>> PaginateResult(int currentPage, int recordsPerPage, string condition) where T : IObject, new ()**
    {
        var data = await FilterItemsAsync(condition);
        return data.OrderBy(d => d.Id).Skip((currentPage - 1) * recordsPerPage).Take(recordsPerPage).ToList();
    }

    **public async Task<int> GetCount() where T : IObject, new ()**
    {
        return await connection.Table<T>().CountAsync();
    }

    **public async Task UpdateItem(T item) where T : IObject, new ()**
    {
        await CreateConnection();
        await connection.UpdateAsync(item); 
        OnItemUpdated?.Invoke(this, item);
    }

    **public async Task AddOrUpdate(T item) where T : IObject, new ()**
    {
        if (item.Id == 0) 
        {
            await AddItem(item);
        }
        else 
        {
            await UpdateItem(item);
        }
    }

    **public async Task DeleteItemAsync(T item) where T : IObject, new ()**
    {
        await CreateConnection();
        await connection.DeleteAsync(item); 
    }

    **public async Task<List<T>> DeleteItemsAsync() where T : IObject, new ()**
    {
        await CreateConnection();
        await connection.DropTable<T>(); 
        await connection.CreateTable<T>();
    }
}

}`

Answers

Sign In or Register to comment.