本文介绍了使用ASP.NET C编写多个if else条件的最佳实践#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想使用最佳实践编写多个if else条件。这是我的代码。 < pre> public class HomeController:Controller { // 创建Result类的对象 结果result = new Result(); // GET:Home public async 任务< ActionResult>索引(字符串键,字符串 值) { 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 ++) { // 更新记录 dbConnection.oracleCommand.CommandText = QUERY; dbConnection.oracleCommand.ExecuteNonQuery(); dbConnection.oracleCommand.CommandText = QUERY; OracleDataReader Reader2 = dbConnection.oracleCommand.ExecuteReader(); while (Reader2.Read()) { / / 获取keyname和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))) { // 检查api类型 如果((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))) { // 获取网址 url = Reader4.GetValue( 1 )的ToString(); // 检查获取或发布 if ((Reader4.GetValue( 0 )。ToString()==( GET))) { RestCall restCall = new RestCall(); await restCall.RunAsync(keyName,keyValue); } } 其他 如果(( Reader4.GetValue( 0 )。ToString()==( POST))) { // 休息一下调用post方法。; RestCall restCall = new RestCall(); await restCall.RunAsync(keyName,keyValue); } 其他 { // not get or a post; errorMessage = 错误; } } } } 其他 { // 空值。; errorMessageNull = 空值。; } } // 肥皂 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))) { // 获取网址 url = Reader4.GetValue( 1 )的ToString(); // 读取功能 if ((Reader4.GetValue( 0 )。ToString()==( READ))) { // SoapCall soapCall = new SoapCall(); // soapCall.DoSoapCall() ; } } // 创建函数 else if (( Reader4.GetValue( 0 )。ToString()==( 创建))) { // 做肥皂要求发布方法。; } else { // 不是获取或发布; errorMessage = 错误; } } } 其他 { // 不是get或post; errorMessage = 错误; } } else { // 不是获取或发布; errorMessage = 错误; } Reader.Dispose(); Reader3.Dispose(); dbConnection.CloseConnection(); } } catch (例外e) { exception = e.Message.ToString( ); } 我想在符合特定条件的情况下执行这些查询。之后我想做休息和肥皂电话。是否有任何方法来编写此代码而不使用多个if else条件。我的意思是一种更易阅读的方式。 我尝试过: 我不知道另一种方法。解决方案 你可以摆脱这样的直接嵌套 if(conditionA) { if(conditionB) { DoSomething(); } } 但是如果 则将所有条件都放在一起br /> if(conditionA&& conditionB) { DoSomething(); } 对于处理读卡器的代码,你可以把它放在它自己的函数中,作为读者通过如果(conditionA&& conditionB) { ProcessReader(Reader4); } 如果您仍然有太多的嵌套,那么重复将一些嵌套代码放入一个函数中的过程然后你打电话。 I want to write multiple if else conditions using best practices. Here is my code.<pre>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(); }I want to execute these queries if they meet certain conditions. After that I want to do rest and soap calls. Is there any method to write this code without using multiple if else conditions. I mean a more readable way.What I have tried:I did not have an idea of another method. 解决方案 You can get rid of immediate nesting like thisif (conditionA){ if (conditionB) { DoSomething(); }}but putting all conditions in the same ifif (conditionA && conditionB){ DoSomething();}For the code that processes the reader you could just put that in its own function as pass the reader in as a param.if (conditionA && conditionB){ ProcessReader(Reader4);}If you have too much nesting still then repeat the process of putting some of the nested code in a function which you then call. 这篇关于使用ASP.NET C编写多个if else条件的最佳实践#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 06:13