C#接口与内部setter [英] C# interface with internal setters

查看:140
本文介绍了C#接口与内部setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景
图片形状的图稿,其中包含多层嵌套形状。属性的变化,例如任何这些形状内的形状的区域和长度都会导致所有相关的属性和形状发生变化。

Background: 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.

我的设计模式如下:

我有一个对象图(称为讨论)NestedShapes,它有许多彼此相关的属性,例如Area和Length 。但图表设计为愚蠢,即给定任一值,它不知道如何计算另一个,也不会这样做。

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.

反而是图形可以附加到GraphManager,它在其构造函数中采用顶级根节点IRootShape。

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实现IRootShape,它也实现了INotifyPropertyChanged。 GraphManager订阅这些属性更改,并运行逻辑来计算相关字段并通过IRootShape将图表设置为正确的状态。

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.

问题
与IRootShape一起,我有IShape,ISquare,ICircle等,这些都是真正的C#接口。但问题是这些属性中的一些我只希望它们具有GraphManager专用的setter。我知道实现形状仍然可以暴露公共setter,但我不希望必须在UI端公开这些以便能够从GraphManager设置属性。我该怎么办?内部基类设置的方法是什么?

Problem: 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?

推荐答案

让GraphManager与Base类交互。

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{
      get;set;
   }

   /* blah blah ... */
}

public interface ICircle {
   /* No properties */

   /* blah blah ...*/
}

这篇关于C#接口与内部setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