【发布时间】:2015-06-15 11:16:41
【问题描述】:
也许它是重复的,但我无法成功找到所需的答案。
实际上,我正在尝试构建通用包装器,以便通过 wcf 使用 Entity/L2Sql。所以在服务器端:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyDbContext<TTable> : IMyDbContext<TTable> where TTable:class
{
private readonly DataContext _dbContext;
private readonly Table<Device> _table;
public MyDbContext()
{
_dbContext = new WCF_DataContext(connection);
_table = _dbContext.GetTable<TTable>();
}
public void InsertOnSubmit(TTable table)
{
_table.InsertOnSubmit(table);
}
...and the same stuff further...
}
在客户端我是这样使用的
var client = new ServiceRef.MyDbContextClient<ServiceRef.PersonTable>();
client.GetData(); client.insertOnSubmit(); ...other stuff...
我了解 WCF 为通用的东西、不同的语言创建了什么,并且自然禁止直接 T,但也许有一种方法可以通过一些属性或描述来解决这个问题(例如 where T:DataContractAttribute,但这是无效的)。
或者是我开始想错了=)
对于某些视图模型,我需要每个视图模型一个 DbContext,而不是使用短的 (..dbcontext..)
【问题讨论】:
标签: c# .net wcf generics servicecontract