我是编程新手。我想使用最佳实践编写多个if else条件。这是我的代码。
public class HomeController : Controller
{
//Creating object of Result class
Result result = new Result();
// GET: Home
public async Task<ActionResult> Index(string key, string value)
{
DBConnection dbConnection = new DBConnection();
dbConnection.MakeConnection();
try
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader = dbConnection.oracleCommand.ExecuteReader();
Reader.Read();
count = Reader.GetInt16(0);
for (int i = 0; i < count; i++)
{
//Updating records
dbConnection.oracleCommand.CommandText = "QUERY";
dbConnection.oracleCommand.ExecuteNonQuery();
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader2 = dbConnection.oracleCommand.ExecuteReader();
while (Reader2.Read())
{
//Getting keyname and keyvalue
keyName = (Reader2.GetValue(1)).ToString();
keyValue = (Reader2.GetValue(2)).ToString();
}
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader3 = dbConnection.oracleCommand.ExecuteReader();
while (Reader3.Read())
{
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
//Checking api type
if ((Reader3.GetValue(1).ToString() == ("REST")))
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();
while (Reader4.Read())
{
if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
{
//Getting url
url = Reader4.GetValue(1).ToString();
//Checking whether a get or a post
if ((Reader4.GetValue(0).ToString() == ("GET")))
{
RestCall restCall = new RestCall();
await restCall.RunAsync(keyName, keyValue);
}
}
else if ((Reader4.GetValue(0).ToString() == ("POST")))
{
//"Do a rest call for post method.";
RestCall restCall = new RestCall();
await restCall.RunAsync(keyName, keyValue);
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
}
}
else
{
//"A null value.";
errorMessageNull = "A null value.";
}
}
//Soap
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
if ((Reader3.GetValue(1).ToString() == ("SOAP")))
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();
while (Reader4.Read())
{
if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
{
//Getting url
url = Reader4.GetValue(1).ToString();
//Read function
if ((Reader4.GetValue(0).ToString() == ("READ")))
{
//SoapCall soapCall = new SoapCall();
//soapCall.DoSoapCall();
}
}
//Create function
else if ((Reader4.GetValue(0).ToString() == ("CREATE")))
{
//"Do a soap call for post method.";
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
Reader.Dispose();
Reader3.Dispose();
dbConnection.CloseConnection();
}
}
catch (Exception e)
{
exception = e.Message.ToString();
}
如果符合某些条件,我想执行这些查询。之后我想做休息和肥皂电话。是否有任何方法来编写此代码而不使用多个if else条件。我的意思是更具可读性。
答案 0 :(得分:2)
如果if语句中有 ONLY ONE if语句(甚至不是else
子句),那么你可以使用{{1} 组合这两个语句(和)。例如,
&&
可以合并为
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
//Checking api type
if ((Reader3.GetValue(1).ToString() == ("REST")))
{
如果你正在检查同一变量的不同条件,你可以使用if(!Reader3.GetValue(1).Equals(System.DBNull.Value) && Reader3.GetValue(1).ToString() == ("REST"))
switch
除此之外,多个if语句是不可避免的!
switch(Reader3.GetValue(1).ToString())
{
case "REST":
//Do something.
break; //This is important!
case "SOAP":
//Do something!
break;
case "CREATE":
//I SAY DO SOMETHING!!!
break;
default: //This is the "else clause"
//Are you going to do something or not!?
break;
}
祝你好运!祝你好运!