在 C# 中跨类强制方法命名一致性
我在一个项目中有一系列类,它们本质上都做相同的事情,但在不同的对象上。由于其中一些是在不同时间、由不同人编写的,因此命名上存在一些不一致。我想以一种不仅在当前对象中而且在将来创建的新对象中强制执行某些一致性的方式更新我的代码。我对编程的理解使我相信我需要一个基类或一个接口,但我不知道如何让它们工作。我想要的方法是:
internal BusinessObject MethodA(EntityObject entity)
internal Void MethodB(EntityContext context, BusinessObject obj, EntityObject entity)
我遇到的问题是,在每个类中,“BusinessObject”和“EntityObject”都会不同,例如在一个类中它可能是“CarObject 和 CarEntity”,在另一个类中它可能是“BusObject 和 BusEntity” ”。我仍然想要这两个方法,并且我仍然想要将它们命名为 MethodA 和 MethodB 我只想交换实现中的实际对象类型。实现本身会有所不同,因为它们使用不同的对象。
我在编译时知道对象类型,并且需要能够访问对象的属性,因此如果使用泛型,我需要将泛型强制转换为实现中的正确类型。此外,MethodA 的实现还需要创建一个“新”BusinessObject,即“BusinessObject x = new BusinessObject()”(如果这有什么不同的话)。
我尝试在方法中使用带有泛型的接口,以及带有抽象方法的基类,但我无法弄清楚如何让它们工作。
处理这个问题的最佳方法是什么?示例代码将不胜感激。
I have a series of classes in a project which all do essentially the same thing but on different objects. Due to some of them having been coded at different times and by different people there is some inconsistency in naming. I want to update my code in a manner that will enforce some consistency not only in the current objects but new ones that are created in the future. My understanding of programming leads me to believe that I need either a base class or an interface but I can't figure out how to get either to work. The methods I want to have are:
internal BusinessObject MethodA(EntityObject entity)
internal Void MethodB(EntityContext context, BusinessObject obj, EntityObject entity)
The issue I am having is that in each class the "BusinessObject" and "EntityObject" will be different e.g. in one it might be "CarObject and CarEntity" and in another it would be "BusObject and BusEntity". I still want the two methods and I still want them named MethodA and MethodB I just want to swap out the actual object types in the implementation. The implementations themselves will be different because they are using different objects.
I know the object types at compile time and need to be able to access the properties of the objects so if generics are used I need to cast the generic as the correct type in the implementation. Also the implementation of MethodA requires creating a "new" BusinessObject i.e. "BusinessObject x = new BusinessObject()" if that makes any difference.
I tried using an interface with generics in the methods and also a base class with abstract methods but I haven't been able to figure out how to get either to work.
What is the best way to handle this? Sample code would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
泛型就是这样,
所以我会按照以下方式声明一个接口(或基类,如果您喜欢这种路线):
然后在实现您的类时:
等等...
您还可以强制您的类从某些东西派生:
Generics are the way
So I would declare an interface (or base class if you prefer that route) along these lines:
Then when it comes to implementing you class:
etc...
You can also enforce that your classes descend from something:
只是为了展示带有抽象基类的解决方案,请参阅 Neils 答案以获取更多信息...
然后扩展此类:
Just to show the solution with an abstract base class, see Neils answer for more information...
Then extend this class:
实际上有三种方法:
泛型:以独立于 BusinessObject 和 EntityObject 类型的方式指定容器对象。您可以稍微扩展一下,以对泛型接受作为带有泛型约束的参数的对象类型进行限制。最大的挑战是,如果不进行转换和其他相对不安全的操作,很难实际使用 BusinessObject 和 EntityObject。
继承:在这种情况下,您将 BusinessObject 实现为基类,并要求用作参数的所有对象都从该基类派生,与 EntityObject 类似。然后,您可以将实际需要使用的任何方法放入基类中(如果需要,可以在派生类中重写它们。)
接口:这是两者之间的中间方式。在这里,您说 BusinessObject 和 EntityObject 是接口(传统上它们的名称以 I 开头,如 IBusinessObject 等)。在这里,您在接口中指定需要任何 BusinessObject 或 EntityObject 实现的方法,因此可以在容器中调用它们对象。
因此,根据您提供的信息有限,不可能说出哪个是最合适的。然而,这些是您的三个基本选择。
There are really three approaches:
Generics: Where you specify container object in such a way as it is independent of the types of BusinessObject and EntityObject. You can extend that a little to place restrictions on the types of objects that the generic accepts as a parameter with generic constraints. The big challenge is that it is difficult to actually use BusinessObject and EntityObject without casting and other relatively unsafe operations.
Inheritance: In this case, you implement BusinessObject as a base class, and require that all objects used as a parameter be derived from that, and similarly with EntityObject. Then you can put any methods that you actually need to use in the base class (and if necessary override them in the derived classes.)
Interfaces: This is a half way between the two. Here you say that BusinessObject and EntityObject are interfaces (tradtionally their names will begin with I, as in IBusinessObject etc.) Here you specify in the interface the methods that you require any BusinessObject or EntityObject to implement, and can consequently call them in your container object.
So with the limited information you give it is impossible to say which is most appropriate. However, those are your three basic choices.