6

次のコードがあります。

using System;
using System.Collections.Generic;
using System.Text;
using ProjectBase.Utils;

namespace EnterpriseSample.Core.Domain
{
    public class Notifikasi : DomainObject<string>, IHasAssignedId<string>
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime CreatedDate { get; set; }
        public string CreatedBy { get; set; }
        public DateTime UpdateDate { get; set; }
        public string UpdateBy { get; set; }

        public void SetAssignedIdTo(string assignedId)
        {
            Check.Require(!string.IsNullOrEmpty(assignedId), "assignedId may not be null or empty");

            // As an alternative to Check.Require, which throws an exception, the 
            // Validation Application Block could be used to validate the following
            Check.Require(assignedId.Trim().Length == 5, "assignedId must be exactly 5 characters");

            ID = assignedId.Trim().ToUpper();
        }

        public override int GetHashCode()
        {
            return (GetType().FullName + "|" +
                    ID + "|" +
                    Name).GetHashCode();

        }
    }
}

別のクラスで Notifikasi クラスを使用していますがoNoti.ID = "12";、エラーが発生します。

set アクセサーにアクセスできないため、このコンテキストではプロパティまたはインデクサーを使用できません。

誰でもこれを解決するのを手伝ってもらえますか? (関連する場合は ORM を使用します)。

INotifikasiDao dao = DaoFactory.GetNotifikasiDao();

dao.closeSession();
dao.openSession();
EnterpriseSample.Core.Domain.Notifikasi oNoti = new EnterpriseSample.Core.Domain.Notifikasi();
int no = 1;
oNoti.Name = "ETL Account History";
DateTime startDate = DateTime.Today;
DateTime expiryDate = startDate.AddDays(30);
oNoti.StartDate = startDate;
oNoti.EndDate = expiryDate;
oNoti.Description = "ACC_HIST" + startDate + "Failure";
oNoti.ID = "12"; //this is where the error happens

dao.Update(oNoti);
4

2 に答える 2

11

あなたの質問には の重要な定義がありませDomainObjectんが、エラー メッセージはプロパティIDが次のように宣言されていることを示しています。

public string ID
{
    get;
    protected set;
}

つまり、 の値はID誰でも読み取ることができますが、DomainObjectおよび派生クラスのインスタンス内からのみ設定できます。

5 文字が必要なため、使用SetAssignedIdToすることもできません。割り当てる ID の長さは 2 文字のみです。このメソッドは、通常は自動インクリメント キーを持つテーブル内の行に ID を手動で割り当てることができるようにするために存在すると想定しています。この ID に 5 文字を強制することで、手動で割り当てられたキーが自動作成されたキーと競合しないことがある程度保証されます。

于 2013-01-22T09:27:29.197 に答える