继承WCF REST操作

继承WCF REST操作,wcf,generics,wcf-rest,Wcf,Generics,Wcf Rest,我正在玩.NET4.0上的WCFREST,最后我想知道以下想法是否有可能实现 我有一套针对EF 4.1 CF项目的非常基本的POCO。它们都继承自一个基本实体类,其唯一目的是定义PK public abstract Entity { public int Id { get; set; } } 现在,在大多数情况下,我将在更大的系统上对我的每个实体或聚合的CRUD功能进行单独的操作 e、 g 现在,这主要是为了方便,但我想这样的东西真的很方便: [ServiceContract] pub

我正在玩.NET4.0上的WCFREST,最后我想知道以下想法是否有可能实现

我有一套针对EF 4.1 CF项目的非常基本的POCO。它们都继承自一个基本实体类,其唯一目的是定义PK

public abstract Entity {
    public int Id { get; set; }
}
现在,在大多数情况下,我将在更大的系统上对我的每个实体或聚合的CRUD功能进行单独的操作

e、 g

现在,这主要是为了方便,但我想这样的东西真的很方便:

[ServiceContract]
public abstract class BaseCrudService<T> where T : Entity {
    [OperationContract]
    public void Add(T entity) {
        // commit to EF via generic methods
        fooContext.Set<T>().Add(entity);
        fooContext.SaveChanges();
    }
}

public class UserService : BaseCrudService<User> {
    // blah
}
。。。理论上我可以通过http:///userservice/add 并且像我期望的那样使用它,除了我不能继承ServiceContract修饰的类,所以不可能自动将功能级联到端点本身


我在想:有没有办法做到这一点?我知道以这种方式继承不会减少它,但还有其他方法可以做到这一点吗?在多个服务操作中重复类似的代码听起来像是以前有人会想到的事情,并形成一种更干净的实现方式。

好吧,在服务操作中使用泛型是不可能的:


您可以向UserService中的公共void AddT实体添加包装。。。但这会破坏基于泛型的框架的用途。

好吧,在服务操作中使用泛型是不可能的:


您可以向UserService中的公共void AddT实体添加包装。。。但这会破坏基于泛型的框架的用途。

您可以将接口用作服务契约,因此您可以执行以下操作:

public abstract class BaseCrudService<T> where T : Entity
{        
    public void Add(T entity)
    {
        // commit to EF via generic methods
        fooContext.Set<T>().Add(entity);
        fooContext.SaveChanges();
    }
}

public class UserService : BaseCrudService<User>, IUserService
{
    // No need to put anything in here, just need a constructable class
}

// You would need to create an interface for each service you expose like so
[ServiceContract]
public interface IUserService
{
    [OperationContract]
    void Add(User entity);
}
这样做,您不需要复制任何代码

为完整起见,以下是配置:

<system.serviceModel>
  <bindings />
  <services>
    <service name="Demo.UserService">
      <endpoint address=""
                behaviorConfiguration="json"
                binding="webHttpBinding"
                name="jsonEndpoint"
                contract="Demo.IUserService" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="json">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

您可以将接口用作服务合同,因此可以执行以下操作:

public abstract class BaseCrudService<T> where T : Entity
{        
    public void Add(T entity)
    {
        // commit to EF via generic methods
        fooContext.Set<T>().Add(entity);
        fooContext.SaveChanges();
    }
}

public class UserService : BaseCrudService<User>, IUserService
{
    // No need to put anything in here, just need a constructable class
}

// You would need to create an interface for each service you expose like so
[ServiceContract]
public interface IUserService
{
    [OperationContract]
    void Add(User entity);
}
这样做,您不需要复制任何代码

为完整起见,以下是配置:

<system.serviceModel>
  <bindings />
  <services>
    <service name="Demo.UserService">
      <endpoint address=""
                behaviorConfiguration="json"
                binding="webHttpBinding"
                name="jsonEndpoint"
                contract="Demo.IUserService" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="json">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

我下面的解决方案是否适用于用户1039947?我很想知道你是否满意我下面的解决方案是否适用于用户1039947?我很想知道您是否对它满意这工作得很好,只有一个变化:我还将接口声明为泛型接口,即接口IUserService,其中T:Entity,并使用泛型类型的相同类继承了基本服务和服务契约。谢谢这非常有效,只有一个更改:我还将接口声明为泛型接口,即interface IUserService,其中T:Entity,并使用泛型类型的相同类继承了基本服务和服务契约。谢谢