REST class organization

YanPauloYanPaulo USMember ✭✭

How do you organize REST access classes and why?
I see some people (like me) writing a single class which handles all REST access and use it in other classes.
Example:

public class RestService {
 public async Task<Contract> GetContracts() {}
 public async Task PostContract(Contract contract) {}
 public async Task<Person> GetPeople() {}
 public async Task PostPerson(person) {}
}

And I see some people who tent to separate classes from the user's point of view about interests. In this case it would be Contract and Person, just like this:

public class ContractService {
 public async Task<Contract> GetContracts() {}
 public async Task PostContract(Contract contract) {}
}

public class PerpleService {
 public async Task<Person> GetPeople() {}
 public async Task PostPerson(person) {}
}

So, again: How do you organize REST access classes and why?

Posts

  • YanPauloYanPaulo USMember ✭✭

    Like I said, I tend to make REST access classes with methods for accessing all types of data. It's easier to write and to me it makes more sense to couple things according to functionality viewpoint, and not user's.

  • NMackayNMackay GBInsider, University mod
    edited February 25

    @YanPaulo said:
    Like I said, I tend to make REST access classes with methods for accessing all types of data. It's easier to write and to me it makes more sense to couple things according to functionality viewpoint, and not user's.

    IMHO clear SOC is good, also it means if you can run with mocked services if need be for parts of the app where the Restful implementation is not yet available so 2nd approach always for me...unless it's something basic with 4 calls but say 2 calls handles authentication & 2 calls handle data retrieval, to me they should always be separate. It can get out of hand pretty quickly.

    public interface IOrderService
    {
            Task<OrderSummary> GetOrderSummary(string token, Dictionary<string, object> Params, CancellationToken ct);
            Task<List<SearchResponse>> GetOrderList(string token, Dictionary<string, object> Params, CancellationToken ct);
    
            public async Task<List<SearchResponse>> GetOrderList(string token, Dictionary<string, object> Params, CancellationToken taskToken)
            {
                var url = string.Format(_baseUrl + "route/GetOrderList");
                return await RestServiceCall<SearchResponse>.PostFetchList(url, Params, taskToken, token: new Dictionary<string, string> { { _header, token } });
            }
    
Sign In or Register to comment.