Wednesday, July 8, 2009

Creating a WPF DALinq Client App

Since RemObjects DataAbstract for .NET doesn't have a wizard like it does for WinForms projects yet you have to do a few extra steps yourself. I started with the DALinq project that I'd been working on in recent blog posts and simply added a new WPF project to the solution. I named the project WPFLinqClient and after the new project is created we're only a few steps away.

  1. Add a new Component Class to the project using the Add New Item wizard and name it something, I named mine "DataService"

  2. Open the DataService component in the designer and add a BinMessage, a LinqRemoteDataAdapter, a client channel (ipHttpClientChannel in my case), Bin2DataStreamer and a RemoteService on the designer surface and wire them appropriately

  3. Since this is a simple exercise I made the Modifier for the LinqRemoteDataAdapter on the DataService component public so I could access it easily. For a larger project you'll probably want to do this in a more OOP manner.

  4. Select the LinqRemoteDataAdapter's Smart Tag and chose the "Create DA LINQ Classes" option. Be sure to have your server running when you run the wizard or you'll get an error because it can't connect. Afterwards you should have a TableDefinitions.cs file automatically added to the WPF client's project.  

  5. Right click on your WPF project and use the Add Existing Item wizard to insert a shortcut to the DataAbstract Server's _Intf file. Be sure to use the drop down menu and select "Add As Link".

  6. Add a DataGrid and a couple buttons to the main Window. One button will be for opening the query and one for updating the query after any changes you make using DALinq DataBinding.

  7. Add a couple variables to use and instantiate our DataService so we can get the data like this:

    private IQueryable<Customers> _list;
    private DataService _dataService;

    public Window1()
    {
    InitializeComponent();
    _dataService = new DataService();
    }

  8. Add the following code behind our query button:

    private void btnOpenQuery_Click(object sender, RoutedEventArgs e)
    {
    _list = from cust in _data.linqRemoteDataAdapter.GetTable<Customers>()
    where (cust.City == "México D.F.")
    orderby cust.ContactName ascending
    select cust;

    dataGrid1.AutoGenerateColumns = true;
    dataGrid1.ItemsSource = null;
    dataGrid1.ItemsSource = _list.ToDABindingList();
    }

  9. We'll add this DALinq DataBinding code behind our update button:

    private void btnUpdate_Click(object sender, RoutedEventArgs e)
    {
    ((DABindingList<Customers>)dataGrid1.ItemsSource).SaveChanges(_dataService.linqRemoteDataAdapter);
    _dataService.linqRemoteDataAdapter.ApplyChanges();
    }

The steps look like more effort than they really are. Particularly once you get comfortable with the framework. Since I'm still somewhat new to WPF that's where I spent most of my time learning new things. In the end we have a nice little form that opens our LINQ query and updates it for us automagically. The end result looks like this:



This test project can be downloaded here.

About The Author

Ron Grove draws on over ten years of training, network administration and development experience. He loves to work with new technology and see how that technology can be best utilized by his clients. You can find him through his company Evanoah, LLC and his LinkedIn profile is here.

0 comments:

Post a Comment