我有一個需要2個構造函數的命令類。但是, 使用structuremap似乎我只能指定一個構造函數來使用 。我現在通過子類型化特定的命令類來解決問題,其中每個實現都實現它自己的接口和構造函數 。就像下面的代碼所示。 ISelectCommand爲 字符串構造函數和int構造函數實現了兩個單獨的接口,僅僅爲了 使用結構映射註冊了這兩個子類型。重載構造映射構造函數
但是,我認爲這是一個黑客,我只是想知道爲什麼它不是 結構映射可以解決由作爲構造函數的參數傳入的 類型的構造函數簽名?然後我可以註冊 SelectProductCommand作爲一個ISelectCommand並且 將它例化爲: ObjectFactury.With(10).Use>(); (「testproduct」)。使用>();
public class SelectProductCommand : ISelectCommand<IProduct>,
ICommand, IExecutable
{
private readonly Func<Product, Boolean> _selector;
private IEnumerable<IProduct> _resultList;
public SelectProductCommand(Func<Product, Boolean> selector)
{
_selector = selector;
}
public IEnumerable<IProduct> Result
{
get { return _resultList; }
}
public void Execute(GenFormDataContext context)
{
_resultList = GetProductRepository().Fetch(context,
_selector);
}
private Repository<IProduct, Product> GetProductRepository()
{
return ObjectFactory.GetInstance<Repository<IProduct,
Product>>();
}
}
public class SelectProductIntCommand: SelectProductCommand
{
public SelectProductIntCommand(Int32 id): base(x =>
x.ProductId == id) {}
}
public class SelectProductStringCommand: SelectProductCommand
{
public SelectProductStringCommand(String name): base(x =>
x.ProductName.Contains(name)) {}
}
P.我知道如何告訴結構圖什麼構造函數圖使用,但我的問題是,如果有一種方法讓結構圖根據傳遞給構造函數的參數(即使用常規方法重載)選擇正確的構造函數。
這裏是讓你在命令/處理程序去一些鏈接: http://lostechies.com/jimmybogard/2010/01/07/advanced-structuremap-custom-registration-conventions-for-partially-關閉類型/ http://lostechies.com/derickbailey/2008/11/20/ptom-command-and-conquer-your-ui-coupling-problems/ – Henning
我很抱歉,但這不是我正在尋找的答案對於。我知道你由傑里米米勒提到的帖子。但正如我所提到的,這隻涉及事先指定一個特定的構造函數。至於你對命令模式的評論,謝謝,我會研究一下。但是,您應該認識到命令類(可能命名不合適)僅僅是從存儲庫中獲取產品的select方法的包裝。我也有插入,刪除和更新命令。這種設置的意圖是,我可以將它們添加到傳遞給事務管理器的命令列表中。 – halcwb