0

曜日に真/偽の値としてフラグを立てるために使用しているアトミッククラスがあります。

public class DaysOfWeek
{
    public bool Sunday { get; set; }
    public bool Monday { get; set; }
    public bool Tuesday { get; set; }
    public bool Wednesday { get; set; }
    public bool Thursday { get; set; }
    public bool Friday { get; set; }
    public bool Saturday { get; set; }

    public bool this[string day]
    {
        get
        {
            return (bool)GetType().GetProperty(day).GetValue(this, null);
        }
        set
        {
            GetType().GetProperty(day).SetValue(this, value);
        }
    }
}

これを Entity を使用して単一の列として保存したいと思います。次のようなPOCOがあります。

public class SSRS_Subscription
{
    [Key]
    public Guid id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public Recurrence RecurrencePattern { get; set; }
    public DateTime StartTime { get; set; }
    public int? MinuteInterval { get; set; }
    public int? DaysInterval { get; set; }
    public int? WeeksInterval { get; set; }
    [NotMapped]
    public DaysOfWeek DaysOfWeek
    {
        get
        {
            return SerializeHelper.DeserializeJson<DaysOfWeek>(internalDaysOfWeek);
        }
        set
        {
            internalDaysOfWeek = SerializeHelper.SerializeJson(value);
        }
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    [Column("DaysOfWeek")]
    public string internalDaysOfWeek { get; set; }

    public SSRS_Subscription()
    {
        DaysOfWeek = new DaysOfWeek();
    }
}

ここでの問題は、DaysOfWeek プロパティにアクセスするときに値を設定できないことです。

var testSub = new SSRS_Subscription();
testSub.DaysOfWeek.Friday = true;
// testSub.DaysOfWeek.Friday stays false (the default value)

// However doing this sets the correct value...
var tmpDaysOfWeek = testSub.DaysOfWeek;
tmpDaysOfWeek.Friday = true;
testSub.DaysOfWeek = tmpDaysOfWeek;

必要なのは ObservableCollection イベントだと思いますが、例を検索した後、それを実装する方法が正確にはわかりません。Entity POCO SSRS_Subscription を変更して追加する必要がありますか? これをより良く行うためのヒントやヒントをいただければ幸いです。

4

1 に答える 1

0

これが私が思いついた解決策でした...私はISerializeインターフェースを作成し、POCOにそれを実装させました...

public interface ISerialize
{
    void Serialize();
}

public class SSRS_Subscription : ISerialize
{
    [Key]
    public Guid id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public Recurrence RecurrencePattern { get; set; }
    public int? MinuteInterval { get; set; }
    public int? DaysInterval { get; set; }
    public int? WeeksInterval { get; set; }
    [NotMapped]
    public DaysOfWeek DaysOfWeek { get; set; }

    [Column("DaysOfWeek")]
    private string internalDaysOfWeek { get; set; }

    [NotMapped]
    public MonthsOfYear MonthsOfYear { get; set; }


    [Column("MonthsOfYear")]
    private string internalMonthsOfYear { get; set; }

    public Catalog_Reports_Subscription()
    {
        DaysOfWeek = new DaysOfWeek();
        MonthsOfYear = new MonthsOfYear();
    }

    public WhichWeek? MonthWhichWeek { get; set; }  

    public string Days { get; set; }

    public void Serialize()
    {
        internalDaysOfWeek = SerializeHelper.SerializeJson(DaysOfWeek);
        internalMonthsOfYear = SerializeHelper.SerializeJson(MonthsOfYear);
    }

    public class Configuration : EntityTypeConfiguration<SSRS_Subscription>
    {
        public Configuration()
        {
            Property(s => s.internalDaysOfWeek).HasColumnName("DaysOfWeek");
            Property(s => s.internalMonthsOfYear).HasColumnName("MonthsOfYear");
        }
    }
}

次に、DbContext に次の変更を加えました。

   public ReportingDbContext() : base("ReportingDbContext")
    {
        var objectContext = ((IObjectContextAdapter)this).ObjectContext;
        objectContext.SavingChanges += new EventHandler(OnSavingChanges);
    }

    public void OnSavingChanges(object sender, EventArgs e)
    {
        foreach (ObjectStateEntry entry in
            ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
        {
            if (!entry.IsRelationship && (entry.Entity is ISerialize))
            {
                (entry.Entity as ISerialize).Serialize();
            }
        }
    }

コンテキストで SaveChanges が呼び出されるたびに、SavingChanges が先に実行され、ISerialize を実装するオブジェクトを探し、Serialize 関数を呼び出します。この関数は、エンティティにマップされていないプロパティを取得し、それらを JSON にシリアル化し (私の場合)、マップされた文字列プロパティに格納します。それらを表すもの。

于 2016-12-20T14:24:05.903 に答える