views:

262

answers:

2

I used this side to create my demo application http://windowsclient.net/learn/video.aspx?v=314683

The site was very useful in getting my started and in their example, they created a file called EmployeeRepository.cs which appears to be the source for the data. In their example, the data was hard-wired in code. So I'm trying to learn how to get the data from a data source (like a DB). In my specific case, I want to get the data from a Microsoft Access DB. (READ ONLY, So I'll only use SELECT commands).

using System.Collections.Generic;
using Telephone_Directory_2010.Model;

namespace Telephone_Directory_2010.DataAccess
{
    public class EmployeeRepository
    {
        readonly List<Employee> _employees;

        public EmployeeRepository()
        {
            if (_employees == null)
            {
                _employees = new List<Employee>();
            }
            _employees.Add(Employee.CreateEmployee("Student One", "IT201", "Information Technology", "IT4207", "Building1", "Room650"));
            _employees.Add(Employee.CreateEmployee("Student Two", "IT201", "Information Technology", "IT4207", "Building1", "Room650"));
            _employees.Add(Employee.CreateEmployee("Student Three", "IT201", "Information Technology", "IT4207", "Building1", "Room650"));
        }

        public List<Employee> GetEmployees()
        {
            return new List<Employee>(_employees);
        }
    }
}

I found another example where an Access DB is used but it doesn't comply with MVVM. So I was trying to figure out how to add the DB file to the project, how to wire it up and bind it to a listbox (i'm not that far yet). Below is my modified file

using System.Collections.Generic;
using Telephone_Directory_2010.Model;
// integrating new code with working code
using Telephone_Directory_2010.telephone2010DataSetTableAdapters;
using System.Windows.Data;

namespace Telephone_Directory_2010.DataAccess
{
    public class EmployeeRepository
    {
        readonly List<Employee> _employees;

        // start
        // integrating new code with working code
        private telephone2010DataSet.telephone2010DataTable employeeTable;
        private CollectionView dataView;
        internal CollectionView DataView
        {
            get
            {
                if (dataView == null)
                {
                    dataView = (CollectionView) CollectionViewSource.GetDefaultView(this.DataContext);
                }
                return dataView;
            }
        }

        public EmployeeRepository()
        {
            if (_employees == null)
            {
                _employees = new List<Employee>();
            }
            telephone2010TableAdapter employeeTableAdapter = new telephone2010TableAdapter();
            employeeTable = employeeTableAdapter.GetData();
            this.DataContext = employeeTable;
        }

        public List<Employee> GetEmployees()
        {
            return new List<Employee>(_employees);
        }
    }

}

I get the following error messages when building

Error 1 'Telephone_Directory_2010.DataAccess.EmployeeRepository' does not contain a definition for 'DataContext' and no extension method 'DataContext' accepting a first argument of type 'Telephone_Directory_2010.DataAccess.EmployeeRepository' could be found (are you missing a using directive or an assembly reference?) C:\Projects\VS2010\Telephone Directory 2010\Telephone Directory 2010\DataAccess\EmployeeRepository.cs 23 90 Telephone Directory 2010

+1  A: 

The MVVM-pattern should suggest three layers:

Model (=Data-access): Retrieve data from database, here should be your EmployeeRepository. I would recommend not to use any view-specific components here, i.e. no CollectionView. Better just put your employees in a List or ObservableCollection.

ViewModel: Here you can prepare your data for the view or implement commands, which can be used by the view to retrieve or manipulate data etc. In your example this should be some EmployeeViewModel, that retrieves all the employees from the DataAccess-Layer and makes them available as public property, such that the view can bind to it. Do not put database-specific code here, that should completely be contained in the data-access-layer.

View: Your employee-View containing all the UI-stuff, buttons and all this. And of course you can put some datagrid or ItemsControl here, which binds to the ViewModel and displays its data. The datacontext of your view should be your ViewModel, at least that's the most common way how to do it.

Simpzon
Well my plan was to get the data from the DB.then run a for loop for each row in the DB foreach(employee in Table) { _employee.Add(employee.Name, employee.CourseNumber, employee.courseDepartment, employee.studentID, employee.Building, employee.RoomNumber) }the above is just pseudo code but the idea was to add it to the _employee variable. Maybe I'm on the wrong path but later I want to filter my results via a Textbox for searching (so I want to find a student named Jason Borne or search for all students taking the ENG101 course). One textbox 2search all columns
Cocoa Dev
sure, you can do so, but this way you are combining Model (DB) and ViewModel (according to MVVM), which is not necessarily bad if you don't need this extra level of abstraction.It should also work, but you will have to call the GetEmployees()-method from the view at the right time. More elegant would be putting your employees in an ObservableCollection<Employee> (and use a property "Employees" instead of a method "GetEmployees"), than you can just bind to this property and don't need to trigger the refresh from the view.
Simpzon
Well if I go that route, will I be able to filter my data without having to rerun another SQL query.Right now Im trying to get the data into my DataGrid (or Listbox).But later I want to add a "Search Box" so I can quickly type a few letters and get the results from the DB.
Cocoa Dev
+1  A: 

I think there is a couple things you need to research in order to do what you are trying to do.

  1. WPF Toolkit's DataGridVIew - Instead of using a listbox to bind your data, use this datagrid which is made to bind with a dataset or a datatable. DataGrid

  2. ADO.NET - Access uses the OleDB namespace. You will need to create a connection with the database, and retrieve the data. You can fill your dataset that is bound to your datagrid through it. OleDB

You can stick with MVVM when you have learned both of these, and from my own experience, it is worth it to learn both of these.

jsmith