是否需要封装Collections?

发布于 2024-10-01 12:24:20 字数 306 浏览 5 评论 0原文

是否需要封装Collections?

private List<Item> items;

public Items Items
{
    get { return this.items; }
    set { this.items= value; }
}

或只是

public List<Item> Items;

编辑:

  • 有没有一种方法可以绑定到集合而不暴露公共吸气剂?

Is there a need to encapsulate Collections?

say

private List<Item> items;

public Items Items
{
    get { return this.items; }
    set { this.items= value; }
}

or just

public List<Item> Items;

EDIT:

  • Is there a way to bind to the collection without exposing the public getter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

任谁 2024-10-08 12:24:20

我通常将集合公开为只读。通常没有理由分配新的集合。我将在构造函数或属性 getter 中实例化一次集合。这减少了空引用错误的可能性。

I generally expose collections as read-only. There's typically no reason to assign a new collection. I'll instantiate the collection one time in the constructor or property getter. This reduces the possibility of null reference errors.

呆头 2024-10-08 12:24:20

公开收藏品是个坏主意。虽然封装字段在任何情况下都很重要,但集合更为重要。

集合本身通常不是您真正关心的对象。因此,类的调用者不应该能够设置它、替换它,甚至删除它(例如:设置为 null)。它应该只关心其中的项目。如果您可以从外部设置列表实例,则您的类的多个实例可能会共享同一个列表实例。这会产生一些令人讨厌的副作用。

您的类负责(至少)创建列表实例,以确保它是专用列表而不是空引用。

通过公共 getter 公开列表实际上就像完全公开它一样。这是你应该做的最小封装,它实际上就像根本没有封装一样。

完全封装是通过仅将项目公开为 IEnumerable 并在单独的方法或属性中提供添加、删除、计数和类似操作来完成的。你需要的越多,事情就越复杂。如果您确实不需要来自外部的许多基本操作并尽可能在类内进行管理,那么它很有用。

Making collections public is quite a bad idea. While encapsulating fields is important in any case, collections are even more important.

The collection itself is usually not an object you actually care about. So the caller of your class shouldn't be able to set it, replace it or even take it away (say: set to null). It should only care about the items in it. If you make it possible to set the list instance from the outside, you risk that several instances of your class share the same list instance. This will have some nasty side effects.

Your class is responsible (at least) to create the list instance to make sure that it is a dedicated list and never a null reference.

Exposing the list by a public getter is actually like fully exposing it. This is the minimal encapsulation you should do, it is actually like no encapsulation at all.

Full encapsulation is done by only exposing the items as IEnumerable and provide Add, Remove, Count and similar operations in separate methods or properties. The more you need, the more it gets complicated. It is useful if you really don't need many basic operations from the outside and manage as much as possible within the class.

终陌 2024-10-08 12:24:20

一般来说,公共字段在 .NET 世界中是不受欢迎的。另外,返回空集合方法或属性也不被接受。因此,我肯定会“封装”这个集合。

Public fields are, in general, frowned upon in the .NET world. Also, returning null collections from methods or properties is also frowned upon. Therefore, I'd definitely "encapsulate" the collection.

往昔成烟 2024-10-08 12:24:20

.net 中有一个标准,即新实例化的对象将其集合初始化以最小化空引用。您会注意到大多数框架库都遵循这一点。

public class PotOfUnicorns
{

    private List<Item> items = new List<Item>();

    public Items Items
    {
        get { return this.items; }
        private set { this.items= value; }
    }

}

There is a standard in .net that newly instantiated objects have their collections initialized to minimize null references. You will notice most of the framework libraries adhere to this.

public class PotOfUnicorns
{

    private List<Item> items = new List<Item>();

    public Items Items
    {
        get { return this.items; }
        private set { this.items= value; }
    }

}
秋千易 2024-10-08 12:24:20

有时我会像这样连接起来......

List<Item> _items;
public Items Items
{
    get 
    {
        if (_items == null) _items = new List<Items>();
        return _items;
    }
    private set { _items = value; }
}

Some times I hook it up like so....

List<Item> _items;
public Items Items
{
    get 
    {
        if (_items == null) _items = new List<Items>();
        return _items;
    }
    private set { _items = value; }
}
回首观望 2024-10-08 12:24:20

建议您不要将集合属性设置器公开;请参阅 MSDN 上的此规则。更好的做法是仅公开接口而不是具体类型。

class Example
{
     public Example()
     {
         this.MyList = new List<string>();
     }

     public IList<string> MyList { get; private set; }
}

You are advised against making collection property setters public; see this rule on MSDN. Even better practice is to only expose the interface rather than concrete type.

class Example
{
     public Example()
     {
         this.MyList = new List<string>();
     }

     public IList<string> MyList { get; private set; }
}
过度放纵 2024-10-08 12:24:20

创建自定义集合来包装标准集合,以便您可以控制调用者的操作是最好的选择,但是它有成本

我认为这取决于有多少不同人将针对您的对象模型编写代码,以及这些人是否与您属于同一团队

通过封装你的集合,你可以阻止调用者做“坏事”;这可能会节省调用者的调试时间,并且还可以让调用者更轻松地了解如何使用 API。

因此,这是您的时间与将使用您的对象模型的程序员的时间之间的权衡。

Creating a custom collection to wrap the standard collection so you can control what your callers do is the best option, however it has costs.

I think it comes down to how many different people will be writing code against your object model and weather these people are part of the same team as you.

You are stopping the callers doing “bad things” by encapsulation your collection; this is likely to save the callers debugging time and also make it easier for your callers to work out how to use your API.

So it is a trade of between your time and the time of the programmers that will be using your object model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文