我正在使用实体框架 6(模型优先)。所以我有几个由model.tt为我生成的类。这是我的汽车课:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MyNamespace
{
using System;
using System.Collections.Generic;
public partial class Car
{
public Car()
{
this.Wheels = new HashSet<Wheel>();
}
public int CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Year { get; set; }
public string VIN { get; set; }
public virtual ICollection<Wheel> Wheels { get; set; }
}
}
我还在项目中的其他类上使用 PropertyChanged.Fody。我有几个类的属性只是包装了我生成的类的属性,如下所示:
using System;
using PropertyChanged;
namespace MyNamespace
{
[ImplementPropertyChanged]
public class CarWrapper
{
public CarWrapper(Car car)
{
Car = car;
}
public Car car { get; set; }
public string Make
{
get { return Car.Make; }
set { Car.Make = value; }
}
public string Model
{
get { return Car.Model; }
set { Car.Model = value; }
}
public string Year
{
get { return Car.Year; }
set { Car.Year = value; }
}
public string VIN
{
get { return Car.VIN; }
set { Car.VIN = value; }
}
}
}
因此,ProperyChanged.Fody 会对我的 Car 属性而不是其他属性发挥作用,但如果我要编辑我的 Model.tt 并添加[ImplementPropertyChanged]属性,我生成的类都会在属性更改时发出通知。然后我可以像这样修改 CarWrapper 中的 Car 属性:
[AlsoNotifyFor("Make")]
[AlsoNotifyFor("Model")]
[AlsoNotifyFor("Year")]
[AlsoNotifyFor("VIN")]
public Car car { get; set; }
如果我想在 Car 中收到有关属性更改的通知,这会是一件好事吗?会不会是多余的?还有其他建议吗?