Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface: Setter without a Getter

Tags:

c#

I came across an interface recently that only defined a setter like so:

public interface IAggregationView
{
   DataTable SetSiteData { set; }
}

I queried this, and it is believed that this is one of the practices advocated by Microsoft for WebPart design (for SharePoint). In fact this example is directly copied from their examples.

I see this as a bad pattern, I don't see why someone should be able to set a value, and then not be able to read it again, and I believe a setter should always be accompanied with a getter (but not necessarily the other way around).

I'm wondering if anyone can explain the benefit of only having a setter, why Microsoft might be suggesting it in this case, and if it's really a good pattern to be following?

like image 595
Ian Avatar asked Sep 17 '12 08:09

Ian


People also ask

Can I have a setter without a getter?

But yes; it is very unusual to have a set-only property. If you expect at least some implementing classes to optionally add a getter, then it makes sense to have it as a setter instead of a SetFoo method. Interfaces don't stand alone, they represent a common subset of the operations on a family of classes.

CAN interface have getters and setters?

Technically the class that implements the interface is free to use a property or a getter. In the same way there is no way for us to specify that a property is a setter in an interface, but we can still use a setter in our class.

How do you avoid getters and setters?

Thus: you avoid getters and setters by thinking in terms of behavior, not in terms of state. Getters/setters manipulate state, from the "outside" (by doing avail = purse.

Should you use the getter method also whenever you use the setter method?

Getters and Setters Are Highly Overused All fields should be kept private, but, setters should only be kept private when it makes sense, which makes that object Immutable. Adding an unnecessary getter reveals an internal structure, which is an opportunity for increased coupling.


3 Answers

There are two scenarios I can see where this might be reasonable:

  1. it is not possible get the value, for example a password; however, I would replace that with a void SetPassword(string) method, personally
  2. the API it is designed for has no requirement to ever read the value, and it is being restricted purely to expose the minimum required API

Re my first point, a Set... method may not be ideal if the consuming API is essentially an automated mapper that assigns values to properties; in that scenario properties would indeed be preferable.

Re your "I don't see why someone should be able to set a value, and then not be able to read it again" - by the same point, however, it could be argued that someone setting the value already knows the value (they set it), so they have no requirement to do this.

But yes; it is very unusual to have a set-only property.

like image 185
Marc Gravell Avatar answered Sep 23 '22 15:09

Marc Gravell


The role of get and set in interface properties is slightly different from those in classes.

public interface IAggregationView
{
   DataTable SetSiteData { set; }
}

class AggregationViewImp : IAggregationView
{
   public DataTable SetSiteData { get; set; }  // perfectly OK
}

The interface specifies that the property should at least have a public setter. The definition and accessibility of the getter is left to the implementing class.

So if the interface contract only needs to write, get can be left open. No need to demand a public getter.

As a consequence, you cannot really specify a read-only property in interfaces either. Only 'at least read access'.

interface IFoo
{
    int Id { get;  }
}

class Foo : IFoo
{
    public int Id { get; set; }  // protected/private set is OK too
}
like image 43
Henk Holterman Avatar answered Sep 23 '22 15:09

Henk Holterman


I can imagine using it for (manual) dependency injection. A class may need to have a collaborator injected that it only uses internally. Of course one would normally choose to do this in the class' constructor, but there may be times when one would wish to change the collaborator at runtime.

like image 45
KaptajnKold Avatar answered Sep 23 '22 15:09

KaptajnKold