![](/img/trans.png)
[英]How to Inject Generic Repository(Or Business Logic Layer) Into ViewModel
[英]Must Inject the Specifications on Business Layer?
我正在嘗試學習有關依賴注入和規范模式的一些事情。
如果我有這種情況:我有三種方法,它們有不同的驗證規則。 此規則由規范驗證。 所以...我的班級必須在構造函數上收到這樣的規格嗎?
public PostService(IRepositorio rep, ISpecificationSave ss, SpecificationGet g, ISpecificationDelete sd) {
// do things...
}
但如果這是正確的,當我添加一個新方法時,我需要更改de構造函數以接收更多的規范?
或者,即使使用Dependency Inject,更好的是,在這種情況下,在方法上創建一個規范實例,如何使用規范:
public void DoSomeThing(MyObject object) {
Specification<MyObject> specification = new Specification<MyObject>();
// do things...
}
我知道這個問題對你們中的一些人來說很簡單,但我正在努力學習這些模式。
您可以在每個驗證器中使用這些規范,通過它們在您的班級中逐個添加,使用Specitication Pattern,如下所示:
public Class Class1 : IClass1 {
private List<ISpecification> contents;
private List<ISpecification> specializations;
public List GetContents() {
return contents;
}
public Set GetFeatures() {
return specifications;
}
public Class1() {
features = new List<ISpecification>(){//put specializations who belongs this class here};
specialications = new List<ISpecification>();
}
public boolean Validator1() {
foreach(ISpecification as spec in this.specializations) {
if (!spec.GetSpecification().IsSatisfiedBy(this))
return false;
}
return true;
}
}
public class Specification1 : ISpecification {
private object requiredFeature;
public Specification1(object feature) {
requiredFeature = feature;
}
public boolean IsSatisfiedBy(IClass class) {
return class.GetFeatures().contains(requiredFeature);
}
}
然后,您可以通過以下方式在應用中添加規范:
IClass1 class = new Class1();
class.GetFeatures().add(new Specialization1(// some feature));
class.GetFeatures().add(new Specialization2(// some feature));
class.GetFeatures().add(new Specialization3(// some feature));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.