CommunityToolkit / CommunityToolkit/dotnet

ObservableProperty should support lazy initialization.

Aperta
#136 3 commenti 1 reazione 0 assegnatari Vedi su GitHub
feature request :mailbox_with_mail:
Lingua principale
C#
Stelle
3.8k
Fork
400
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### Describe the problem

**ObservableProperty** right now doesnt provide a way to initialize property or **lazy initialize the properties**. So in case where we need that we have to manually do that in the constructor.

**Example**

Following is a property (along with backing field) with traditional mvvm way and I am including lazy initialization in it so that this object will never be null when app wants to use it.

```
private DropDownsVM _dropDowns;
public DropDownsVM DropDowns { get => _dropDowns ??= new DropDownsVM(); set => SetProperty(ref _dropDowns, value); }

```
Now when I wanna use **ObservableProperty** annotation from **Microsoft.Toolkit.Mvvm.ComponentModel** I do the following way and have to add initialization in constructor.

```
public AppViewModel()
{
DropDowns = new DropDownsVM();
}
[ObservableProperty]
private DropDownsVM dropDowns;
```

### Describe the solution

> Ideally there should be a way to indicate that property should autogenerate the lazy initialization or atleast normal initialization.

**Maybe Something like this**

```
[ObservableProperty(new(), true)]
private DropDownsVM dropDowns;
```

```
[ObservableProperty(new(123), true)]
private DropDownsVM dropDowns;
```

> in the first example **[ObservableProperty(new(), true)]** takes 2 parameters, first one being the automatic constructor of the object using latest c# language so we dnt have to write **new DropDownsVM()** fully, the 2nd parameter is a boolean indicating whether **LazyInitialization=true**

> 2nd example is same as first, only in this case we are providing a different constructor for the object and that constructor takes a parameter int, which is in this case **123** this lets us do all kind of initializations of the property right within the annotation and will reduce code a lot I think.

This could also work for other kinds of nonNullable objects i.e : providing a default value for a boolean or string etc.

### Alternatives

_No response_

### Additional info

**Package version** : Microsoft.Toolkit.Mvvm (7.1.2)

### Help us help you

_No response_

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia individuando l’annotazione ObservableProperty e i relativi test di generazione del codice sorgente; il payload non identifica file specifici. Chiarisci la sintassi supportata per l’inizializzazione e l’inizializzazione lazy, nonché il relativo comportamento per i tipi di riferimento e i valori predefiniti primitivi, quindi definisci il completamento tramite test del codice generato che coprano i casi proposti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.