Próbuję dowiedzieć się, jak prawidłowo przekazać właściwości za pośrednictwem wielu klas. Wiem, że mogę zaimplementować INotifyPropertyChanged
w każdej klasie i posłuchać zmian w obiekcie, ale wydaje się, że jest to dość niepotrzebny kod.Propagowanie zmian właściwości za pomocą wielu klas
Sytuacja:
Mam klasy (nazwijmy go Class1
) z dwoma właściwościami Zależność: FilterStatement
(string) i Filter
(klasa filtra). Ustawienie instrukcji wpływa na filtr i odwrotnie.
Logika konwersji między instrukcją a filtrem nie znajduje się jednak w Class1
, ale w Class3
- która Class1
nie zna bezpośrednio. Pomiędzy nimi jest Class2
, który musi przejść przez zmiany. (Możesz sobie wyobrazić, że klasy od 1 do 3 to Viewmodel, Model i Repozytorium, chociaż w rzeczywistości sytuacja ta nie pasuje całkowicie).
public class Class1
{
public static readonly DependencyProperty FilterProperty = DependencyProperty.Register(
"Filter",
typeof(Filter),
typeof(Class1),
new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty FilterStatementProperty = DependencyProperty.Register(
"FilterStatement",
typeof(String),
typeof(Class1),
new FrameworkPropertyMetadata(null));
public Filter Filter
{
get { return (Filter)GetValue(FilterProperty); }
set { SetValue(FilterProperty, value); }
}
public string FilterStatement
{
get { return (string)GetValue(FilterStatementProperty); }
set { SetValue(FilterStatementProperty, value); }
}
public Class2 MyClass2Instance { get; set; }
}
public class Class2
{
public Class3 MyClass3Instance { get; set; }
public void ChangeClass3Instance(object someParam) {
... // this can change the instance of MyClass3Instance and is called frome somewhere else
// when changed, the new Class3 instance has to get the property values of Class1
}
}
public class Class3
{
private Filter _filter; // here is where the filter set in Class 1 or determined by the statement set in class 1 has to be put
public string MyFilterToStatementConversionMemberFunction(Filter filter)
{
...
}
public Filter MyStatementToFilterConversionMemberFunction(string statement)
{
...
}
}
Mój naiwny rozwiązaniem byłoby powielić właściwości we wszystkich trzech klasach, wdrożyć INotifyPropertyChanged
w Class2
i Class3
i słuchać zmian, propagowanie wszystko w dół do Class3
a wynik z powrotem do Class1
. Czy nie ma lepszego rozwiązania?
Nie możesz użyć komunikatora takiego jak ten w świetle MVVM. Zasadniczo jest to zaprojektowane tak, aby można było powiadamiać o zmianach właściwości na maszynach wirtualnych, które nie muszą się wzajemnie znać. – Keithin8a
Obecnie nie używam tego, a ja chciałbym go użyć do tego celu ... ale tak, masz rację, to może być rozwiązanie –
Tak, dlatego pomyślałem, że zamieściłbym komentarz zamiast Odpowiedź. Wygląda na to, że mógłbyś pójść w drogę, nie robiąc tego, co trzeba, ponieważ uważasz, że jest lepszy sposób na zrobienie tego. Ale znasz swój projekt lepiej ode mnie, więc jestem pewien, że istnieje powód, po prostu bądź ostrożny;) – Keithin8a