在我的ASP.NET MVC 3項目中,我已經在單獨的類庫項目中實現了存儲庫模式。IValidatableObject和依賴注入支持
另外我使用EF作爲ORM。我還使用IValidatableObejct
接口實施了一些模型驗證。下面是它的樣子:
[MetadataType(typeof(AccommPropertySeasonPeriodAlias.MetaData))]
public partial class AccommPropertySeasonPeriodAlias : IValidatableObject {
private class MetaData {
[StringLength(5), Required]
[Display(Name = "Period Alias Name")]
public string AccommPropertySeasonPeriodAlias1 { get; set; }
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
var repo = new AccommPropertySeasonPeriodAliasRepository();
if (repo.GetAll(this.AccommPropertySeasonID).
Where(x => x.AccommPropertySeasonPeriodAlias1 == this.AccommPropertySeasonPeriodAlias1) != null)
yield return new ValidationResult("Alias Name needs to be unique");
}
}
正如你看到的,從現在開始,我的模型完全緊耦合的,因爲我已經使用AccommPropertySeasonPeriodAliasRepository
類,而不是直接使用IAccommPropertySeasonPeriodAliasRepository
的。
什麼是這樣做的權利,使我的模型就可以了,好了,(不知道這是正確的字)的方式假能進行單元測試?
完全是我在找的東西。非常感謝! – tugberk