52 Replies
Here's what I'm supposed to do but I'm just currently making a DB first
I don't think Friends should be nullable (or settable) here. I would do a get-only autoproperty for both, initialized with
[]
FetchPerson
looks fine.
AddPerson
as well... although I'd question if you want name
and age
to be nullable.My Logic was to use ID as a primary key and those 2 being nullable wouldn't matter
UpdatePerson
is fine, I might force a ternary, but I'm silly like that.I'm kind of lost about updateperson
Cause it says to use a both PUT and PATCH endpoint
well, all the more reason to have them be not nullable.
I understand one is complete and the other one is partial but not sure how to fix it without adding an additional URL
I'll change the Person class too then
Is it fine to add a json serializer and deseralizer to store the DB in it
mmm, web side of C# is not my strong suit, but from the perspective of this in memory DB you can have perhaps a special "update person" type, with nullable fields, and only update the person fields that are not null.
What about adding discards
I'm not sure what you mean by that.
uhh
_ = IReturnSomeThingThatYouDontCareAbout()
?
like Remove
maybe?
But then name can't be null
:sadge:
ah, no, that won't work.
Well can a persons name logically be null? Seems like it shouldn't be.
If this is stemming from the demands of
Update
just have a special UpdatePerson
type for that case.
On the whole it looks good. Removing that nullability I think will help you tighten up your logic a bit, but its pretty fine as is.
Anyways Update could be like...
Assuming Person
is a record
It's not that a record but that's better
Makes way more sense
I'll make an UpdatePersonPatch and Put methods
Should I just change Person to record @,ax
If you can, yeah.
I don't think this is fine
Id should be init
I stole your method I apologise
I don't even know how to change it since it's the best option
no need to apoolgize, that's why I wrote it.
I changed Person to record
set Id as Init
since it should be immutable
Although if I take into consideration what happens once we remove it maybe the IDs should change in value
Person
can probaby be a one-liner:
no need for a ToString
or set
. Record
already has a nice ToString
and when you use the with
syntax, there is no need for set (technically you are creating a new object).
and since there is then no method that changes a Person
object, it can be completely immutable.I forgot it did this
I thought it was the opposite
Although, if I use a constructor which isn't empty it gives me an error for the Add method
What about update person
Oh
With
Nevermind
yeah you can't use the object initializer syntax like that anymore. But you can just do a normal
new
So with is basically reconstructing
Is there a reason why
It's kind of silly in my opinion
Also, when should I use structs I never used one
Classes -> pretty much anything
Records -> immutable objects
Structs -> ?
Well, preferably immutable*
And they also don't compare by reference but by value
structs are useful for small objects, like say:
though that could be a
record struct
as well now.
but when in doubt, prefer a class.
oh also, no need to check if the id is present in Remove
it doesn't throw if the key is not present so you can just do...
Just like Add
Also, was the LINQ fine
GetFriends
looking at that now
I may have gone a little overboard, feel free to ask if you have any questions:
https://sharplab.io/#gist:ddee989aca0b9673a960b823b5dbab13
SharpLab
C#/VB/F# compiler playground.
your GetFriend method was probably fine, but you can do better than itterating the entire people table for matches.
Am I correct in understanding that friends was a list of people IDs, indexed by a another people ID?
and I used a hashset for that since the friend IDs should be unique.
I added a AddFriend method
and also update the AddPeople method to add a blank friends list for that person when yoou add that person
hmm... probably you should remove that entry from the friends list when you remove the person as well.
hmm... and also remove the entry in every instance of the friends list. Cascading updates are a pita!
might be better than to more closely mirror how it would be in a DB and have the Friends list be a hashset of
(PersonId, PersonId)
Yes
Did you use a HashSet cause it's better for iteraing thru it?
also, wouldn't the AddFriend thing need to be added back to the HashSet
I'm out of control, I have to stop here:
https://sharplab.io/#gist:34a5e789cfb6ab7f194111be4f42743d
SharpLab
C#/VB/F# compiler playground.
I used a HashSet because it best maps to the problem domain.
A unique set of values is a HashSet.
Could you explain this a bit if it's not an issue
ohhh
it jsut returns the people
happy to explain anything.
okay yeah you don't need to
It's just an iteration of the HashSet which gives us ToString of each friend
what does yield do though
But yeah, that's a fancy way to get an
IEnumerable
out of a function.
well... yield
is kind of magic but basically it turns the method into a method that returns an IEnumerable
you could just build up a list and return that the normal way if you like instead
like each element that is yield return
ed because an element in the enumeration.I feel bad now cause I legit took your code
isn't this illegal
I mean I made it for you.
But what is important is that you understand it.
I mean I do but it'd be kind of hard for me to write it myself in the near future
I find looking at solutions to problems to be inspirational. So I tend to give out a lot of implementations.
Sorry if I went to far here.
No no it's fine
I understood the majority and I also understood what it improved
I don't know how familiar you are with DBs, but the thing with using a hashset of
Friendship
objects makes more sense if you are used to lookup tables of that sort.I've only done SQL
But like SQL in Apex
likewise, removing the id from the Person class, and using an auto-incrementing value instead.
I'm gonna do this part myself, including the POST, GET, PUT endpoints
I wanted to keep it simple for now
Just try stuff out, see how it works
good because I don't know as much about that side of things.
anyways in SQL terms the DB would be something like:
It works
I basically did this without even looking at anything I'm proud of myself
I need to make one for friends which is slightly harder
Well it isn't hard, it's just bothersome to write friends
@maxmahem should I move to the next lecture
those marked with 0 are easy to do
you jsut need to make endpoints for them
thanks a lot for the help too
hah, well if you look at the code it already did some of thosoe.
It basically did everything
And the stuff it didn't do could be done by calling the methods you've made