仅设置属性是不好的做法吗?

发布于 2024-10-09 22:31:40 字数 193 浏览 1 评论 0原文

我有一些 C# 代码,喜欢创建具有 setter 但没有 getter 的属性。对我来说,这似乎是一种反模式,但我错过了什么吗?

public List<SiteVisitSummary> DataSource {
    set {
        // crazy logic here 
    }
}

I have some C# code that loves to create properties that have setters but no getters. To me this seems like an anti-pattern, but am I missing something?

public List<SiteVisitSummary> DataSource {
    set {
        // crazy logic here 
    }
}

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

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

发布评论

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

评论(7

夏了南城 2024-10-16 22:31:40

我不知道是否可以将其称为反模式。我认为要认识到的重要一点是,示例中的属性本质上是一种伪装成属性的方法。由于这个原因,它是不直观的,我通常会避免它。对我来说,这看起来更自然:

public void SetDataSource(IEnumerable<SiteVisitSummary> dataSource)
{
    // crazy logic here
}

我见过的仅设置属性的一个相当合理的情况是,当有多个属性组合在一起时,将所有属性设置为单个值是合理的事情(但显然,将所有内容作为一个值来获取是没有意义的)。例如:

class Polyhedron
{
    public int Height { get; set; }
    public int Width { get; set; }
    public int Depth { get; set; }

    public int AllDimensions
    {
        set { Height = Width = Depth = value; }
    }
}

I don't know if I'd call it an anti-pattern or not. I think the important thing to realize is that the property in your example is essentially a method, disguised as a property. For this reason it's unintuitive, and I'd generally avoid it. To me this looks a lot more natural:

public void SetDataSource(IEnumerable<SiteVisitSummary> dataSource)
{
    // crazy logic here
}

One fairly reasonable case I have seen made for set-only properties is when there are several properties grouped together such that setting all to a single value is a reasonable thing to do (but obviously, getting all as one value doesn't make sense). For example:

class Polyhedron
{
    public int Height { get; set; }
    public int Width { get; set; }
    public int Depth { get; set; }

    public int AllDimensions
    {
        set { Height = Width = Depth = value; }
    }
}
趁微风不噪 2024-10-16 22:31:40

它可能对 依赖注入,您不使用构造函数进行注入,而是使用属性。

It could be useful for Dependency Injection, where instead of using a constructor for the injection you are using properties.

没有心的人 2024-10-16 22:31:40

我不认为这种做法本质上有什么不好,尽管它对我来说没有多大意义。

封装的目的是限制可以改变对象状态的内容,以便状态变得更容易维护。我想不出任何充分的理由将属性设置为只写。而且,如果可以的话,将其变成一种方法可能更有意义。

用户习惯于能够读取属性,他们可能会浪费时间想知道为什么你的属性如此奇怪。

I don't see anything inherently bad about the practice, although it doesn't make much sense to me.

The point of encapsulation is to limit what can change the state of an object so that the state becomes easier to maintain. I can't think of any good reason to make a property write-only. And, if I could, it might more sense to make it a method instead.

Users are accustomed to being able to read properties and they may waste time wondering why the heck your property is so weird.

一张白纸 2024-10-16 22:31:40

这样的做法并不坏,只是不是很有用(您无法阅读的属性?)。

Not bad practice as such, just not very useful (a property that you can't read?).

在梵高的星空下 2024-10-16 22:31:40

如果您期望发生很多逻辑,那么通过使用一种方法来设置它似乎会更清楚地了解成本。

If you are expecting much logic to happen, it would seem much clearer of the cost by using a method to set that.

摇划花蜜的午后 2024-10-16 22:31:40

这可能不是每个人都喜欢的,但我 发布了一个答案另一个问题,我在其中演示了“infuser”概念的使用,在本例中是一个嵌套在不可变结构中的结构。 Infuser 具有只读属性,可以补充不可变类型的只读属性。

公平地说,我同意这种方法可能会令人困惑,并且在将这样的代码包含在不仅仅是一个业余爱好项目的应用程序中之前,我会三思而后行。

这个概念对于普通类也很有用,而不仅仅是不可变类型。它允许负责实例化对象的代码对该对象中包含的数据进行读/写访问。然后,该对象可以传递给其他代码,这些代码对对象内的数据具有只读访问权限。

This may not be everyone's cup of tea, but I posted an answer to another question where I demonstrated the use of an "infuser" concept, in this case a struct nested inside of an immutable struct. The infuser has write-only properties that complement the read-only properties of the immutable type.

To be fair, I agree that this approach might be confusing and I would think twice before including code like this in an application that is not just a hobby project.

The concept can also be useful for normal classes, not just immutable types. It allows the code that is responsible for instantiating an object to have read/write access to the data contained within that object. Then the object can be passed off to other code, which has read-only access to the data within the object.

婴鹅 2024-10-16 22:31:40

这确实是一件很奇怪的事情。为什么要编写一个允许用户设置列表但不能读取列表的 API?返回 ReadOnlyCollection 并将属性公开为 IList 而不是 List 怎么样?

It's kind of a weird thing really. Why would you write an API that allows the user to set a list but not read it? How about returning a ReadOnlyCollection instead and exposing the property as an IList instead of a List?

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