Interfaces with get/set  
Author Message
Koiti





PostPosted: Architecture General, Interfaces with get/set Top

Hello, I was wondering if is it a bad design issue to put get/set attributes into an interface, does anyone knows something about it

something like:

public interface IInterface
{
string Attribute1 { get; set; }
int Attribute2 { get; }
bool Attribute3 { set; }
}

and so on...

thanks a lot!



Architecture5  
 
 
MarcD





PostPosted: Architecture General, Interfaces with get/set Top

An interface is designed to ensure that an application implements a certain ability.
If you need to ensure that a certain property HAS a getter or that a certain property HAS BOTh a getter and a setter then do so.


 
 
Koiti





PostPosted: Architecture General, Interfaces with get/set Top

ok! thanks a lot! :D

 
 
RMD





PostPosted: Architecture General, Interfaces with get/set Top

Some would argue that you should avoid using properties all together. They introduce ambiguity as to what the property actually does and what it affects.

A method suggests work as well as the potential to change the state of the object.

A property gives the appearance of only changing the state of a single member of the object, yet often times does far more than that.

This leads to unexpected behavor, especially in cases where the documentation is lacking.

In your specific case, if your dead set on using properties then defining an interface that requires them is just as valid as defining one tha requires methods or events.

 
 
Arnon Rotem Gal Oz





PostPosted: Architecture General, Interfaces with get/set Top

RMD is right - you should consider if you realy need setters and getters. You can see my post on the "Open/Closed Principle"

Arnon