を検証すオブジェクトに基づく外部要因が打ち出されている。-データストアの一意性)

StackOverflow https://stackoverflow.com/questions/1721327

質問

説明

私の解決にはこれらのプロジェクト:

  • ダル =修飾体の枠組み
  • DTO =データ転送オブジェクトが検証自ら
  • BL =ビジネス層のサービス
  • ウェブ =発表Asp.net MVCの応用

DAL、BLのWEBすべての参照DTOあります。
のライセンスを実行しこの方法:

  1. ウェブ請求にあたっては、ウェブ
  2. ウェブがDTOs掲載
    • DTOs得automagically検証経由でカスタムActionFilter
    • 認証エラー自動収集
  3. (バリデーションはOK)ウェブの呼び出しBLの提供DTOs
  4. BLの呼び出しダルを用いDTOs(いずれかを渡しても使用)

DTO検証問題そして...

私DTOsでの検証の基本に据え、自分の頭で自分の状態(プロパティ値)。しかし現時点では、そういった場合の問題の場合はこの限りではありません.いて検証を使用BL(そしてその結果としてDAL).

私の実例:ユーザー登録-WEBがユーザー DTOることが検証されます。問題のある部 username バリデーションを実施します。その独自性を確認する必要があるデータに対応す。
かんすることになっているのです。

が追加情報がすべてのDTOsインターフェースを実装する計画です。 User DTOを実装し IUser)のためにIoCの目的やTDD.両方の DTOプロジェクト.

きう

  1. できないを参照BLにDTOがかな円形の参考にする。
    Compilation error
  2. んを追加DTO.Valプロジェクトが参照の一部をDTO授業の実施を検証がっ参考BL+DTO).
    Partial classes can't span assemblies.

きう

  1. を特 ActionFilter とを検証すオブジェクトの外部条件です。この作成した ウェブプロジェクト このように見DTO、BLて使用される。
  2. 入DTOsにBLいDTOインターフェースを実DTOsによって参照される他のプロジェクトは、refactorすべてのコードを使用界面の代わりにコンクリート。
  3. な取扱いを外部依存の検証して外部依存関係の例外をスローすんだろうな 最悪の 課題

んのでしょうか。

役に立ちましたか?

解決 4

得られた溶液

Iは、物体自体から得ることができない外部要因に対してオブジェクトを検証することができたコントローラアクションフィルタを使用してしまった。

私は、その特定のパラメータを検証するタイプをチェックして、バリするactionパラメータの名前を取り、フィルタを作成しました。もちろん、このバリデータは、すべてが再利用可能にするために、特定のインターフェイスを実装する必要があります。

[ValidateExternalFactors("user", typeof(UserExternalValidator))]
public ActionResult Create(User user)

バリデータはこのシンプルなインタフェースを実装する必要があります。

public interface IExternalValidator<T>
{
    bool IsValid(T instance);
}

これは、一見複雑な問題にシンプルかつ効果的なソリューションです。

他のヒント

私はあなたが試していました爆最後の週です。

に基づく この刺激 私の作成DTOsることを検証すが少し異なりますの DataAnnotations ます。サンプルDTO:

public class Contact : DomainBase, IModelObject
{
    public int ID { get; set; }
    public string Name { get; set; }
    public LazyList<ContactDetail> Details { get; set; }
    public DateTime Updated { get; set; }


    protected override void ConfigureRules()
    {
        base.AddRule(new ValidationRule()
        {
            Properties = new string[] { "name" },
            Description = "A Name is required but must not exceed 300 characters in length and some special characters are not allowed",
            validator = () => this.Name.IsRequired300LenNoSpecial()
        });

        base.AddRule(new ValidationRule()
        {
            Properties = new string[] { "updated" },
            Description = "required",
            validator = () => this.Updated.IsRequired()
        });
    }
}

これ以上の仕事があり DataAnnotations ともに、フランス語が堪能な方、是非あな莫大なものです。多だと思い商用アニメーションのクラスしてくれてありがとうございます何も醜いDTO教室と DataAnnotations attributes-できないものの特性に他なります。の匿名の代表が本アプリは、ほとんどの書籍の価値(がんを発見す).

基底クラス:

public partial class DomainBase : IDataErrorInfo
{
    private IList<ValidationRule> _rules = new List<ValidationRule>();

    public DomainBase()
    {
        // populate the _rules collection
        this.ConfigureRules();
    }

    protected virtual void ConfigureRules()
    {
        // no rules if not overridden
    }

    protected void AddRule(ValidationRule rule)
    {
        this._rules.Add(rule);
    }





    #region IDataErrorInfo Members

