Dyad
OOP and REST/JSON Design
public class Person
{
public string FirstName {get;set;}
public string MiddleName {get;set;}
public string LastName {get;set;}
public string FullName {get {return $"{FirstName} {MiddleName} {FullName}";}}
}
When creating a GET endpoint for a resource, should I...
1) Pre-compute method results such as FullName() and send to the client?
2) Re-implement methods such as FullName() client side?
Thoughts:
- Pre-computation would prevent me from having to duplicate the logic client side, and any time the FullName() logic changes, it would only have to be updated in one spot, the server
- FullName() is a pretty trivial example, but I can imagine more complex functions with ever changing rules / validation
- If we pre-compute and serialize function results, it seems like we might run into different problems
- For example, the client does not know about the function dependencies and is left guessing when to re-fetch a person to update FullName()
- A PUT/PATCH request could re-send the representation after modification
- However, this is a trivial example, and doesn't work so well when there are parent/child dependencies, etc
- For example, let's imagine a Shopping Cart and its children Line Items. Cart.TotalPrice() might be a function that sums up the line item prices and their quantities. If the client changes a single line item's quantity, the client would have to be smart enough to re-fetch the entire shopping cart to get the new total. Or..., the PUT/PATCH request could respond with the entire cart and not just the modified line item
- It seems like pre-computing and serializing function results hides object relationships/dependencies from the client, leaving the client "guessing" when state becomes stale
- It seems like re-implementing methods client side is not very DRY and could cause sync issues with ever changing business rules
Would love to hear some opinions / advice on this! 🙂
25 replies
❔ Maybe Disposable Object
I want to have a function that returns an HttpClient or an error. The function would take in JSON API user credentials and return an HttpClient with the Base Address and Authorization Headers set if the credentials are valid, otherwise an error message. When consuming this function I was going to use a using statement:
using(var client = MakeNewClient(username, password))
{
}
or...
var clientOrError = MakeNewClient(username, password);
if(client.Error != null)
return;
using(clientOrError.client)
{
}
What should the return type of MakeNewClient() be in order to surface the error and still be able to use it in a using statement? Or is my approach to this wrong all-together?
13 replies
❔ Aggregate Group By Functions on Items
```
var items = new [] {
new Item { Year = 2021, Quarter = "Q1", Collection = "Modern", Price = 10 },
new Item { Year = 2021, Quarter = "Q2", Collection = "Antique", Price = 20 },
new Item { Year = 2021, Quarter = "Q3", Collection = "Modern", Price = 30 },
new Item { Year = 2021, Quarter = "Q4", Collection = "Modern", Price = 40 },
new Item { Year = 2022, Quarter = "Q1", Collection = "Antique", Price = 50 },
new Item { Year = 2022, Quarter = "Q2", Collection = "Modern", Price = 60 },
...
}
var groupBys = new []{ x => x.Year, x => x.Quarter, x => x.Collection }
//How to do this part?
//Or is there a better way?
var result = groupBys.Aggregate(items, (accumulator, groupBy) => accumulator.GroupBy(groupBy))
4 replies