PropertyBind 0.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package PropertyBind --version 0.1.2                
NuGet\Install-Package PropertyBind -Version 0.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PropertyBind" Version="0.1.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PropertyBind --version 0.1.2                
#r "nuget: PropertyBind, 0.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PropertyBind as a Cake Addin
#addin nuget:?package=PropertyBind&version=0.1.2

// Install PropertyBind as a Cake Tool
#tool nuget:?package=PropertyBind&version=0.1.2                

PropertyBind

GitHub GitHub code size in bytes Github Last commit
SqModel SqModel

Property synchronization process using Source Generator.

Demo

Assume that the Blog class has a Post class collection, and the Post class has a Blog property.

In this case, you need to write code like the following to synchronize the Blog class without any conflicts.

using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class Blog
{
	public ObservableCollection<Post> Posts { get; } = new();

	public Blog()
	{
		Posts.CollectionChanged += Posts_CollectionChanged;
	}

	private void Posts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
	{
		if (e.Action == NotifyCollectionChangedAction.Add)
		{
			if (e.NewItems == null) return;
			foreach (Post item in e.NewItems)
			{
				item.Blog = this;
			}
		}
	}
}

public class Post
{
	public Blog Blog { get; set; } = null!;
}

The above code is very tedious. Therefore, by using the SourceGenerator PropertyBind, you can write code with the same meaning as above using attributes.

using System.Collections.ObjectModel;

[GeneratePropertyBind(nameof(Posts), nameof(Post.Blog))]
public partial class Blog
{
	public ObservableCollection<Post> Posts { get; } = new();
}

public class Post
{
	public Blog Blog { get; set; } = null!;
}

Specify the property name of the collection in the first argument.

In the second argument, specify the property name you want to associate with itself.

Note: auto-generated code

GeneratePropertyBindAttribute.cs

namespace PropertyBind
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    internal sealed class GeneratePropertyBindAttribute : Attribute
    {
        public string ObservableCollectionPropertyName { get; } 
        public string BindPropertyName { get; } 
        public GeneratePropertyBindAttribute(string observableCollectionPropertyName, string bindPropertyName)
        {
            this.ObservableCollectionPropertyName = observableCollectionPropertyName;
			this.BindPropertyName = bindPropertyName;
        }
    }
}

Blog.g.cs

using System.Collections.Specialized;

public partial class Blog
{
	public Blog()
	{
		Posts.CollectionChanged += Posts_CollectionChanged;
	}

	private void Posts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
	{
		if (e.Action == NotifyCollectionChangedAction.Add)
		{
			if (e.NewItems == null) return;
			foreach (Post item in e.NewItems)
			{
				item.Blog = this;
			}
		}
	}
}
There are no supported framework assets in this package.

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.

Version Downloads Last updated
0.2.0.1 261 12/30/2023
0.1.4 150 12/27/2023
0.1.3.1 186 12/27/2023
0.1.2 136 12/26/2023
0.1.1 179 12/26/2023
0.1.0 116 12/26/2023