Dyad
Dyad
CC#
Created by Dyad on 3/27/2024 in #help
Better approach to grouping and ordering items?
Should containers be in charge of ordering, or the items themselves?

Approach 1:

class Item
{
string GroupId;
int GroupOrder;
}

var items = [...];
var groups = items.orderBy(x => x.GroupOrder).groupBy(x => x.GroupId);

This approach seems tedious if I want to reorder items, since I might have to update many items GroupOrder when moving one.

Approach 2:

class Group
{
string Id;
List<Item> Items;
}

var groups = new List<Groups>
{
new Group { Id = "group.one", Items = ... },
new Group { Id = "group.two", Items = ... },
...
}

This approach seems easier to move items around, but involves manual entry into the Lists themselves
Should containers be in charge of ordering, or the items themselves?

Approach 1:

class Item
{
string GroupId;
int GroupOrder;
}

var items = [...];
var groups = items.orderBy(x => x.GroupOrder).groupBy(x => x.GroupId);

This approach seems tedious if I want to reorder items, since I might have to update many items GroupOrder when moving one.

Approach 2:

class Group
{
string Id;
List<Item> Items;
}

var groups = new List<Groups>
{
new Group { Id = "group.one", Items = ... },
new Group { Id = "group.two", Items = ... },
...
}

This approach seems easier to move items around, but involves manual entry into the Lists themselves
22 replies
CC#
Created by Dyad on 2/20/2024 in #help
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
CC#
Created by Dyad on 1/11/2024 in #help
How to do this with LINQ?
``` var values = new []{ 1, 2, 3, 4, 5} int winner; for(int i = 0; i < values.Count; i++) { var value = values[i]; var r = value * 2; if(r > 7) { winner = r; break; } }
51 replies
CC#
Created by Dyad on 8/31/2023 in #help
❔ 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
CC#
Created by Dyad on 6/21/2023 in #help
❔ Create list from items and other lists
Whats the best way to create a new list that combines an item A, an item B, and a list C?
18 replies
CC#
Created by Dyad on 6/6/2023 in #help
❔ 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
CC#
Created by Dyad on 5/17/2023 in #help
❔ How to create a list of generic objects?
public class Item
{
DateTime Date;
string Name;
int Age;
}

public class ReportSetup<T>
{
string Title;
Func<Item, T> GroupBy;

public ReportSetup(title, groupBy)
{
Title = title;
GroupBy = groupBy;
}

static ReportSetup ByDate = new ReportSetup<DateTime>("By Date", x => x.Date);
static ReportSetup ByName = new ReportSetup<string>("By Name", x => x.Name);
static ReportSetup ByAge = new ReportSetup<int>("By Age", x => x.Age);

//In the UI, I want to be able to display a dropdown with the available report types
//How do I have a List of generic objects
static AllReports = new List<ReportSetup<object>>{ ByDate, ByName, ByAge };
}

public class Report<T>
{
string Title;
List<ReportRow<T>> Rows;
}

public class ReportRow<T>
{
T Key;
List<Item> Items;
}
public class Item
{
DateTime Date;
string Name;
int Age;
}

public class ReportSetup<T>
{
string Title;
Func<Item, T> GroupBy;

public ReportSetup(title, groupBy)
{
Title = title;
GroupBy = groupBy;
}

static ReportSetup ByDate = new ReportSetup<DateTime>("By Date", x => x.Date);
static ReportSetup ByName = new ReportSetup<string>("By Name", x => x.Name);
static ReportSetup ByAge = new ReportSetup<int>("By Age", x => x.Age);

//In the UI, I want to be able to display a dropdown with the available report types
//How do I have a List of generic objects
static AllReports = new List<ReportSetup<object>>{ ByDate, ByName, ByAge };
}

public class Report<T>
{
string Title;
List<ReportRow<T>> Rows;
}

public class ReportRow<T>
{
T Key;
List<Item> Items;
}
5 replies