[英]“Consolidate” all method calls into one?
Let's say I have several classes that essentially do the same thing: process a string and then insert values to a respective table. 假设我有几个类在本质上做同样的事情:处理一个字符串,然后将值插入到相应的表中。 So essentially, it's a SWICH...CASE statement that looks like the following.
因此,从本质上讲,这是一个SWICH ... CASE语句,如下所示。 The only similarity between all the classes is that the all have a "ProcessString" method.
所有类之间的唯一相似之处是所有类都有一个“ ProcessString”方法。
Now I would like to add some error handling to all these method calls. 现在,我想为所有这些方法调用添加一些错误处理。 I can try...catch all the calls, but I was wondering if there's a way to somehow consolidate all these calls, so the that I can call one "ProcessString" at the end of the switch, but that applies to its respective class (sort of like setting a variable to the class name?).
我可以尝试...捕获所有调用,但是我想知道是否有一种方法可以合并所有这些调用,以便可以在开关末尾调用一个“ ProcessString”,但这适用于其各自的类(有点像为类名设置变量?)。 That way I can add exception handling to just one call and possibly use reflection to get the class and method being invoked.
这样,我可以将异常处理添加到仅一个调用中,并可能使用反射来获取要调用的类和方法。
switch (strKeyword)
{
case "KPI_teachers":
Teachers.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
case "KPI_students":
Students.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
case "KPI_classrooms":
Classrooms.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
}
Any help is appreciated. 任何帮助表示赞赏。 Thanks.
谢谢。
It's not necessary to consolidate the switch
into a single statement just to handle exceptions, but it's a good exercise anyway. 不必为了处理异常而将
switch
合并为单个语句,但是无论如何这都是一个好习惯。
First of all, you need to give the types backing your properties (I assume they are properties, right?) some common ground from the viewpoint of the type system. 首先,从类型系统的角度来看,您需要为支持属性的类型(我假设它们是属性,对吗?)赋予一些共同点。 That means they need to implement the same interface.
这意味着他们需要实现相同的接口。 Extrapolating from your code, that interface could be
从您的代码推断,该接口可能是
interface IStringProcessor // a bad name, but all of this is rather abstract
{
// Those parameter names are highly suspicious -- is string really the
// correct type for something called "processDate"?
void ProcessString(string line, string keyword,
string processDate, string processHour);
}
so if you have a property Teachers
of type TeacherType
, you need to make TeacherType
implement IStringProcessor
. 因此,如果您具有类型为
TeacherType
Teachers
属性,则需要使TeacherType
实现IStringProcessor
。 The same goes for the other two. 其他两个也一样。
Then you want to create a mechanism that maps from a string to an IStringProcessor
. 然后,您想创建一个从字符串映射到
IStringProcessor
。 That could be some kind of IDictionary
, but let's keep it simple for now and make it a method. 那可能是某种
IDictionary
,但是现在让我们保持简单并使其成为一种方法。
private IStringProcessor GetProcessor(string name)
{
switch (name)
{
case "KPI_Teachers": return Teachers;
case "KPI_Students": return Students;
case "KPI_Classrooms": return Classrooms;
default: throw new ArgumentException("blah blah");
}
}
You now have all the machinery in place to go single-statement: 现在,您已经具备了执行单语句的所有机制:
// The bad naming tradition continues here -- lose the "str" prefix.
// If you forget what `keyword` is and try to do something inappropriate with it
// the compiler will be happy to chastise you.
GetProcessor(strKeyword).processString(...);
This may be way off but: 这可能还很遥远,但是:
try
{
case "KPI_teachers":
Teachers.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
case "KPI_students":
Students.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
case "KPI_classrooms":
Classrooms.processString(strLine, strKeyword, strProcessDate, strProcessHour);
break;
}
catch (Exception Ex)
{}
Finally {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.