I have a class that creates several IDisposable objects, all of these objects are then passed to another 'manager' class in a 3rd party library. As I need some of the objects in later calls I kept a local field reference to the created objects so that I could access them at a later time. When I ran FxCop on the class it said that I should implement IDisposable, due to the disposable objects that I kept a reference to. My questions are:
我有一个创建多个IDisposable对象的类,然后将所有这些对象传递给第三方库中的另一个'manager'类。由于我在以后的调用中需要一些对象,因此我保留了对创建对象的本地字段引用,以便以后可以访问它们。当我在课堂上运行FxCop时,它说我应该实现IDisposable,因为我保留了引用的一次性对象。我的问题是:
- Should I implement IDisposable for my class? (or is it the manager's responsibility?)
- If so, should I only dispose the objects I kept a reference to? or should I find a way to dispose all objects I created.
我应该为我的班级实施IDisposable吗? (或者是经理的责任吗?)
如果是这样,我应该只处理我保留参考的对象吗?或者我应该找到一种处理我创建的所有对象的方法。
My code:
public class MyClass
{
ClassThatIsDisposable aReference;
}
public MyClass(ManagerClass manager)
{
ClassThatIsDisposable transient=new ClassThatIsDisposable();
manager.Add(transient);
aReference=new ClassThatIsDisposable();
manager.Add(aReference);
}
public void LaterCall()
{
areference.Method();
}
2 个解决方案
#1
2
The class that owns the object is the one that should dispose of them... It sounds like your manager is the owner, so he should dispose of the object.
拥有该对象的类是应该处理它们的类......听起来你的经理是所有者,所以他应该处置该对象。
If you're trying to avoid the FXCop warning then one option is to have MyClass request the disposable object from the manager each time is needs to use it. This way you won't have a member variable holding a reference to it. You could do this my having the Add() method return akey for the object that has been added and then use this key to retrieve the object from the manager when you need to use it.
如果您试图避免FXCop警告,那么一个选项是让MyClass每次需要使用它时从管理器请求一次性对象。这样您就不会有成员变量持有对它的引用。你可以这样做,我的Add()方法返回已添加的对象的一个键,然后使用此键在需要使用它时从管理器中检索对象。
#2
1
If you create any disposable objects, you should either dispose them yourself or make sure you're handing them off to another class which will take responsibility for that.
如果你创造了任何一次性物品,你应该自己处理它们,或者确保你把它们交给另一个对此负责的班级。
In this case, I'd say it really depends on what the manager class is going to do. If that guarantees that it will dispose of anything that is added to it, you're okay. It does look like a somewhat odd design pattern though - I can't say I've used anything similar myself. That's not to say it's necessarily wrong/bad, but it would at least be worth taking another look to see if there's any way of avoiding this slight confusion of ownership.
在这种情况下,我会说这实际上取决于经理类将要做什么。如果这可以保证它会处理添加到它的任何东西,那你就可以了。它确实看起来像一个有点奇怪的设计模式 - 我不能说我自己使用了类似的东西。这并不是说它必然是错误的/坏的,但至少值得再看看是否有任何方法可以避免这种轻微的所有权混淆。
Bear in mind that if the manager disposes the object you're holding a reference to, it's likely to be unusable afterwards.
请记住,如果经理处理您持有引用的对象,则之后可能无法使用。
#1
2
The class that owns the object is the one that should dispose of them... It sounds like your manager is the owner, so he should dispose of the object.
拥有该对象的类是应该处理它们的类......听起来你的经理是所有者,所以他应该处置该对象。
If you're trying to avoid the FXCop warning then one option is to have MyClass request the disposable object from the manager each time is needs to use it. This way you won't have a member variable holding a reference to it. You could do this my having the Add() method return akey for the object that has been added and then use this key to retrieve the object from the manager when you need to use it.
如果您试图避免FXCop警告,那么一个选项是让MyClass每次需要使用它时从管理器请求一次性对象。这样您就不会有成员变量持有对它的引用。你可以这样做,我的Add()方法返回已添加的对象的一个键,然后使用此键在需要使用它时从管理器中检索对象。
#2
1
If you create any disposable objects, you should either dispose them yourself or make sure you're handing them off to another class which will take responsibility for that.
如果你创造了任何一次性物品,你应该自己处理它们,或者确保你把它们交给另一个对此负责的班级。
In this case, I'd say it really depends on what the manager class is going to do. If that guarantees that it will dispose of anything that is added to it, you're okay. It does look like a somewhat odd design pattern though - I can't say I've used anything similar myself. That's not to say it's necessarily wrong/bad, but it would at least be worth taking another look to see if there's any way of avoiding this slight confusion of ownership.
在这种情况下,我会说这实际上取决于经理类将要做什么。如果这可以保证它会处理添加到它的任何东西,那你就可以了。它确实看起来像一个有点奇怪的设计模式 - 我不能说我自己使用了类似的东西。这并不是说它必然是错误的/坏的,但至少值得再看看是否有任何方法可以避免这种轻微的所有权混淆。
Bear in mind that if the manager disposes the object you're holding a reference to, it's likely to be unusable afterwards.
请记住,如果经理处理您持有引用的对象,则之后可能无法使用。