Skip to main content
2 of 2
added 20 characters in body; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

WCF data service class

I have this WCF data service class and I believe it is doing a lot:

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class PetSuppliesRelationalData : EntityFrameworkDataService<PetSuppliesEntities>
{       
    private string _TenantID
    {
        get
        {
            return HttpContext.Current.User.Identity.Name;
        }
    }

    public static void InitializeService(DataServiceConfiguration config)
    {
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        config.UseVerboseErrors = true;
        
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

        config.SetEntitySetAccessRule("Categories", EntitySetRights.All);
        config.SetEntitySetAccessRule("Products", EntitySetRights.All);

        config.SetEntitySetAccessRule("RealErrors", EntitySetRights.None);
        config.SetEntitySetAccessRule("OtherDiagrams", EntitySetRights.None);
        config.SetEntitySetAccessRule("Favourites", EntitySetRights.None);
        config.SetEntitySetAccessRule("Auditing", EntitySetRights.None);

        config.SetEntitySetAccessRule("Compaines", EntitySetRights.None);
        
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }

    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        base.OnStartProcessingRequest(args);
        ThrowIfNotAuthenticated();
    }

    protected override void HandleException(HandleExceptionArgs args)
    {
        LoggingFramework.LogError("Exception ", args.Exception);
        base.HandleException(args);
    }

    private static void ThrowIfNotAuthenticated()
    {
        var identity = HttpContext.Current.User.Identity;
        if (identity == null || !identity.IsAuthenticated)
        {
            throw new Exception("Not Authenticated");
        }
    }
    
    
    #region Query Intercepters
    
    [QueryInterceptor("Table1")]
    public Expression<Func<Table1, Boolean>> OnQueryTable1()
    {
        return x => x.TenantID == _TenantID;
    }
    
    // one for each table in db
    
    #endregion

    #region Change Interceptors
    
    [ChangeInterceptor("Table1")]
    public void OnChangeTable1(Table1 data1, UpdateOperations operations)
    {
        // check if user is authorized etc..
        switch (operations)
        {
            case UpdateOperations.Add:
                break;
            case UpdateOperations.Change:
                break;
            case UpdateOperations.Delete:
                break;
            case UpdateOperations.None:
                break;
        }
    }

    // one for each table in db
    
    #endregion
}   

I'm thinking moving out Query and Change interceptors out of this class but not sure if this will be a good move. I also don't know if it is possible, but the code is working as it is, so any improvement would help.

Mathematics
  • 934
  • 2
  • 8
  • 27