    public string Error
    {
        get { return String.Empty; }    // Validation should call the indexer so return "" here
    }                                   // ..we dont need to support this property.

    public string this[string columnName]
    {
        get
        {
            // get all the rules that apply to the property being validated
            var rulesThatApply = this._rules
                .Where(r => r.Properties.Contains(columnName));

            // get a list of error messages from the rules
            StringBuilder errorMessages = new StringBuilder();
            foreach (ValidationRule rule in rulesThatApply)
                if (!rule.validator.Invoke())   // if validator returns false then the rule is broken
                    if (errorMessages.ToString() == String.Empty)
                        errorMessages.Append(rule.Description);
                    else
                        errorMessages.AppendFormat("\r\n{0}", rule.Description);

            return errorMessages.ToString();
        }
    }

    #endregion
}

ValidationRule 私の検証機能

public class ValidationRule
{
    public string[] Properties { get; set; }
    public string Description { get; set; }
    public Func<bool> validator { get; set; }
}


/// <summary>
/// These extention methods return true if the validation condition is met.
/// </summary>
public static class ValidationFunctions
{
    #region IsRequired

    public static bool IsRequired(this String str)
    {
        return !str.IsNullOrTrimEmpty();
    }

    public static bool IsRequired(this int num)
    {
        return num != 0;
    }

    public static bool IsRequired(this long num)
    {
        return num != 0;
    }

    public static bool IsRequired(this double num)
    {
        return num != 0;
    }

    public static bool IsRequired(this Decimal num)
    {
        return num != 0;
    }

    public static bool IsRequired(this DateTime date)
    {
        return date != DateTime.MinValue;
    }

    #endregion


    #region String Lengths

    public static bool IsLengthLessThanOrEqual(this String str, int length)
    {
        return str.Length <= length;
    }

    public static bool IsRequiredWithLengthLessThanOrEqual(this String str, int length)
    {
        return !str.IsNullOrTrimEmpty() && (str.Length <= length);
    }

    public static bool IsRequired300LenNoSpecial(this String str)
    {
        return !str.IsNullOrTrimEmpty() &&
            str.RegexMatch(@"^[- \r\n\\\.!:*,@$%&""?\(\)\w']{1,300}$",
                RegexOptions.Multiline) == str;
    }

    #endregion

}

う場合のコードを見汚くことになるので私みたことの検証アプローチのための最後の数日間です。このアイデアを満たす数の要件:

  • 私をサポートする必要がある IDataErrorInfo interfaceっMVC層の存在を自動的に
  • がある複雑な認証シナリオ全てのご質問かもしれません):こういうことができるように検証するに対し複数の特性をオブジェクトと同じオブジェクト(ie.StartDateとFinishDate);特性の異なる複数/れに関連したオブジェクトのようにしてオブジェクトのグラフ;とてもいいっていない。
  • 私をサポートする必要があるようにエラーを複数物件
  • の一環として、TDDとDDD旅の気持ちを抱いて欲しいと思いドメインのオブジェクトの記述より私のインターネットドメイン'によってサービスの層方法でこれらの複雑な条件のモデルオブジェクト(DTOsというこ

このアプローチだと思いまだに何をしたいと思います。

私は想像だに飛び乗っこいわれるよう'ものですが使い勝手は大きく変わります。私は、 新しい検証機能をMVC2 していますが、いまだにしない上記のほしい物リストなカスタム。

武器agiは、dexで下がらないboxerぐ.

S#ARPアーキテクチャは、クラスレベルのバリデータは[HasUniqueDomainSignature】作業を行うであろうと使用[DomainSignature]メソッド識別子を有しています。以下のサンプルコードを参照してください。

[HasUniqueDomainSignature]
public class User : Entity
{
    public User()
    {
    }

    public User(string login, string email) : this()
    {
        Login = login;
        Email = email;
    }

    [DomainSignature]
    [NotNullNotEmpty]
    public virtual string Login { get; set; }

    [DomainSignature]
    public virtual string Email { get; set; }

}

http://www.sharparchitecture.net/する

で詳しく見てみましょう

私はこれと同じ問題を抱えていたし、日と日と日間の周りの仕事を見つけようとした後、私は1つのライブラリに私のDTO、DAL、およびBLをマージすることになりました。私は私のプレゼンテーション層を分離しておきます。 それはあなたのための選択肢であるかどうかわかりません。私にとっては、私が今までにデータストアを変更する私のチャンスは非常にわずかだったので、別の層が本当に必要なかったことを考え出します。

私はまた、すべての私のDTOの検証のためのMicrosoftの検証アプリケーションブロックを実装しています。彼らはあなたが複雑な検証を行うことができます「自己検証」メソッドを持っています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top