WCF数据服务中的CRUD操作
问题描述:
我刚开始查看oData并开始使用WCF数据服务。所以我所问的可能不合逻辑。我已经实现了基本的东西,比如从实体框架公开数据并使用客户端来使用它。WCF数据服务中的CRUD操作
现在我想做其他CRUD操作(创建,更新,删除)并执行一些其他业务逻辑,但问题是我不明白在哪里编写代码。对于现在我需要与下面的代码SVC文件:
public class OdataPOCService : DataService< POCEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
我对WCF工作是过去的,但是,真的不明白的地方我应该写,我们使用经营合同写的代码。
种不能完成图片。基本问题是:
- 可以和我们如何执行插入,更新和删除。
- 哪里写业务逻辑。可以说我想在曝光之前更改日期格式。
- WCF数据服务可以接收复杂对象,如类和枚举,因为没有wsdl。
一些代码教程或博客将非常有帮助。
答
我通常将我的代码是这样的(比方说我们的测试机型为BankAccountModel
):
[ServiceContract]
public interface IBankAccountService {
[OperationContract]
BankAccountModel Insert(BankAccountModel item);
[OperationContract]
BankAccountModel Update(BankAccountModel item );
[OperationContract]
void Delete(string ID);
//... interface of other methods
}
这是服务合同。公开这一点,描述如何使用你的服务,但没有业务逻辑。 然后创建该接口的类:
public class BankAccountService : IBankAccountService {
public BankAccountModel Insert(BankAccountModel item)
{
// business logic for insert
}
public BankAccountModel Update(BankAccountModel item )
{
// business logic for update
}
public void Delete(string ID)
{
// business logic for delete
}
}
这将是你的后端/业务逻辑。不要暴露这个班级。客户没有(也不允许)知道这里是什么。
对于传递类似BankAccountModel
的复杂对象,您必须将其定义为DataContract
,并且要将其序列化为DataMember
。简单的例子:
[DataContract]
public class BankAccountModel {
[DataMember]
public string Code { get; set; }
[DataMember]
public string Type { get; set; }
// ...
}
谢谢DDan。我对这个东西很熟悉。但是这看起来像一个WCF服务。这是否也适用于WCF oData服务。能否请你确认。我不这样认为这将保持休息,并将提供oData提供的所有过滤和其他东西。 – Mohit
我目前无法确认,我以前从未使用过oData服务。这一定是做到这一点的方法,所以我想这一定是可能的。我会回到这个。 – DDan