创建一个虚拟容器,以便内部对象可以重新分配并且不会丢失? (C#)

发布于 2024-12-04 17:33:34 字数 380 浏览 0 评论 0原文

我有一个在我的一门课程中广泛使用的对象。但是,我有另一个类存储对该对象的引用,稍后可能需要重新分配它。

众所周知,如果一个类包含对存储为成员变量的对象的引用,然后重新分配它,那么该引用就会丢失,现在有两个对象。

有人可能会争论 ref 关键字的使用,但不幸的是我不能,因为我将成员变量存储在对象构造函数中,然后在类的方法之一中重新分配它。

此问题的解决方案是仅创建一个小型虚拟容器类并将其传递,然后在两个类之间共享此虚拟容器。然后,两个类都可以随意重新分配内部成员变量,并且它永远不会丢失或复制。

现在你可能会想:“看来你已经有了答案!你的问题是什么?”嗯,我没有确切的问题。我只是想知道这种事情是否已经完成。这是一种反模式吗?有更好的办法吗?重构?

感谢您的阅读。

I have an object that is used extensively in one of my classes. However, I have another class that stores a reference to that object and later may need to re-assign it.

As we all know, if a class contains a reference to an object stored as a member variable and then re-assigns it, then the reference is lost and now there are two objects.

One could argue the use of the ref keyword but unfortunately I cannot as I store the member variable in the objects constructor and then later re-assign it in one of the class's methods.

A solution to this problem would be to just create a small dummy container class and pass that around instead and then share this dummy container between the two classes. Both classes can then feel free to re-assign the inner member variable and it will never be lost or copied.

Now you may be wondering, "it seems like you already have your answer! What is your question?" Well, I don't have an exact question. I'm just wondering if this kind of thing is done. Is it an anti-pattern? Is there a better way? A refactoring?

Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2024-12-11 17:33:34

看一下 ComponentModel 架构。那里有 IServiceProvider 接口,它具有 GetService 方法获取已注册服务的实例。还有 IServiceContainer 接口,它具有附加功能用于注销现有服务和注册新服务的方法,然后可以在外部范围中使用它们。框架中已经有一个实现,ServiceContainer 类,可以使用。

class A
{
    private readonly IServiceContainer _services;
    public A(IServiceContainer services)
    {
        _services = services;
    }

    public void Call() 
    {
         var service = (ISomeService)_services.GetService(typeof(ISomeService));
         service.DoSomething();
    }

    public void ChangeService()
    {
         // set the new service instance as 
         // the ISomeService in the service container
         var newService = new SomeService();
         _services.RemoveService(typeof(ISomeService), true);
         _services.AddService(typeof(ISomeService), newService);             
    }
}

然后你可以这样做:

var svc = new ServiceContainer();
var a1 = new A(svc);
var a2 = new A(svc);
a1.ChangeService();
a1.Call(); // call the created service
a2.Call(); // the service created in a1 will be called
a2.ChangeService();
a1.Call(); // the service created in a2 will be called

Take a look at the ComponentModel architecture. There you have the IServiceProvider interface, which has the GetService method to get a instance of the registered service. There is also the IServiceContainer interface which has additional methods for unregistering existing services and registering new services, that can be then used in the outer scope. There is already an implementation in the framework, the ServiceContainer class, that can be used.

class A
{
    private readonly IServiceContainer _services;
    public A(IServiceContainer services)
    {
        _services = services;
    }

    public void Call() 
    {
         var service = (ISomeService)_services.GetService(typeof(ISomeService));
         service.DoSomething();
    }

    public void ChangeService()
    {
         // set the new service instance as 
         // the ISomeService in the service container
         var newService = new SomeService();
         _services.RemoveService(typeof(ISomeService), true);
         _services.AddService(typeof(ISomeService), newService);             
    }
}

Then you can do:

var svc = new ServiceContainer();
var a1 = new A(svc);
var a2 = new A(svc);
a1.ChangeService();
a1.Call(); // call the created service
a2.Call(); // the service created in a1 will be called
a2.ChangeService();
a1.Call(); // the service created in a2 will be called
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文