1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

From the outset the code looks ok. I would leave the Change and Query methods inside the class.

If the class does get abit too much then partial the class into sensible files. Also, as a side note, look up SOLID it will help you to break down and structure your code if appropriate.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.