if you're searching in a large number of data, filtering the data using database queries might be faster because Databases usually do the search pretty quick.
Also if you load the data once (into RAM), when data is changed then the DataGrid shows invalid data.
Best practice might be to write several DataService functions such as :
List<ColumnType> GetColumn(int column)
{
var data = new List<ColumnType>();
using (var connection = new MyConnection())
{
//load data
}
return data;
}
in each of these functions connect and disconnect to the Database to get the items. but surely putting a function such as GetSingle(ItemId)
in a loop is a mistake.
I suggest the approach with less coupling, avoiding many links between classes or using of static properties for the sake of simplicity. because usually it does not make a significant performance improvement to keep the data and avoid connecting to database. however I can't tell for sure, it depends on your application.
EDIT:
In case your Db file is very small, you can sacrifice simplicity for performance and load all data once and store it in RAM. you can also use a standard Repository like this:
public class Repository<TModel> where TModel: class
{
public Repository<TModel>(Context context)
{
_context = context;
}
private Context _context;
...
public IEnumerable<TModel> Find(Expression<Func<TModel, bool>> where)
{
return _context.CreateObjectSet<TModel>().Where(where);
}
public IEnumerable<TResult> GetColumn(Func<TSource, TResult> selector)
{
return _context.CreateObjectSet<TModel>().Select(selector);
}
}
Context is where you put all loaded data. it should has a generic function like this:
public class Context
{
private List<Customer> _customerList;
private List<Product> _productList;
public Context()
{
//Load All Lists here or in the following function instead
}
public List<TModel> CreateObjectSet<TModel>() where TModel : class
{
if (TModel is Customer)
{
//you can load _customerList here instead of in constructor
//check if _customerList is null then load it here for now and future use
return _customerList;
}
if (TModel is Product)
{
//check if _productList is null then load it here for now and future use
return _productList;
}
...
throw...
}
}
now you can easily query your Context data using lambda expressions:
var repository = new Repository<Product>(context);
List<string> nameColumn = repository.GetColumn(x => x.Name);