2015-01-24 1 views
0

데이터베이스를 조작하기 위해 REST API를 생성해야한다는 요구 사항이 있습니다. WCF Data Services v5.6은 휠을 다시 쓰고 싶지 않기 때문에 필자는이 방식이 표준이되고 있다고 생각했습니다.EF6로 백업 된 일반적인 방식으로 WCF 데이터 서비스에 비즈니스 규칙을 적용하는 방법

하지만 비즈니스 규칙을 개체에 적용해야합니다. 관련된 모든 엔티티는 IsDeleted와 같은 제어 필드가있는 기본 클래스에서 파생되므로 선택/GET에 대한 예를 확인해야합니다.

  • DomainModel : 만 POCO 엔터티를 포함 (새 프로젝트
  • DataAccessLayer에 Model.tt를 분리하여 만든 :

    내 디자인은 4 개 프로젝트가가 Context.tt을 포함을 그 EventsDomainModel 컨텍스트 클래스

  • BusinessLayer : 유효성 검사를 수행하는 사용자 지정 DbContext가 포함되어 있으며, 이 순간에 더 많은 것을
  • RestApi : 웹 사이트 및 서비스.

현재, 이것은 내가 가지고 검증입니다 :

public class GenericBusinessValidator<T>:DbContext where T: class, IBaseEntity 
    { 
     private DbContext _ctx; 
     private DbSet<T> _set; 
     /// <summary> 
     /// Standard constructor 
     /// </summary> 
     /// <param name="context">The DbContext object</param> 
     public GenericBusinessValidator(DbContext context) 
     { 
      _ctx = context; 
      _set = _ctx.Set<T>(); 

     } 

     public IEnumerable<T> GetAll() 
     { 
      return _set.Where(x => x.IsActive == true); 
     } 
    } 

및 서비스 작업을 만드는 데 필요한 코드 (Events.svc), 이제 표준

public class Events : EntityFrameworkDataService<EventsDomainModel> 
    { 
     // This method is called only once to initialize service-wide policies. 
     public static void InitializeService(DataServiceConfiguration config) 
     { 
      // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. 
      // Examples: 
      config.SetEntitySetAccessRule("*", EntitySetRights.All); 
      config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); 
      config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; 
     } 
    } 

무엇인지 내가 성취하려고하는 것은 EventsDomainModel 클래스를 바꾸는 것입니다. 바닐라 DbContext에서 파생 된 클래스입니다. 첫 번째 코드에서는 두 번째 코드에서 유효성 검사를 수행하지만 그 방법을 알지 못합니다.

이해하는 방식으로, 유효성 검사기는 주어진 DbContext 내에서 특정 DbSet을 처리합니다. WCF 서비스의 코드에는 특정 dbContext가 필요합니다. 그래서

, 난 내 클래스를 particularizing없이 모든 DbSets을 검증 할 수있는 방법, 즉, 내가 달성하기 위해 노력하고있어

이 있는지 확인하고, 기본적으로

public class GenericBusinessValidator<T>:DbContext where T: class, IBaseEntity 
    { 
     private DbContext _ctx; 
     private DbSet<T> _set; 
     /// <summary> 
     /// Standard constructor 
     /// </summary> 
     /// <param name="context">The DbContext object</param> 
     public GenericBusinessValidator(DbContext context) 
     { 
      _ctx = context; 
      _set = _ctx.Set<T>(); 

     } 

     private DbSet<Venue> Venues; 
     private DbSet<EventCategory> Categories; 
     ... 
     ... 
     ... 

걸리지 않도록 그 전화처럼 http://localhost/Events.svc/EventCategories은?

모든 EventCategories이 isActive = 사실 (AP = 연합 뉴스를 나 반환 형식 = 응용 프로그램/JSON 달러 ServiceOperations에 의존하고, 따라서 나오는 것에 REST

답변