0

Entity Framework 6 (Model First) を使用しています。そのため、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 内のプロパティの変更について通知を受けたい場合、これを行うのは良いことでしょうか? それは冗長でしょうか?他の提案はありますか?

4

1 に答える 1

0

[ImplementsPropertyChanged] を Car クラスの他の部分クラスに追加することになりました。これにより、どのクラスが実際にプロパティの変更を実装する必要があるかを制御できます。

.tt ファイルに追加しても問題は発生しないようですが、部分クラスの残りの「半分」に追加すると、より詳細に制御できます。

于 2014-11-19T04:06:44.340 に答える