https://github.com/wckywckf/wckywckf.model2viewmodel
这是一个源生成器,旨在帮助你在MVVM设计模式中,将复杂且庞大的Model配置类生成一个ViewModel版本。
https://github.com/wckywckf/wckywckf.model2viewmodel
avaloniaui mvvm prism reactiveui
Last synced: 3 days ago
JSON representation
这是一个源生成器,旨在帮助你在MVVM设计模式中,将复杂且庞大的Model配置类生成一个ViewModel版本。
- Host: GitHub
- URL: https://github.com/wckywckf/wckywckf.model2viewmodel
- Owner: WCKYWCKF
- License: mit
- Created: 2025-03-19T14:55:52.000Z (about 1 month ago)
- Default Branch: master
- Last Pushed: 2025-04-15T13:56:31.000Z (6 days ago)
- Last Synced: 2025-04-15T14:46:03.305Z (6 days ago)
- Topics: avaloniaui, mvvm, prism, reactiveui
- Language: C#
- Homepage:
- Size: 32 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: License
Awesome Lists containing this project
README
# WCKYWCKF.Model2ViewModel
## 简介
这是一个源生成器,旨在帮助你在MVVM设计模式中,将复杂且庞大的`Model`配置类生成一个`ViewModel`版本。
使用它,你可以节省大量时间,不再需要通过复制粘贴的方式手动创建`ViewModel`,从而有更多时间专注于代码的设计与优化。
此项目处于预览阶段,可能还存在一些细微的BUG,但它已能够完成绝大多数工作。
欢迎你的PR,期待你的Star。
项目使用MIT协议。
## 提供的Nuget包
* WCKYWCKF.Model2ViewModel
## 功能
### GenerateViewModel
此装饰器能够为指定的`Model`生成一个对应的`ViewModel`版本,并且会一并处理`Model`中嵌套的`Model`部分。
生成的`ViewModel`支持三种模式:ReactiveUI、CommunityToolkit和INotifyCollectionChanged。它们通过被装饰器装饰的类的基类决定。如果模式是CommunityToolkit的话也会帮你生成CommunityToolkit样式的`ViewModel`。如果你想应用你希望使用CommunityToolkit的值验证的话你需要使用`GenerateViewModelIgnore`来告诉源生成器不要生成这个属性,然后自己通过分部类实现它。
#### 注意
* 每个生成的ViewModel名称都会带有SGVM后缀,以便于区分和识别。
* 若`Model`中属性成员的类型为`enum`,或者其类型所属的命名空间以`System`开头,则这些类型的属性在生成的ViewModel中将保持原样,不会进行修改或转换。
* 生成的`ViewModel`的命名空间将与被`GenerateViewModel`装饰器修饰的类的命名空间保持一致,这样可以更好地组织和管理代码结构。
* 所有生成的`ViewModel`的修饰符都是`public partial class`,当你有特殊需要时可以通过分部类进行处理。
* 所有实现了`IReadOnlyCollection`和`ICollection`接口但没有实现`INotifyCollectionChanged`接口的类型都将被替换为`ObservableCollection`。### GenerateViewModelIgnore
此装饰器必须与`GenerateViewModel`一起使用,否则将无效。它能够指定某一类型不需要生成`ViewModel`,也能指定不生成某一类型的某一属性。
* 此装饰器对命名空间以`System`开头的类型无效。
### GenerateViewModelReplace
此装饰器必须与`GenerateViewModel`一起使用,否则将无效。它能将指定类型的某一属性的类型替换为其他类型。
* 如果某一属性应用了此装饰器将不会再应用可观察集合替换。
### 使用
建议单独创建一个类型来处理`Model`的转换工作,所有装饰器都应放在这个类上,这能够有效地避免冲突问题,也便于管理。
现在以CommunityToolkit模式为例。以下是使用示例。
```csharp
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client.ClientCapabilities;
using ReactiveUI;
using WCKYWCKF.Model2ViewModel;namespace WCKYWCKF.Model2ViewModel.Sample;
public partial class ComplexModel
{
public List>? NestedCollection { get; set; }
public DDS? DDS { get; init; }
public IEnumerable Dates { get; }
}public class DDS
{
public string? Str { get; init; }
public string? Str2 { get; set; }
public List IntList { get; init; }
}[GenerateViewModel(ModelType = typeof(ComplexModel))]
[GenerateViewModel(ModelType = typeof(ClientCapabilities))]
[GenerateViewModelIgnore(ModelType = typeof(DDS), PropertyName = nameof(DDS.Str2))]
[GenerateViewModelReplace(ModelType = typeof(ComplexModel), PropertyName = nameof(ComplexModel.Dates), ReplaceWithType = typeof(List))]
public class TF : ObservableObject;
```以下是生成的代码
ComplexModelSGVM
```csharp
//
#nullable enableusing CommunityToolkit.Mvvm.ComponentModel;
namespace WCKYWCKF.Model2ViewModel.Sample;
public partial class ComplexModelSGVM : global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject {
private System.Collections.ObjectModel.ObservableCollection>? _nestedCollection = new();
public System.Collections.ObjectModel.ObservableCollection>? NestedCollection {
get => _nestedCollection;
set {
if (!global::System.Collections.Generic.EqualityComparer>?>.Default.Equals(_nestedCollection, value))
{
var t = _nestedCollection;
OnNestedCollectionChanging(value);
OnNestedCollectionChanging(t, value);
OnPropertyChanging();
_nestedCollection = value;
OnNestedCollectionChanged(value);
OnNestedCollectionChanged(t, value);
OnPropertyChanged();
}
}
}private WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? _dDS;
public WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? DDS {
get => _dDS;
set {
if (!global::System.Collections.Generic.EqualityComparer.Default.Equals(_dDS, value))
{
var t = _dDS;
OnDDSChanging(value);
OnDDSChanging(t, value);
OnPropertyChanging();
_dDS = value;
OnDDSChanged(value);
OnDDSChanged(t, value);
OnPropertyChanged();
}
}
}private System.Collections.Generic.List? _dates;
public System.Collections.Generic.List? Dates {
get => _dates;
set {
if (!global::System.Collections.Generic.EqualityComparer?>.Default.Equals(_dates, value))
{
var t = _dates;
OnDatesChanging(value);
OnDatesChanging(t, value);
OnPropertyChanging();
_dates = value;
OnDatesChanged(value);
OnDatesChanged(t, value);
OnPropertyChanged();
}
}
}partial void OnNestedCollectionChanging(System.Collections.ObjectModel.ObservableCollection>? value);
partial void OnNestedCollectionChanging(System.Collections.ObjectModel.ObservableCollection>? oldValue, System.Collections.ObjectModel.ObservableCollection>? newValue);
partial void OnNestedCollectionChanged(System.Collections.ObjectModel.ObservableCollection>? value);
partial void OnNestedCollectionChanged(System.Collections.ObjectModel.ObservableCollection>? oldValue, System.Collections.ObjectModel.ObservableCollection>? newValue);partial void OnDDSChanging(WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? value);
partial void OnDDSChanging(WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? oldValue, WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? newValue);
partial void OnDDSChanged(WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? value);
partial void OnDDSChanged(WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? oldValue, WCKYWCKF.Model2ViewModel.Sample.DDSSGVM? newValue);partial void OnDatesChanging(System.Collections.Generic.List? value);
partial void OnDatesChanging(System.Collections.Generic.List? oldValue, System.Collections.Generic.List? newValue);
partial void OnDatesChanged(System.Collections.Generic.List? value);
partial void OnDatesChanged(System.Collections.Generic.List? oldValue, System.Collections.Generic.List? newValue);}
#nullable disable
```DDSSGVM
```csharp
//
#nullable enableusing CommunityToolkit.Mvvm.ComponentModel;
namespace WCKYWCKF.Model2ViewModel.Sample;
public partial class DDSSGVM : global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject {
private System.String? _str;
public System.String? Str {
get => _str;
set {
if (!global::System.Collections.Generic.EqualityComparer.Default.Equals(_str, value))
{
var t = _str;
OnStrChanging(value);
OnStrChanging(t, value);
OnPropertyChanging();
_str = value;
OnStrChanged(value);
OnStrChanged(t, value);
OnPropertyChanged();
}
}
}private System.String? _str2;
public System.String? Str2 {
get => _str2;
set {
if (!global::System.Collections.Generic.EqualityComparer.Default.Equals(_str2, value))
{
var t = _str2;
OnStr2Changing(value);
OnStr2Changing(t, value);
OnPropertyChanging();
_str2 = value;
OnStr2Changed(value);
OnStr2Changed(t, value);
OnPropertyChanged();
}
}
}private System.Collections.ObjectModel.ObservableCollection? _intList = new();
public System.Collections.ObjectModel.ObservableCollection? IntList {
get => _intList;
set {
if (!global::System.Collections.Generic.EqualityComparer?>.Default.Equals(_intList, value))
{
var t = _intList;
OnIntListChanging(value);
OnIntListChanging(t, value);
OnPropertyChanging();
_intList = value;
OnIntListChanged(value);
OnIntListChanged(t, value);
OnPropertyChanged();
}
}
}partial void OnStrChanging(System.String? value);
partial void OnStrChanging(System.String? oldValue, System.String? newValue);
partial void OnStrChanged(System.String? value);
partial void OnStrChanged(System.String? oldValue, System.String? newValue);partial void OnStr2Changing(System.String? value);
partial void OnStr2Changing(System.String? oldValue, System.String? newValue);
partial void OnStr2Changed(System.String? value);
partial void OnStr2Changed(System.String? oldValue, System.String? newValue);partial void OnIntListChanging(System.Collections.ObjectModel.ObservableCollection? value);
partial void OnIntListChanging(System.Collections.ObjectModel.ObservableCollection? oldValue, System.Collections.ObjectModel.ObservableCollection? newValue);
partial void OnIntListChanged(System.Collections.ObjectModel.ObservableCollection? value);
partial void OnIntListChanged(System.Collections.ObjectModel.ObservableCollection? oldValue, System.Collections.ObjectModel.ObservableCollection? newValue);}
#nullable disable
```