I have a Silverlight application and I wanted to be able to get data (customers, orders etc) from a database.
Right now I have managed to create in the Server Application a WCF RIA service, which I can call in the client application and get the required data. The problem is that as far as I understood, the Domain Service calls are asynchronous and therefore I have to create a callback, something like this (so, this part of the code works fine)
private void CountriesCallback(LoadOperation<Country> e)
{
List<Country> countries = e.Entities.ToList();
// add the list as a data source
}
private void ShowAllCountriesButton_Click(object sender, RoutedEventArgs e)
{
MyDomainContext context = new MyDomainContext();
Action<LoadOperation<Country>> callbackCountries = new Action<LoadOperation<Country>>(CountriesCallback);
context.Load(context.GetCountriesQuery(), callbackCountries, null);
}
So this way I have a messy code in the silverlight code-behind classes, with callbacks and async methods. That's why I wanted to create some wrapper classes - with a few static methods that could return just the data that I need, something like this:
public class CountriesBL
{
public static int Add(string countryName, string countryCode)
{
MyDomainContext context = new MyDomainContext();
Country c = new Country() { Name = countryName, Code = countryCode };
context.Countries.Add(c);
SubmitOperation submitOp = context.SubmitChanges();
// always returns 0 !!!
return c.CountryId;
}
private static void RemoveCountryCallback(LoadOperation<Country> e)
{
Country c = e.Entities.FirstOrDefault();
if (c != null)
{
Remove(c);
}
}
public static void Remove(int countryID)
{
MyDomainContext context = new MyDomainContext();
EntityQuery<Country> query =
from c in context.GetCountriesQuery()
where c.CountryId == countryID
select c;
Action<LoadOperation<Country>> callbackCountries = new Action<LoadOperation<Country>>(RemoveCountryCallback);
context.Load(query, callbackCountries, null);
}
public static void Remove(Country country)
{
MyDomainContext context = new MyDomainContext();
context.Countries.Remove(country);
context.SubmitChanges();
}
public List<Country> GetAll()
{
//how can I return the collection knowing that the call is Async??
}
}
But as you can see, I cannot return the in开发者_运维知识库serted country's id, nor can I return the list of countries with this approach. Have you any other idea or suggestion on how to create a wrapper class? It would be really strange to have to use a lot of application logic and even data access elements in the user interface.
Thanks
精彩评论