Silverlight 应用程序 - 域服务的包装器 (WCF RIA)
我有一个 Silverlight 应用程序,我希望能够从数据库获取数据(客户、订单等)。
现在,我已成功在服务器应用程序中创建 WCF RIA 服务,我可以在客户端应用程序中调用该服务并获取所需的数据。问题是,据我所知,域服务调用是异步的,因此我必须创建一个回调,类似这样的东西(所以,这部分代码工作正常)
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);
}
所以这样我在 silverlight 中有一个凌乱的代码代码隐藏类,具有回调和异步方法。这就是为什么我想创建一些包装类 - 使用一些静态方法可以只返回我需要的数据,如下所示:
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??
}
}
但如您所见,我无法返回插入的国家/地区的 id,也无法返回列表采用这种方法的国家。您对如何创建包装类还有其他想法或建议吗?如果必须在用户界面中使用大量应用程序逻辑甚至数据访问元素,那确实很奇怪。
谢谢
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 inserted 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论