MVVM Light Toolkit VM 用の Mvvm Property の T4 テンプレート を公開します。
たったこれだけコーディングするだけで Mvvm Property が出来上がります。
もうめんどくさいとか実装が大変だとか言わないでください。
MainViewModel.tt ソース
<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ output extension=".properties.cs" #>
<#@ include file="../Template/MvvmClassInfo.ttinclude" #>
<#
var classinfo = new MvvmClassInfo(
"T4Mvvm.ViewModel", "MainViewModel", new MvvmPropertyInfo[] {
new MvvmPropertyInfo("id","ID", typeof(int), ""),
new MvvmPropertyInfo("name","Name", typeof(string), "[Required()]")
});
#>
<#@ include file="../Template/MvvmClassBody.ttinclude" #>
プロジェクトの構成はこんな感じです。
MvvmClassInfo.ttinclude ソース
<#+
class MvvmClassInfo
{
public string NameSpaceName;
public string ClassName;
public MvvmPropertyInfo[] Property;
public MvvmClassInfo(string nameSpaceName, string className, MvvmPropertyInfo[] property)
{
NameSpaceName = nameSpaceName;
ClassName = className;
Property = property;
}
}
class MvvmPropertyInfo
{
public string Attribute;
public string PrivateName;
public string PublicName;
public Type Type;
public MvvmPropertyInfo(string privateName, string publicName, Type type, string attribute)
{
PrivateName = privateName;
PublicName = publicName;
Type = type;
Attribute = attribute;
}
}
#>
MvvmClassBody.ttinclud ソース
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace <#= classinfo.NameSpaceName #>
{
public partial class <#= classinfo.ClassName #> : INotifyPropertyChanging, INotifyPropertyChanged
{
<# foreach(var property in classinfo.Property) { #>
private <#=property.Type.ToString()#> <#= property.PrivateName #>;
partial void On<#= property.PublicName #>Changing(<#=property.Type.ToString()#> value);
partial void On<#= property.PublicName #>Changed();
<#=property.Attribute#>
public <#=property.Type.ToString()#> <#=property.PublicName#>
{
get
{
return this.<#= property.PrivateName #>;
}
set
{
if (this.<#= property.PrivateName #> != value)
{
this.On<#= property.PublicName #>Changing(value);
this.RaisePropertyChanging("<#= property.PublicName #>");
this.<#= property.PrivateName #> = value;
this.RaisePropertyChanged("<#= property.PublicName #>");
this.On<#= property.PublicName #>Changed();
}
}
}
<# } #>
public event PropertyChangingEventHandler PropertyChanging;
protected virtual void RaisePropertyChanging(String propertyName)
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
}
結果出来上がる MainViewModel.properties.cs
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace T4Mvvm.ViewModel
{
public partial class MainViewModel : INotifyPropertyChanging, INotifyPropertyChanged
{
private System.Int32 id;
partial void OnIDChanging(System.Int32 value);
partial void OnIDChanged();
public System.Int32 ID
{
get
{
return this.id;
}
set
{
if (this.id != value)
{
this.OnIDChanging(value);
this.RaisePropertyChanging("ID");
this.id = value;
this.RaisePropertyChanged("ID");
this.OnIDChanged();
}
}
}
private System.String name;
partial void OnNameChanging(System.String value);
partial void OnNameChanged();
[Required()]
public System.String Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.OnNameChanging(value);
this.RaisePropertyChanging("Name");
this.name = value;
this.RaisePropertyChanged("Name");
this.OnNameChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
protected virtual void RaisePropertyChanging(String propertyName)
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
}