从子对象访问父对象数据的最佳实践 [英] Best practice for accessing Parent object data from child objects

查看:73
本文介绍了从子对象访问父对象数据的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的问题措辞正确。如果我错了请纠正我。



我有一个名为Device的Class对象。它包含几个属性和方法。它还包含其他几个名为Service1,Service2和Service3的类。这些类还有几个属性和方法。



我的问题是,从一个ServiceX类中访问Device类中的属性的最佳实践是什么。



这是我的例子



I hope my question is worded correctly. Please correct me if I am wrong.

I have a Class object called "Device". It contains several properties and methods. It also contains several other classes called Service1, Service2, and Service3. These classes also have several properties and methods.

My question is, what is the best practice to access the properties in the Device class from inside one of the ServiceX classes.

Here is my example

public class Device
{
    public string Name { get; set; }
    public string IPAddress { get; set; }
    public string Port { get; set; }
    public Service1 service1 = new Service1();
    public Service2 service1 = new Service2();
    public Service3 service1 = new Service3();

}

public class Service1
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(string command)
    {
        string URL = @"http://" + Device.IPAddress + ":" + Device.Port + this.controlUrl
    }
}

public class Service2
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(string command)
    {
        string URL = @"http://" + Device.IPAddress + ":" + Device.Port + this.controlUrl
    }
}

public class Service3
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(string command)
    {
        string URL = @"http://" + Device.IPAddress + ":" + Device.Port + this.controlUrl
    }
}



所以,如果我使用这是我的应用程序,


So, if I use this is my application,

Device.Service2.executethis("VolumeUp");



这个或课程根本不起作用。



我通过发送父对象来解决这个问题,如下所示:




This or course does not work at all.

I have over come this by sending the parent object with it, like this:

public class Device
{
    public string Name { get; set; }
    public string IPAddress { get; set; }
    public string Port { get; set; }
    public Service1 service1 = new Service1();
    public Service2 service1 = new Service2();
    public Service3 service1 = new Service3();

}

public class Service1
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(Device parent,string command)
    {
        string URL = @"http://" + parent.IPAddress + ":" + parent.Port + this.controlUrl
    }
}

public class Service2
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(Device parent,string command)
    {
        string URL = @"http://" + parent.IPAddress + ":" + parent.Port + this.controlUrl
    }
}

public class Service3
{
    public string Identifier { get; set; }
    public string controlUrl { get; set; }

    public void executethis(Device parent, string command)
    {
        string URL = @"http://" + parent.IPAddress + ":" + parent.Port + this.controlUrl
    }
}



现在我用它:




now I use this:

Device.Service2.executethis(Device, "VolumeUp");





但我确定这不是正确的方法。



But I'm sure this is not the correct way.

推荐答案

首先,我们所有的考虑只适用于对象树,而不是任意对象图



我没有看到具体问题。如果您或多或少地从子项导航到父项,最佳实践将在树的所有节点中具有 Parent 引用,而不会被排除。



一个很大的潜在问题是多态,因此问题是:什么类型应该是父类型?有些子节点可能由两种或更多种不同的父类型组成。用动态演员(向下演员)打破OOP是一件令人讨厌的事情。但在您的情况下,您只有一个设备类型。所以,这里真的没有什么可讨论的。



-SA
First of all, all our consideration only applicable to the trees of objects, not to arbitrary object graphs.

I don't see a specific problem. If you navigate from children up to the parent more or less frequently, the "best practice" would be having Parent references in all nodes of the tree, without exclusion.

One big potential problem is polymorphism, and hence the question: what type should be the type of Parent? It is possible that some child node can be composed in two or more difference parent types. Breaking OOP with dynamic cast (down-casting) is a kind of nasty thing. But in your case, you have only one Device type. So, there is really nothing to discuss here.

—SA


除了解决方案1:



你可以:



1)在你的中引入一个字段Parent服务 - 班; Device 的类型(或者,考虑到Sergey对多态性的评论,也许是 Device 实现的接口)。



2)不要在类体中的设备中初始化服务 - 实例的字段,而是在构造函数中初始化。



3)向服务类的构造函数引入一个参数;类型设备(或该接口)并在设备的构造函数中以 - 关键字的形式提供它时初始化字段服务 - 实例。



如果您不能在这里关注我,请询问。



无关建议:

- 避免公共设置者(对于属性),这里:名称,IP地址,端口。使它们受到保护或私密。

- 将设备中的服务 - 实例的字段更改为属性(同样,使用非公共设置者) 。

- 使用C#命名规则:所有类成员的PascalCasing(以大写字母开头):字段,属性,方法。 camelCasing仅限本地标识符。





编辑:接受戴夫的回答,我认为可以感觉有点改变你的设计。因为:

In addition to solution 1:

You could:

1) Introduce a field "Parent" in your Service-classes; of type Device (or, bearing Sergey's comment on polymorphism in mind, maybe rather an interface that Device implements).

2) Don't initialize the fields for the Service-instances in Device in the class-body but instead in the constructor.

3) Introduce an argument to the constructors of the service-classes; of type Device (or that interface) and provide it in form of the this-keyword in the constructor of Device when initializing the fields for the Service-instances.

If you can't follow me here, please ask.

Unrelated suggestions:
- Avoid public setters (for properties), here: Name, IPAddress, Port. Make them protected or private.
- Change the fields for the Service-instances in Device to properties (again, with non-public setters).
- Use C# naming rules: PascalCasing (starting with a capital) for all class members: Fields, Properties, Methods. camelCasing only for local identifiers.


edit: Picking up on Dave's answer, I think it could make sense to change your design a bit. Because this:
Device.Service2.executethis("VolumeUp");



..看起来有点可疑。基本上,这是您正在使用的服务,而不是设备。服务正在使用设备。但是为什么服务然后是设备的成员?如果您翻转它,使设备成为服务的成员,它将如下所示:


..looks a bit fishy. Essentially it's the Service here that you're using, not the Device. And the Service is using the Device. But why is the Service then a member of Device? If you flipped this around, making Device a member of Service, it would look like this:

Device device1 = new Device(name, ipaddress, port);
Service2 service2 = new Service2(device1);
service2.executethis("VolumeUp");



..并且您不必从其子对象访问父成员。


..and you wouldn't have to access a parent's members from its child objects.


最佳做法是你不要!



子对象不应该关心它的父母数据。子对象应该关注的范围是它自己的数据,就是这样。没别了。
Best practice is YOU DON'T!

The child object should not care about it's parents data AT ALL. The scope the child object should be concerned with is it's own data and that's it. Nothing else.


这篇关于从子对象访问父对象数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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