DeepExtensions 0.4.0
See the version list below for details.
dotnet add package DeepExtensions --version 0.4.0
NuGet\Install-Package DeepExtensions -Version 0.4.0
<PackageReference Include="DeepExtensions" Version="0.4.0" />
<PackageVersion Include="DeepExtensions" Version="0.4.0" />
<PackageReference Include="DeepExtensions" />
paket add DeepExtensions --version 0.4.0
#r "nuget: DeepExtensions, 0.4.0"
#:package DeepExtensions@0.4.0
#addin nuget:?package=DeepExtensions&version=0.4.0
#tool nuget:?package=DeepExtensions&version=0.4.0
Deep Extensions
The Deep Extensions are a collection of three Deep features based on Source Generation.
Features
- Roslyn Code Generator
- AOT-compatible
- Trimmable
- CommunityToolkit.Mvvm compatible.
- WPF, WinUI, and Avalonia compatible.
Installation
DeepCopy
Generates code for deep copy.
An opt-out / automatic matching approach is used. This means that all properties not marked with [DeepCopyIgnore] will be copied.
Attributes
[DeepCopy]
The [DeepCopy] attribute only needs to be placed on the root element; it signals that a deep copy should be generated recursively for this class, all its base classes, and all its property classes.
[DeepCopyIgnore]
All properties with [DeepCopyIgnore] will be ignored.
Example
using DeepExtensions;
namespace Demo.Models;
[DeepCopy]
public partial class DemoModel : BaseModel
{
public string Name { get; set; } = "Peter";
public SubModel? Sub { get; set; } = new SubModel();
[DeepCopyIgnore]
public int Num { get; set; } = 45;
}
public partial class BaseModel
{
public int Id { get; set; } = 1;
}
// Generated by DeepExtensions
partial class DemoModel : IDeepCopy<DemoModel>
{
public new DemoModel DeepCopy()
{
var copy = new DemoModel();
DeepCopyTo(copy);
return copy;
}
protected void DeepCopyTo(DemoModel target)
{
base.DeepCopyTo(target);
target.Name = this.Name;
target.Sub = this.Sub?.DeepCopy();
}
}
partial class BaseModel : IDeepCopy<BaseModel>
{
public BaseModel DeepCopy()
{
var copy = new BaseModel();
DeepCopyTo(copy);
return copy;
}
protected void DeepCopyTo(BaseModel target)
{
target.Id = this.Id;
}
}
DeepEqual
Generates code for deep equal.
An opt-out / automatic matching approach is used. This means that all properties not marked with [DeepEqualIgnore] will be compared.
[DeepEqual]
The [DeepEqual] attribute only needs to be placed on the root element; it signals that a deep equal should be generated recursively for this class, all its base classes, and all its property classes.
[DeepEqualIgnore]
All properties with [DeepEqualIgnore] will be ignored.
Example
using DeepExtensions;
namespace Demo.Models;
[DeepEqual]
public partial class DemoModel
{
public string Name { get; set; } = "Peter";
public SubModel Sub { get; set; } = new SubModel();
[DeepEqualIgnore]
public int Num { get; set; } = 45;
}
// Generated
partial class DemoModel : IDeepEqual<DemoModel>
{
public bool DeepEqual(DemoModel? value)
{
if (value is null) return false;
return
Name == value.Name &&
((Sub is null && value.Sub is null) || (Sub is not null && Sub.DeepEqual(value.Sub)));
}
}
DeepMap
Mapping between Model and ViewModel trees
- Custom converters
In contrast to DeepCopy and DeepEqual, this approach uses explicit opt-in. This means that all properties intended for mapping must be decorated with [DeepMapProperty].
Examples
Simple Types
Synchronizing properties with simple data types between the Model and the ViewModel.
Model
using DeepExtensions;
namespace Demo.Models;
public class DemoModel
{
public string Name { get; set; } = "Peter";
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(DemoModel))]
public partial class DemoViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty]
public partial string Name { get; set; }
}
List of Simple Types
Synchronizing properties with a list of simple data types between the Model and the ViewModel.
Model
namespace Demo.Models;
public class DemoModel
{
public List<string> Names { get; set; } = [ "Peter", "Paul", "Mary"];
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(DemoModel))]
public partial class DemoViewModel : ObservableObject
{
// no partial, no set, no nullable, and no [ObservableProperty]
[DeepMapProperty]
public ObservableCollection<string> Names { get; }
}
Different Names
Synchronizing properties with different names between Model and ViewModel.
Model
namespace Demo.Models;
public class DemoModel
{
public string Name { get; set; } = "Peter";
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(DemoModel))]
public partial class DemoViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty(nameof(DemoModel.Name))]
public partial string FirstName { get; set; }
}
Children
Synchronizing properties with children between Model and ViewModel.
Model
namespace Demo.Models;
public class RootModel
{
public LeafModel? Leaf { get; set; } = new LeafModel();
}
public class LeafModel
{
public string Name { get; set; } = "Peter";
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(RootModel))]
public partial class RootViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty]
public partial LeafViewModel? Leaf { get; set; }
}
[DeepMap(typeof(LeafModel))]
public partial class LeafViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty]
public partial string Name { get; set; }
}
List of Children
Synchronizing properties with a list of children between Model and ViewModel.
Model
namespace Demo.Models;
public class RootModel
{
public List<LeafModel> Leaves { get; set; } = [new LeafModel(), new LeafModel()];
}
public class LeafModel
{
public string Name { get; set; } = "Peter";
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(RootModel))]
public partial class RootViewModel : ObservableObject
{
// no partial, no set, no nullable, and no [ObservableProperty]
[DeepMapProperty]
public ObservableCollection<LeafViewModel> Leaves { get; }
}
[DeepMap(typeof(LeafModel))]
public partial class LeafViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty]
public partial string Name { get; set; }
}
Converter Functions
Synchronizing properties with converters between the Model and the ViewModel. The converters can also be applied to lists and "list to simpleType" mappings.
Model
namespace Demo.Models;
public class DemoModel
{
public int Num { get; set; } = 25;
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(DemoModel))]
public partial class DemoViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty(Converter = "NumConvert")]
public partial string Num { get; set; }
private string GetNumConvert(int val) => val.ToString();
private int SetNumConvert(string val) => int.Parse(val);
}
Converter Interface
Synchronizing properties with converters between the Model and the ViewModel. The converters can also be applied to lists and "list to simpleType" mappings.
Model
namespace Demo.Models;
public class DemoModel
{
public int Num { get; set; } = 25;
}
ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using DeepExtensions;
namespace Demo.ViewModels;
[DeepMap(typeof(DemoModel))]
public partial class DemoViewModel : ObservableObject
{
[ObservableProperty]
[DeepMapProperty(ConverterType = typeof(IntToStringConverter))]
public partial string Num { get; set; }
public class IntToStringConverter : IDeepMapPropertyConverter
{
public object Convert(object value) => value?.ToString()!;
public object ConvertBack(object value) => (value is string input && int.TryParse(input, out int result)) ? result : null!;
}
}
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Creation