Clint
Clint
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
You could also make it not use object and use JsonElement for example, but then System.Text.JSON contains primitives for making this slightly more workable like the JSON writer / reader utilities etc. Lots of ways to potentially solve this. Another of course would be to make your viewmodels derive from a persistable base, something like:
public abtract class PersistableViewModel<TStorage> {
abstract void Load(TStorage data);
abstract TStorage Persist();
}
public abtract class PersistableViewModel<TStorage> {
abstract void Load(TStorage data);
abstract TStorage Persist();
}
And then implementing it would be something like:
public sealed record TextBoxItemData(CanvasPosition Position, string StringValue);

public sealed class TextBoxViewModel : PersistableViewModel<TextBoxItemData> {
public override TextBoxItemData Persist() {
return new(this.Position, this.TextValue);
}

public override void Load(TextBoxItemData data) {
this.Position = data.CanvasPosition;
this.StringValue = data.StringValue;
}
}
public sealed record TextBoxItemData(CanvasPosition Position, string StringValue);

public sealed class TextBoxViewModel : PersistableViewModel<TextBoxItemData> {
public override TextBoxItemData Persist() {
return new(this.Position, this.TextValue);
}

public override void Load(TextBoxItemData data) {
this.Position = data.CanvasPosition;
this.StringValue = data.StringValue;
}
}
Then you have a well-defined data model for your persistance, that you can then ser/deser however you want, and is easy enough to just treat wholesale for your JSON storage. Your viewmodels can then be as complex as needed, and all you're worried about is upholding that in/out data contract, and the persistance code can take care of the rest.
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Hmmmm, my recommendation would be instead to implement an interface something like
public interface IPersistable {
Dictionary<string, object> Persist();
void Load(Dictionary<string, object>> props);
}
public interface IPersistable {
Dictionary<string, object> Persist();
void Load(Dictionary<string, object>> props);
}
Then have your items implement it, this is sort of similar to how certain things in Apple's ecosystem work, so the idea being your individual types can pack and unpack their specific pieces of data, and you have a consistent interface to interact with that mechanism without having to resort to reflection. Also means you're not necessarily tied into JSON going forward either.
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Yeah sure, not sure how rapidly I'll be able to answer but yeah, drop a message
18 replies
CC#
Created by Saiyanslayer on 10/19/2024 in #help
How to find and update a record in a recursive model?
Hmmmmm, unfortunately doesn't really give me the info I'd need to understand your precise challenge with this here, anything I'd be likely to suggest would be too subjective.
16 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
So let's say you have these items that users can drag around on a canvas, your ViewModels for each item will be unique to their own usecases, but they all share something in common, that is the fact they belong on the canvas and store positional data, so you could have your base view model type:
public sealed record Position(int X, int Y);
public sealed record Position(int X, int Y);
public abstract class CanvasItemViewModel {
public string Id { get; set; }
public Position Position { get; set; }
}
public abstract class CanvasItemViewModel {
public string Id { get; set; }
public Position Position { get; set; }
}
Omitted the INotifyPropertyChanged stuff for brevity. Then let's say you have a VM to represent your canvas container itself:
public class CanvasViewModel {
public List<CanvasItemViewModel> Items { get; set; }

public ImmutableDictionary<string, Position> SnapshotPositions() {
return Items.ToImmutableDictionary(x => x.Id, x => x.Position);
}
}
public class CanvasViewModel {
public List<CanvasItemViewModel> Items { get; set; }

public ImmutableDictionary<string, Position> SnapshotPositions() {
return Items.ToImmutableDictionary(x => x.Id, x => x.Position);
}
}
If you have additional serialisation / deserialisation needs per-item, e.g. having them store and load additional data relevant to them, then you'd want to come up with a more comprehensive mechanism for storing their own data, but this would be how I'd start to tackle the problem.
18 replies
CC#
Created by Saiyanslayer on 10/19/2024 in #help
How to find and update a record in a recursive model?
What is it you're actually trying to do here? The code isn't super clear about what the problem you're facing is. My recommendation would be to rely on XDocument as you get all the tree traversal built-in via Descendants .
16 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
I'd recommend setting up a base view model type that these items' view models can derive from, placing their positioning properties on that base type, and that way you don't have to worry about putting them on every class. You can then pretty easily have your collection where all those items are stored in a VM just be a collection of that base type, and implement your persist / reload logic at that level.
18 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
public string AskUser(string message, ? validator, string failureMessage) {
var value = Console.ReadLine(message);
if (!validator(value)) {
Console.WriteLine(failureMessage);
}

// code to go around while we don't have a valid value not included
}
public string AskUser(string message, ? validator, string failureMessage) {
var value = Console.ReadLine(message);
if (!validator(value)) {
Console.WriteLine(failureMessage);
}

// code to go around while we don't have a valid value not included
}
That's more like what we're aiming for, yes? Where ? is the delegate to perform the validation check.
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
Oh okay, I see the confusion here, your ask methods are returning string but we're getting tripped up on the validation part
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
so close
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
public bool ValidateLastName(string value) {
// return some validation pass / fail result
}
public bool ValidateLastName(string value) {
// return some validation pass / fail result
}
Take the types of the return value and the arguments, what do you think the delegate signature looks like for that?
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
And if it helps, think of:
delegate ? ValidatorDelegate(? input);
delegate ? ValidatorDelegate(? input);
Like the way you'd write a method signature, just with an extra keyword at the start, and NO implementation body.
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
What if take a step back? Merineth what problem is you're trying to solve? Perhaps we can tackle from first principles and slot the delegates in gradually?
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
And they show up all over the place in the core library
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
Pobiega is right, multitude of uses
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
But my offer still stands, we will have you understanding how delegates work cos they're an extremely useful tool!
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
Ah I wasn't aware, apologies.
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
No worries if not though, no problem.
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
Just letting you know that I'm available if you want options and alternative ways of going through this, I think it's great you're looking to learn this, but appreciate that a busy chat window isn't always the best forum for it 🙂
322 replies
CC#
Created by Merineth 🇸🇪 on 10/11/2024 in #help
✅ Can someone explain Delegates to me like i'm 5?
Gives some code to try and then watch what it gets up to, hard to explain the concept without involving the type signatures of what's going on.
322 replies