Jake Jake - 4 years ago 173
C# Question

C# interface with internal setters

Picture an artwork of a shape where within it is multiple levels of nested shapes. Change of properties e.g. area and length of any of those shape-within-a-shape will cause all the related properties and shapes to change.

I have a design pattern which goes like this:

I have an object graph called (for discussion sake) "NestedShapes" that has tons of properties which are related to each other, for example, "Area" and "Length". But the graph is designed to be dumb, i.e. given either value, it doesn't know how to calculate the other and will not do so.

What happens instead is that the graph can be attached to a GraphManager which takes the top level root node IRootShape in its contructor.

NestedShapes implements IRootShape which also implements INotifyPropertyChanged. GraphManager subscribes to those property changes, and runs the logic to calculate related fields and set the graph to the correct state via IRootShape.

Along with IRootShape, I have IShape, ISquare, ICircle etc. which are real C# interfaces. But the problem is for some of these properties I only want them to have setters that are private to GraphManager. I know the implementing shape can still expose a public setter, but I do not want to necessarily expose these on the UI side to be able to set the property from GraphManager. What should I do? Is base classes with internal set the way to go?

Answer Source

Let GraphManager interact with the Base classes.

Everything else interacts with the interfaces only.

Do not expose properties in your interface.

public class Circle : ICircle{
   public double Radius{

   /* blah blah ... */

public interface ICircle {
   /* No properties */

   /* blah blah ...*/
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download