1

I currently have a class similar to the below (omited error checking)

Currently this is how the database is wrapped into easier to use objects.

I was wondering if I was going to be loading say 50,000 orders if I should be creating the table adapters once and then reusing them, instead of creating a new table adapter for each order?

I have not seen any real speed issues doing it this way. But would it be best practise to not keep creating new table adapters each time?

If so is there a pre-existing patten to handle this?

class Order
{
    public int Id {get; private set;}
    public string CustomerEmail { get; set; }
    public SaleLocaion SalePoint { get; set; }

    public Order(int orderId)
    {
        using (var ta = new OrdersTableAdapter())
        {
            using (var res = ta.GetDataById(orderId))
            {
                if (res.Count == 1)
                {
                    var row = res.First();
                    Id = row.Id;
                    CustomerEmail = row.Email;
                    SalePoint = new SaleLocaion(row.SaleLocationId);
                }
            }
        }
    }
}



class SaleLocaion
{
    public int Id {get; private set;}
    public string Name { get; private set; }

    public SaleLocaion(int locationId)
    {
        using (var ta = new SaleLocationAdapter())
        {
            using (var res = ta.GetDataById(locationId))
            {
                if (res.Count == 1)
                {
                    var row = res.First();
                    Id = row.Id;
                    Name = row.Name;
                }
            }
        }
    }
}
james
  • 1,031
  • 1
  • 15
  • 31

1 Answers1

1

It doesn't really matter if you reuse them or not. They implement IDisposable but just because they inherit from Component which implements IDisposable because of the DataSet's drag and drop capabilities. But it's not necessary to dispose them for the same reason why DataSet and DataTable implement IDisposable but don't really need to be disposed.

The constructor or the field initialization are also very cheap since all fields are null and the constructor only contains:

public OrdersTableAdapter() { // an examplary auto-generated table-adapter constructor
    this.ClearBeforeFill = true;
}

So choose the most readable and fail-safe approach.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939