我在寫一個通用類型來處理底層的Active Directory對象,如組,組織單位和用戶。通用接口VS通用方法?
我也在我的通用接口中使用「手動」依賴注入。我想知道,在我的情況下,哪種更合適:泛型接口還是泛型方法?
下面是一個簡單的代碼示例,向您展示:
public interface IDirectorySource<T> where T : IDirectoryEntry {
IDirectorySearcher<T> Searcher { get; }
CustomSet<T> ToList(); // Related to my question, as I will here only return Searcher.ToList();
}
public interface IDirectorySearcher<T> where T : IDirectoryEntry {
DirectorySearcher NativeSearcher { get; }
CustomSet<T> ToList(); // Related to my question...
}
public sealed class GroupSearcher : IDirectorySearcher<Group> {
public GroupSearcher(DirectoryEntry root, SearchScope scope) {
// Instantiating...
}
public DirectorySearcher NativeSearcher { get; private set; }
public CustomSet<T> ToList() { // That is the point of my question.
// Listing all T objects found in AD...
}
}
public sealed class DirectorySource<T> : IDirectorySource<T> where T : IDirectoryEntry {
public DirectorySource(IDirectorySearcher<T> searcher) {
Searcher = searcher;
}
public IDirectorySearcher<T> Searcher { get; private set; }
public CustomSet<T> ToList() { // Here's the point to my question.
return Searcher.ToList();
}
}
所以,這裏是我的觀點。我想讓我的IDirectorySource
界面非一般化,因爲我會推廣我的DirectorySource<T>
類到大衆。所以我只需要聲明的來源,像這樣:
GroupSearcher groupSearcher = new GroupSearcher(root, scope);
IDirectorySource groups = new DirectorySource<Group>(groupSearcher);
所以我能夠以檢索該組的列表:
groups.ToList(); // Getting all the existing groups in AD here...
但是我是否應該讓我IDirectorySource<T>
接口通用的我不知道,或者使其非泛型,並使我的IDirectorySource.ToList()
方法通用,所以我不需要鍵入我的接口,而只需要爲我提供接口實例的類。
它會更好寫我的接口,像這樣:
public interface IDirectorySource {
CustomSet<T> ToList<T>();
} // With all the appropriate changes, indeed.
我知道這也許是不夠明確。隨時問我你的問題,以便我可以幫助你來幫助我。
在此先感謝! =)
你可以在.NET 3.5中使用AccountManagement命名空間嗎?這可能會使你試圖寫入的代碼變得不必要。 – tvanfosson 2010-07-12 19:10:31
您的「簡化代碼示例」可以使用更簡單的一點。嘗試削減代碼,以便僅說明您想要討論的模式。你的示例中有很多無關細節(例如FindProperties()等)。 – Karmastan 2010-07-12 19:12:11
不幸的是,我不允許使用.NET 3.5,儘管我會喜歡它,因爲我可以使用Bart de Smet的LINQ-to-AD庫。 =)謝謝你的建議! – 2010-07-12 19:14:25