Question

I'm new to MVVM Light and I wonder about using Messenger and data services.

I have a dataService class and method GetAll loading data from DBF file. All data are shown in DataGrid. The path to file is selected by OpenFileDialog. When I go to another view I just need a small peace (one column) of that data. Now I wonder about how to get this data in another viewModel:

  1. Send by Messenger string with file path to second viewModel and read data by service (again connecting to file using sent path) this approach is sending smaller object by Messenger but app needs connect to file again or
  2. On first viewModel prepare collection of strings (column from DBF) and send this collection by Messenger.

I also wonder about how to get data several times. If I had GetAll method I have object with all data. Can I have something like this?:

class DataService : IDataService
{
   List<T> _allData = new List<T>();

   List<T> getAll()
   {
     ...
     _allData = ...

     return _allData;
   }
}

Now if I had another methods I can use collection _allData and don't have to connect do DB or file every time when need some data.

But in some projects I saw that every method has connection to DB. What is the best practice?

(sorry if my question is chaotic ;) )

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top