简体   繁体   English

拥有私人访问者的公共领域是否有意义?

[英]Does having a public field with private accessors make sense?

I have a class called GestorePersonale which holds a list of instances of another class: 我有一个名为GestorePersonale的类,它包含另一个类的实例列表:

public List<Dipendente> Dipendenti
{
    get;
    private set;
}

I want to keep this list modifiable only from the methods the class exposes, and not directly. 我想保持这个列表只能从类暴露的方法中修改,而不是直接修改。 I noticed that with the code above, one could just do 我注意到,使用上面的代码,人们可以做到

var gp = new GestorePersonale();
gp.Dipendenti.Add( new Dipendente( ... ) );
and be able to perform any other kind of action on the List<Dipendente> itself. 并能够在List<Dipendente>本身上执行任何其他类型的操作。

I considered converting the first code snippet to 我考虑过将第一个代码段转换为

\nprivate List dipendenti; 私人清单; 

but I could find a few downsides to that: 但我可以找到一些缺点:

  • This would break the personal rule of mine to try to always use the public fields over the private ones from inside the class's methods whenever possible (even though I'm not sure if it is good practice to do so, so any clarification would be welcome); 这会打破我的个人规则,试图在可能的情况下尽量使用公共领域而不是从类方法中使用公共领域(即使我不确定这样做是否是好的做法,所以任何澄清都是受欢迎的);
  • This would impair any external entities' ability to access the contents of the list for reading purposes only, like, say, to execute a LINQ query over the contents of the list. 这会损害任何外部实体访问列表内容的能力,仅用于读取目的 ,例如,对列表内容执行LINQ查询。

What would be the best way to solve this situation? 什么是解决这种情况的最佳方法?

You can wrap the list in a ReadOnlyCollection<T> and expose that: 您可以将列表包装在ReadOnlyCollection <T>中并公开:

private List<Dipendente> dipendenti;
private ReadOnlyCollection<Dipendente> readOnlyDipendenti;

public GestorePersonale()
{
    dipendenti = new List<Dipendente>();
    readOnlyDipendenti = new ReadOnlyCollection<Dipendente>(dipendenti);
}

public ReadOnlyCollection<Dipendente> Dipendenti
{
    get { return readOnlyDipendenti; }
}

Internally, you have access to dipendenti and can add/remove items. 在内部,您可以访问dipendenti并可以添加/删除项目。 External entities have access only to the ReadOnlyCollection<T> that wraps the list, so they can only read, but not add/remove items. 外部实体只能访问包装列表的ReadOnlyCollection <T>,因此它们只能读取,但不能添加/删除项目。

I would agree with dtb that ReadOnlyCollections is the way to go. 我同意dtb,ReadOnlyCollections是要走的路。 However, you can return it from the property getter (using AsReadOnly) and drop the method. 但是,您可以从属性getter(使用AsReadOnly)返回它并删除该方法。

    private List<Dipendente> dipendenti = new List<Dipendente>();

    public ReadOnlyCollection<Dipendente> ReadOnlyDipendenti
    {
        get
        {
            return dipendenti.AsReadOnly(); 
        }
    }

there are a couple of things you can do: 你可以做几件事:

  • you use ReadOnlyCollection 你使用ReadOnlyCollection
  • you can return an IEnumerable<_type> 你可以返回一个IEnumerable<_type>
  • you can wrap the list in another class 你可以将列表包装在另一个类中
  • you can roll your own collection class, implementing the appropriate interface 您可以滚动自己的集合类,实现适当的接口

    the method you use depends on the functionality you need and what you want/need to expose to the user of your class 您使用的方法取决于您需要的功能以及您希望/需要向您的类用户公开的内容

  • What you have is a public property with a private accessor. 您拥有的是带私人访客的公共财产。 It is very useful. 这非常有用。 It allows an instance to expose a value that is controlled (set) by the instance itself, eg a state. 它允许实例公开由实例本身控制(设置)的值,例如状态。

    For example, take a collection with a Count property. 例如,使用Count属性获取集合。 It makes no sense for it have a public accessor. 它有一个公共访问器是没有意义的。 An implementation could be to update the property (internally) when the collection is changed (to avoid having to count it each time). 实现可以是在更改集合时更新属性(内部)(以避免每次都计数)。

    Do a setter method or wrap the field in another class. 执行setter方法或将字段包装在另一个类中。 This is a classic collection set and collection.add problem. 这是一个经典的集合集和collection.add问题。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM