我的工廠正在使用方法注入,因爲我認爲這是迄今爲止最好的方法。此外,我不確定在從屬對象中調用它的Create
方法後它是一件好事。正確使用參數化的Factory.Create()方法使用DI
在繼續使用參數化工廠Create
方法時,我可能會考慮的唯一方法是直接在MainPresenter
中注入依賴關係,以便它可以提供方法的依賴關係,並且我不喜歡它。它不喜歡它,因爲它不是MainPresenter
,它取決於ICustomerManagementView
和ICustomerDetailPresenterFactory
,這是它的依賴關係。所以我覺得我這樣做破壞了我自己的代碼。
MainPresenter
public class MainPresenter : Presenter<IMainView>, IMainViewUiHandler {
public MainPresenter(IMainView view
, ICustomerManagementPresenterFactory customerManagementFactory)
: base(view) {
this.customerManagementPresenterFactory = customerManagementPresenterFactory;
}
public void ManageCustomers() {
// The following line is causing trouble.
// As you can see per the ICustomerManagementPresenterFactory code sample,
// the Create() method takes two parameters:
// 1. ICustomerManagementView, and
// 2. ICustomerDetailPresenterFactory
// Hence I have to provide the dependencies manually, I guess. Which is
// something to avoid at any cost.
var customerManagementPresenter = customerManagementPresenterFactory.Create();
customerManagementPresenter.ShowView();
}
}
ICustomerManagementPresenterFactory
public interface ICustomerManagementPresenterFactory {
// Here. Though I ask Ninject to inject my dependencies, I need to
// provide values to the parameters when calling the method from within
// the MainPresenter class. The compiler won't let me do otherwise! And
// this makes sense!...
[Inject]
CustomerManagementPresenter Create(ICustomerManagementView view
, ICustomerDetailPresenterFactory factory);
}
IMainView
public interface IMainView : IView, IHasUiHandler<IMainViewUiHandler> {
}
IMainViewUiHandler
public interface IMainViewUiHandler : IUiHandler {
void ManageCustomers();
}
IUiHandler
public interface IUiHandler {
}
IHasUiHandler
public interface IHasUiHandler<H> where H : IUiHandler {
H Handler { set; }
}
的MainForm
public partial class MainForm : Form, IMainView {
public MainForm() { InitializeComponent(); }
public IMainViewUiHandler Handler { private get { return handler; } set { setHandler(value); } }
}
CompositionRoot
public class CompositionRoot {
private CompositionRoot() { }
public static IKernel BuildObjectGraph() {
IKernel kernel = new StandardKernel();
BindFactories(kernel);
BindViews(kernel);
}
private static void BindFactories(IKernel kernel) {
kernel.Bind(services => services
.From(AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.FullName.Contains("Tests")))
.SelectAllInterfaces()
.EndingWith("Factory")
.BindToFactory()
);
}
private static void BindViews(IKernel kernel) {
kernel.Bind(services => services
.From(AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => a.FullName.Contains("Windows")
&& !a.FullName.Contains("Tests"))
.SelectAllClasses()
.EndingWith("Form")
.BindSelection((type, baseType) => type
.GetInterfaces()
.Where(iface => iface.Name.EndsWith("View"))
)
);
}
}
所以我在想,是不是最好的實現ICustomerManagementPresenterFactory
我CompositionRoot
中的綁定與它的實施者,這樣我就可以提供通過構造函數注入這些依賴於Create
不應再採取任何論據的方法,否則我應該採取其他辦法嗎?
我喜歡編寫一個簡單的界面,Ninject對我來說都是爲工廠做的,並且不需要代碼來構建所需類型的實例。另外,當要創建的類的構造函數使用構造函數注入時,似乎不可能將一個簡單的工廠接口綁定爲工廠,而且需要手動實現工廠接口。
我得到了什麼對/錯?