C#:如何在不依赖依赖的情况下确保 class 完成工作?
C#: How to make sure the class finished the work, without relying on dependents?
我有一个收集字符串的 class。有一个 Commit 方法,它将字符串写入目标文件。
如果收集了一个字符串,但从未调用提交,我想将错误报告到日志文件中。
IDisposable
不会解决问题,因为家属会忘记调用它。
我实现了终结器,但出现编译器错误:
The class has a finalizer implemented in it.
Consider deriving from IDisposable, CriticalFinalizerObject or SafeHandle, instead.
我无法更改我公司的编译器设置。而且,建议的选项似乎过于复杂。我对如何实施提交验证有什么想法吗?
I have a class that collects a string. There is a method Commit, that
writes the string to a target file.
据我了解这个问题,你被太长的字符串读取所困,w/o 调用 Commit(string s) - 可能有很多原因,比如内存小(所以在加载期间不是整个字符串适合内存)或者你正在从一些连续的流中读取,...
现在取决于程序失败的地方
它会抛出异常吗? > 使用try-catch/re-throw错误。
是不是不停地读? > 给它读取时间限制,一旦超限,抛出错误或只是return你所拥有的并提交它。
public class MyClass
{
StringBuilder SB;
public string ObtainString()
{
// This is executed, but takes lot of time/times out
// Possible solution below
SB = new StringBuilder();
DateTime dt = DateTime.Now;
bool isRead = false;
while(DateTime.Now.Add(20) > dt) && !isRead)
{
string helpVar = ReadPartOfFileOrBuffer();
SB.Append(helpVar);
isRead = checkIfFileOrBufferIsRead();
};
if(!isRead) throw new Exception("Read timed out");
return SB.ToString();
}
public voic Commit(string s)
{
//This is not being executed!
}
public void Do()
{
string result = ObtainString();
Commit(result);
}
根据你的问题,我觉得是这样的:
免责声明,这只是在记事本中编写的糟糕代码,未经测试。
class CollectAndCommit : IDisposable
{
private Dictionary<string, bool> collectedStrings { get; set; }
public CollectAndCommit()
{
collectedStrings = new Dictionary<string, bool>();
}
public void Collect(string collectedString)
{
collectedStrings.Add(collectedString, false);
}
public void CommitAllUncommitedStrings()
{
foreach (var uncommitedString in collectedStrings.Where(c => c.Value == false))
{
collectedStrings[uncommitedString.Key] = Commit(uncommitedString.Key);
}
}
public bool HasUncommitedStrings()
{
return collectedStrings.Count(c => c.Value == false) > 0;
}
private bool Commit(string str)
{
try
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
File.Create(path);
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(str);
}
return true;
}
catch
{
return false;
}
}
public void Dispose()
{
if (HasUncommitedStrings())
{
throw new Exception("Uncommited Strings!");
}
}
}
我有一个收集字符串的 class。有一个 Commit 方法,它将字符串写入目标文件。
如果收集了一个字符串,但从未调用提交,我想将错误报告到日志文件中。
IDisposable
不会解决问题,因为家属会忘记调用它。
我实现了终结器,但出现编译器错误:
The class has a finalizer implemented in it.
Consider deriving from IDisposable, CriticalFinalizerObject or SafeHandle, instead.
我无法更改我公司的编译器设置。而且,建议的选项似乎过于复杂。我对如何实施提交验证有什么想法吗?
I have a class that collects a string. There is a method Commit, that writes the string to a target file.
据我了解这个问题,你被太长的字符串读取所困,w/o 调用 Commit(string s) - 可能有很多原因,比如内存小(所以在加载期间不是整个字符串适合内存)或者你正在从一些连续的流中读取,...
现在取决于程序失败的地方
它会抛出异常吗? > 使用try-catch/re-throw错误。
是不是不停地读? > 给它读取时间限制,一旦超限,抛出错误或只是return你所拥有的并提交它。
public class MyClass
{
StringBuilder SB;
public string ObtainString()
{
// This is executed, but takes lot of time/times out
// Possible solution below
SB = new StringBuilder();
DateTime dt = DateTime.Now;
bool isRead = false;
while(DateTime.Now.Add(20) > dt) && !isRead)
{
string helpVar = ReadPartOfFileOrBuffer();
SB.Append(helpVar);
isRead = checkIfFileOrBufferIsRead();
};
if(!isRead) throw new Exception("Read timed out");
return SB.ToString();
}
public voic Commit(string s)
{
//This is not being executed!
}
public void Do()
{
string result = ObtainString();
Commit(result);
}
根据你的问题,我觉得是这样的:
免责声明,这只是在记事本中编写的糟糕代码,未经测试。
class CollectAndCommit : IDisposable
{
private Dictionary<string, bool> collectedStrings { get; set; }
public CollectAndCommit()
{
collectedStrings = new Dictionary<string, bool>();
}
public void Collect(string collectedString)
{
collectedStrings.Add(collectedString, false);
}
public void CommitAllUncommitedStrings()
{
foreach (var uncommitedString in collectedStrings.Where(c => c.Value == false))
{
collectedStrings[uncommitedString.Key] = Commit(uncommitedString.Key);
}
}
public bool HasUncommitedStrings()
{
return collectedStrings.Count(c => c.Value == false) > 0;
}
private bool Commit(string str)
{
try
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
File.Create(path);
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(str);
}
return true;
}
catch
{
return false;
}
}
public void Dispose()
{
if (HasUncommitedStrings())
{
throw new Exception("Uncommited Strings!");
}
}
}