Dapper Data Access release memory resources after closing a form

Viewed 31

I am trying to load data from SQL database using dapper so this is my SqlDataAccess class

private readonly IConfiguration _config;
public SqlDataAccess(IConfiguration config)
{
    _config = config;
}
public async Task<IEnumerable<T>> LoadData<T, U>(
    string storedProcedure,
    U parameters,
    string connectionId = "Default")
{
    using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));

    return await connection.QueryAsync<T>(storedProcedure, parameters,
        commandType: CommandType.StoredProcedure); 
}

And this is my UserModel class

public class UserModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And this is my UserData Class

private readonly ISqlDataAccess _db;   
public UserData(ISqlDataAccess db)
{
    _db = db;
}

public Task<IEnumerable<UserModel>> GetUsers() =>
    _db.LoadData<UserModel, dynamic>("dbo.spUser_GetAll", new { });

the code behind the winform looks like this

private readonly IUserData userData;
public XtraForm1(IUserData userData)
{
    InitializeComponent();
    this.userData = userData;
}
private async void simpleButton1_Click(object sender, EventArgs e)
{
    gridControl1.DataSource = await userData.GetUsers();
}
    private void FormClosedEvent(object sender, FormClosedEventArgs e)
{
    simpleButton1.Click -= simpleButton1_Click;
    gridView1.Columns.Clear();
    gridControl1.DataSource = null;
}

This is the memory usage on each step
opening Main Form 71MB
opening Form1 84MB
Load Data 157MB
Close Form1 157MB
Is there anything I need to do to release all of the data?

0 Answers
Related