C
C#5d ago
Tantrim

INotifyPropertyChanged

I have a List<ClassSpecModel> and I attach the property changed event like below in the image. I only have 1 "active" ClassSpecModel at a time that I make changes to. My question is, when I swap to a different ClassSpecModel in the list, and I attach the ClassSpecModel.PropertyChanged like shown in the image to the new ClassSpecModel, does the original ClassSpecModel.PropertyChanged become orphaned that I originally attached? Do I need to detach the ClassSpecModel.PropertyChanged in the original ClassModelObject Please let me know if this is clear or not, thanks.
No description
21 Replies
SpReeD
SpReeD5d ago
You don't attach or deattach, you subscribe or unsubscribe to an event, meaning it's a 1:n relation.
Tantrim
Tantrim5d ago
You are correct, sorry for my inaccurate vocabulary choice. Do I need to unsubscribe the original subscriber? If so, I'm strugglying to find the syntax
SpReeD
SpReeD5d ago
It depends on the rest of the code, generally speaking, "yes". However, I guess you're using the INotifyPropertyChanged in WPF/MAUI? - If so, you don't subscribe directly to the event, let the UI handle it by using bindings between View & ViewModel - also you don't wanna use List, but ObservableCollection or better BindingList since both implement the INotifyCollectionChanged interface.
Tantrim
Tantrim5d ago
from my understanding, ObservableCollection doesn't notify when an item inside of a list is modified only on add or remove type functions of new items into list
SpReeD
SpReeD5d ago
Idd, that's where BindingList joins the room, also it relays when its items has implemented the INotifyPropertyChanged interface.
Tantrim
Tantrim5d ago
and I'm using a library called Config.Net which stores settings locally in json files. I'm using INotifyPropertyChanged to keep the Config.Net class in sync with my actual data model that I manipulate I tried using the Observer pattern but that couldn't see updates from within the lists. I need to look into BindingList maybe
SpReeD
SpReeD5d ago
and I'm using a library called Config.Net Doesn't matter, that's BusinessLogic part, let the ViewModel contact what's behind This answer explains BindingList it in short terms: https://stackoverflow.com/questions/4284663/difference-between-observablecollection-and-bindinglist
Tantrim
Tantrim5d ago
this is a .Net Maui Blazor Hybrid app that I'm only targetting windows with
SpReeD
SpReeD5d ago
Sounds strange, but the config is for your MAUI app?
Tantrim
Tantrim5d ago
I'm using JSON as local data storage. I'm using Config.Net to just make it easier to manipulate the JSON. It's a small application with about 156kb of data spread between around 40 JSON files I didn't want to use a database. I'm not an expert in this by any means but it seemed logical to me.
SpReeD
SpReeD5d ago
40 json files? - why not use a DB? Serializing is fine for user-settings etc. but when it comes to data storage you wanna use an actual db.
Tantrim
Tantrim5d ago
maybe SQLite would have been best? I would prefer something very portable without needing a db installed on the machine
SpReeD
SpReeD5d ago
SQLite is the way to go, it's a file-based SQL database, no need for a server, can be used with all common ORM like Dapper and EntityFramework.
Tantrim
Tantrim5d ago
individual JSON files will potentially be shared between users as well. The JSON read/write overhead isn't too much of a concern for this app. But I do understand storing data in JSON is far from best practice
SpReeD
SpReeD5d ago
You may import/export (exchange) JSON objects between users by using Base64 (or Base85) coding to improve sharability and performance.
Tantrim
Tantrim5d ago
you definitely have me questioning my choice of JSON storage lol. I think I will go ahead with storing in JSON but I might refactor to using SQLite later if I get a wild hair.
Tantrim
Tantrim4d ago
if I subscribe to an event using an object of a model that I'm setting like in the image below. Do I need to unsubscribe when I subscribe again with a new model that overwrites the variable of the last one? I call the SubscribeToModel every time the working object model changes. I only work on 1 object at a time. I understand how variables of classes are just references correct? This makes me think I SHOULD unsubscribe. However, it doesn't seem to fire the event when I overwrite the variable as shown in the image and the modify the original object directly in the list without modifying it using the lassData.CurrentClassSpecSelectedModel variable.
No description
SpReeD
SpReeD4d ago
variables of classes There's nothing like it, you have either Properties or Fields, the latter are the actual storage, while a Property is like a linked gate to a shadow-field/backing-field behind it. Going further, this is the so called state of the instance of the object, which changes throughout runtime. While static is a non-instance object, so every method or field has only one instance, like a singleton pattern styled class. I wrote this, because I see you have plenty of static, if not only, methods. Can you provide more $code ?
MODiX
MODiX4d ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Tantrim
Tantrim4d ago
thank you for the vocabulary update. the field ClassData.CurrentClassSpecSelectedModel is static. That probably explains the SubscribeToOverrideModels is the same pattern as SubscribeToSpellModel so I don't think it needs to be shared. Also I'm not subscribing to events with ClassData.CurrentClassSpecSelectedSetting so it's mostly irrelevant I think
No description
No description
No description
Tantrim
Tantrim4d ago
No description