Objective
To write a effecient Active Directory library to ease the work of technicals who are responsible to create access models into the domain controller's Active Directory. This library must allow the following:
- 基本操作:添加、修改、删除、列表条目;
- 条目可以是组织单位、组或用户(目前不需要进一步要求);
我想过要有一个类来代表我们想要使用的领域。
public class Domain {
public Domain(string root) {
Root = root;
Entries = new Dictionary<string, IDirectoryEntry>();
}
public string Root { get; private set; }
public Dictionary<string, IDirectoryEntry> Entries { get; private set; }
}
然后,我使用依赖注入(inject)来强制对条目域的归属约束。例如:
public abstract class DirectoryEntry : IDirectoryEntry {
public DirectoryEntry(Domain domain, string name) {
Domain = domain;
Name = name;
Domain.Entries.Add(name, this);
}
public Domain { get; private set; }
public Name { get; set; }
}
public class OrganizationalUnit : DirectoryEntry {
public OrganizationalUnit(Domain domain, string name)
: base(domain, name) {
}
}
public class Group : DirectoryEntry {
public Group(Domain domain, string name)
: base(domain, name) {
}
}
现在,请注意,我在实例化 IDirectoryEntry
接口(interface)时使用 Domain.Entries.Add()
添加条目到给定的域。
Questions
如果我不希望用户更改任何
IDirectoryEntry
实例的Domain
属性,这是一个好习惯吗?简单地让
Domain.Entries.Add()
行消失,并在我的Domain
类中有一个方法会添加域的条目?
问题 #2 的代码示例
public class Domain {
//See above for other members.
public void AddEntry<T>(T entry) {
Entries.Add(entry.Name, entry);
}
}
- 在您看来,这种情况下最好的架构是什么?
两者似乎都足够好,可以考虑,所以我有点困惑它希望为图书馆最终用户提供最简单的方法。
最佳答案
您是否看过 .NET 3.5/4 的 System.DirectoryServices.AccountManagement 命名空间?它在更加统一且 .NET 友好的界面中提供了您需要的大部分功能。我个人编写了一个与您的要求类似的库,并结合使用了两者。
总的来说,我认为你的设计看起来不错,但我对你的问题领域了解不够,无法知道你是否会把自己逼到墙角。
具体来说,对于问题1,我认为可以;但是,任何引用 Domain
实例的人都可以删除任何给定的条目。
对于问题 2,这很可能是我自己实现的方式,除非我有令人信服的理由不这样做。
关于c# - 哪一个是更好的架构/设计方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168573/