c# - 哪一个是更好的架构/设计方法?

标签 c# .net vb.net architecture dependency-injection

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:

  1. 基本操作:添加、修改、删除、列表条目;
  2. 条目可以是组织单位、组或用户(目前不需要进一步要求);

我想过要有一个类来代表我们想要使用的领域。

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

  1. 如果我不希望用户更改任何 IDirectoryEntry 实例的 Domain 属性,这是一个好习惯吗?

  2. 简单地让 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/

相关文章:

C# 和匿名对象数组

vb.net - 用于从继承类自动创建构造函数的 Visual Studio 快捷方式

c# - 在 Silverlight 中的服务器和客户端项目之间共享类

.net - netstandard1.0/netstandard1.2 和 LINQ

c# - SSIS 脚本任务 AcquireConnection 返回 Null

c# - 如何以引用的 id 作为条件进行 NHibernate 查询?

mysql - 使用 vb 2012 从 mysql 获取特定的行 col 值

c# - (.net) 如何检查给定变量是否定义了属性

c# - 根据条件禁用拖放

javascript - 如何使用变量更改 jquery 数据表语言