.net 3.5 - Refactoring the following methods to remove duplicate code -


I have a bunch of DAO classes that are similar to one unit I was thinking that someone Can help: 1) Simplify this code 2) Stop duplicating code like this for each unit.

  public IList & lt; IUser & gt; GetAll () {IList & lt; IUser & gt; User = new list & lt; IUser & gt; (); (Var myConnection = New SqlConnection (ApplicationConfig.ConnectionString)) ({my varicommand = new SqlCommand ("sp_GetAllUsers", myConnection)) {myCommand.CommandType = CommandType.StoredProcedure; MyConnection.Open (); (Using myReader = myCommand.ExecuteReader ()) using {if (myReader! = Null) {while (myReader.Read ()) users.Add (FillDataRecord (myReader)); MyReader.Close (); }}} MyConnection.Close (); } Users return; } Public IUser GetBy (int id) {IUser user = null; {Var (in compliance) my command = new SQL command ("sp_GetUserById", myConnection) {myCommand.Parameters.Add ("@ id", system, using my myconnection = new SqlConnection (AppConfiguration.ConnectionString)). Data.SqlDbType.Int) value = ID; MyCommand.CommandType = CommandType.StoredProcedure; MyConnection.Open (); (Using myReader = myCommand.ExecuteReader ()) using {if (myReader! = Null) {while (myReader.Read ()) user = FillDataRecord (myReader); MyReader.Close (); }}} MyConnection.Close (); } Return User; } Private Static IUser FillDataRecord (IDataRecord myDataRecord) {IAddress address = faucet; If (! MyDataRecord.IsDBNull (myDataRecord.GetOrdinal ("AddressId")) address = GetAddress (myDataRecord.GetInt32 (myDataRecord.GetOrdinal ("AddressId")); IUser user = new user {id = myDataRecord.GetInt32 (myDataRecord.GetOrdinal ("ID"), LastName = myDataRecord.IsDBNull (myDataRecord.GetOrdinal ("LastName")? String: empty: myDataRecord.GetString (myDataRecord.GetOrdinal ("LastName")), first name = myDataRecord.IsDBNull (myDataRecord. GetOrdinal ("FirstName") string? Empty: myDataRecord.GetString (myDataRecord.GetOrdinal ("FirstName")), MyAddress = address}; return user;}  

Thank you in advance < / P>

I think the simplest refactoring :

  Public ILIST  IUser> GetAll (use {var myConnection = new SqlConnection (ApplicationConfig.ConnectionString)} {use (var myCommand = new SqlCommand ("sp_GetAllUsers" , MyConnection)) {return loadUserList (myConnection, myCommand); } MyConnection.Close (); // it should be used in the end if it should be) withdrawal tap; } Private IILIT & lt; IUser & gt; LoadUserList (SqlConnection myConnection, SqlCommand myCommand) {IList & lt; IUser & gt; User = new list & lt; IUser & gt; (); MyCommand.CommandType = CommandType.StoredProcedure; MyConnection.Open (); {If (myReader! = Null) {while (myReader.Read ()) users.Add (FillDataRecord (myReader)) using SqlDataReader myReader = myCommand.ExecuteReader ()); MyReader.Close (); }} Returning user; }  

If you want, you can return only the second record to return from IUser.

To make this more common, the process for processing, so each unit can be responsible for its own parsing, and the use of generic has already been mentioned.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -