dispose

DI with disposable objects

纵饮孤独 提交于 2019-12-20 03:15:13
问题 Suppose my repository class looks like this: class myRepository : IDisposable{ private DataContext _context; public myRepository(DataContext context){ _context = context; } public void Dispose(){ // to do: implement dispose of DataContext } } now, I am using Unity to control the lifetime of my repository & the data context & configured the lifetimes as: DataContext - singleton myRepository - create a new instance each time Does this mean that I should not be implementing the IDisposable on

Return an object created by USING

孤者浪人 提交于 2019-12-19 17:46:22
问题 I am creating an object(obj below) in using and return that object as part of the function return.Will this cause any problem like object will be disposed before I try to use returned value in another function ? using (MyObject obj = new MyObject()) { . . . return obj; } 回答1: Will this cause any problem like object will be disposed before I try to use returned value in another function? Yes. Can you explain what you're trying to do here? This code doesn't make any sense. The whole point of

Passing an IDisposable object by reference causes an error?

丶灬走出姿态 提交于 2019-12-19 10:16:09
问题 I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject() To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference. But I am getting a compilation error that says The 'ref' argument type doesn't match parameter type In the below (simplified) code, both _Baz and _Bar implement IDisposable. So the questions are, Why am I getting this error? Is there a way to get around it? [UPDATE]

Structuremap Disposing of DataContext object

回眸只為那壹抹淺笑 提交于 2019-12-19 07:10:11
问题 I wanted to be sure if structuremap will dispose my DataContext after per request ends. Here is my setup ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>(); SelectConstructor<MyDataContext>(() => new MyDataContext()); Will structuremap auto dispose my datacontext or do i need to call Dispose manually?? 回答1: No it will not Dispose it automatically, unless you use nested containers and Dispose the container holding the context instance. It's up to the creator of the

Structuremap Disposing of DataContext object

对着背影说爱祢 提交于 2019-12-19 07:10:04
问题 I wanted to be sure if structuremap will dispose my DataContext after per request ends. Here is my setup ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>(); SelectConstructor<MyDataContext>(() => new MyDataContext()); Will structuremap auto dispose my datacontext or do i need to call Dispose manually?? 回答1: No it will not Dispose it automatically, unless you use nested containers and Dispose the container holding the context instance. It's up to the creator of the

Make using statement usable for multiple disposable objects

荒凉一梦 提交于 2019-12-19 05:09:33
问题 I have a bunch of text files in a folder, and all of them should have identical headers. In other words the first 100 lines of all files should be identical. So I wrote a function to check this condition: private static bool CheckHeaders(string folderPath, int headersCount) { var enumerators = Directory.EnumerateFiles(folderPath) .Select(f => File.ReadLines(f).GetEnumerator()) .ToArray(); //using (enumerators) //{ for (int i = 0; i < headersCount; i++) { foreach (var e in enumerators) { if (

Does calling Clear disposes the items also?

我怕爱的太早我们不能终老 提交于 2019-12-19 05:04:32
问题 Many times there is a clear method, that removes all the items from the collections, are these items disposed also. Like, toolStripMenuItem.DropDownItems.Clear(); is sufficient, or should I have to call like that: foreach (ToolStripItem item in toolStripMenuItem.DropDownItems) { toolStripMenuItem.DropDownItems.Remove(item); item.Dispose(); } Edit: Well ToolStripItem is an example not a question, for those who says Clear is enough I found another example, TabControl has also item collection

Why do HttpClient.PostAsync and PutAsync dispose the content?

。_饼干妹妹 提交于 2019-12-19 03:21:53
问题 The behavior of the HttpClient.PostAsync method is to dispose of the provided HttpContent object. There are many ways to get around this behavior including constructing a new HttpContent for each call made on the client or loading the content to a stream and changing the pointer. I'm wondering why invoking this method automatically invokes the disposal of its IDisposable parameters? As far as I'm aware this is not a common behavior in .NET It's also worth noting that this behavior is observed

Why should Dispose() be non-virtual?

我怕爱的太早我们不能终老 提交于 2019-12-18 12:06:07
问题 I'm new to C#, so apologies if this is an obvious question. In the MSDN Dispose example, the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own method. Thanks! 回答1: Typical usage is that Dispose() is overloaded, with a public, non-virtual Dispose() method, and a virtual, protected Dispose(bool). The

Disposing JFrame by clicking from an inner JPanel

天大地大妈咪最大 提交于 2019-12-18 09:33:48
问题 I'm trying to dispose my JFrame by clicking a button, located on a JPanel that is placed on the JFrame that I want to close. I tried to make a static method on the JFrame class, but ofcourse my IDE told me that wasn't going to happen. Anyone thinking of a solution? Thanks! 回答1: Try this: public class DisposeJFrame extends JFrame{ JPanel panel = new JPanel(); JButton button = new JButton("Dispose JFrame"); public DisposeJFrame(){ super(); setTitle("Hi"); panel.add(button); add(panel); pack();