我想知道如果我在這裏思考的正確軌道,我主要編程接口,所以我想知道下面的類應該通過DI注入還是應該我自己實例化一個類。 ..依賴注入 - 構造
注:這些服務在我的核心舉行libaray不是我的web應用程序(asp.net的MVC)
// IUserSession是從來不知道,這取決於客戶端應用程序,這是從來沒有已知所以我必須始終注射
// IWorkflowService我需要注入的客戶端應用程序有一些不同的服務/包,具體取決於業務規則可能會發生變化
// IReadonlySession是永遠只1我的應用程序,所以我從來沒有需要注入?可能是在其他客戶端調用不同,但不太可能
// INotificationService將永遠是不管我說的是什麼,它也始終是永遠將是一兩件事,所以我應該對編程的接口,而不是注入?
private readonly IUserSession _userSession;
private readonly IReadOnlySession _readonlySession;
private readonly INotificationService _notificationService;
public Processor(IUserSession userSession, IWorkflowService workflowService)
: base(workflowService)
{
_userSession = userSession;
_readonlySession = new ReadonlySession();
_notificationService = new NotificationService();
}
// IReadonlySession注入,因爲這可能會改變取決於我是否正在運行的測試,我可以使用不同的數據庫,或者我可以從不同的客戶端應用程序調用我的代碼(不可能的,但有可能)
public Processor(IUserSession userSession, IWorkflowService workflowService, IReadonlySession readonlySession)
: base(workflowService)
{
_userSession = userSession;
_readonlySession = readonlySession;
_notificationService = new NotificationService();
}
問:
是我的對象的實例化正確嗎?我實施它的方式是否正確?
您的問題是什麼? – 2012-01-17 11:30:42
@MarkSeemann:更新... – Haroon 2012-01-17 11:42:26