Create an empty instance of the response object
Create an empty instance of the response objectCreate the SqlParameters (Note the SQL parameter names are always the same as a request object property names. The data types are also identical for primitive types, anything else is serialized and passed as an XML parameter).
Create the SqlParameters (Note the SQL parameter names are always the same as a request object property names. The data types are also identical for primitive types, anything else is serialized and passed as an XML parameter).Create the
Create theDatabaseCommandInfoDatabaseCommandInfoCall a
Call aDatabaseHelpermethod and return a result (could be scalar object, DataSet/DataTable)DatabaseHelpermethod and return a result (could be scalar object, DataSet/DataTable)Populate the response object with the result from the database helper.
Populate the response object with the result from the database helper.If a SqlException is thrown, store the error code and return the response with that code.
public class ReadAsset { private const string StoredProc = "up_Assets_ReadAsset";
private readonly IDatabaseHelper databaseHelper; public ReadAsset() { databaseHelper = new DatabaseHelper("Data Source=.; Initial Catalog=Assets; integrated security=true;"); } /// <summary> /// Constructor used to inject dependencies /// </summary> /// <param name="databaseHelper"></param> public ReadAsset(IDatabaseHelper databaseHelper) { this.databaseHelper = databaseHelper; } public ReadAssetResponse Execute(ReadAssetRequest request) { var response = new ReadAssetResponse(); var sqlParams = new[] { new SqlParameter("@TypeId", request.TypeId), new SqlParameter("@OwnershipId", request.OwnershipId), new SqlParameter("@GroupId", request.GroupId), new SqlParameter("@StatusIds", request.StatusIds.ToXml()), }; var dbCommandInfo = new DatabaseCommandInfo(StoredProc, sqlParams, new[] {"AssetInfo"}); try { var dataTable = databaseHelper.GetDataTable(dbCommandInfo); response.AssetInformation = new AssetInformation(); if (DataTableIsNotPopulated(dataTable)) return response; var row = dataTable.Rows[0]; response.AssetInformation.Id = row.GetValue<int>("Id"); response.AssetInformation.Address = row.GetValue<string>("Address"); response.AssetInformation.Uprn = row.GetValue<string>("Uprn"); response.AssetInformation.OSLocation = row.GetNullableValue<int>("OSLocation"); } catch (SqlException sqlException) { response.Errors = new List<int> {sqlException.ErrorCode}; } return response; } private static bool DataTableIsNotPopulated(DataTable dataTable) { return dataTable == null || dataTable.Rows == null || dataTable.Rows.Count != 1; }}
If a SqlException is thrown, store the error code and return the response with that code.
I haven't included the database information (i.e. table/procs, etc) as it is not relevant or required here.
public class ReadAsset{private const string StoredProc = "up_Assets_ReadAsset";private readonly IDatabaseHelper databaseHelper;public ReadAsset(){databaseHelper = new DatabaseHelper("Data Source=.; Initial Catalog=Assets; integrated security=true;");}////// Constructor used to inject dependencies//////public ReadAsset(IDatabaseHelper databaseHelper){this.databaseHelper = databaseHelper;}public ReadAssetResponse Execute(ReadAssetRequest request){var response = new ReadAssetResponse();var sqlParams = new[]{new SqlParameter("@TypeId", request.TypeId),new SqlParameter("@OwnershipId", request.OwnershipId),new SqlParameter("@GroupId", request.GroupId),new SqlParameter("@StatusIds", request.StatusIds.ToXml()),};var dbCommandInfo = new DatabaseCommandInfo(StoredProc, sqlParams, new[]{"AssetInfo"});try{var dataTable = databaseHelper.GetDataTable(dbCommandInfo);response.AssetInformation = new AssetInformation();if(DataTableIsNotPopulated(dataTable))return response;var row = dataTable.Rows[0];response.AssetInformation.Id = row.GetValue("Id");response.AssetInformation.Address = row.GetValue("Address");response.AssetInformation.Uprn = row.GetValue("Uprn");response.AssetInformation.OSLocation = row.GetNullableValue("OSLocation");}catch(SqlException sqlException){response.Errors = new List{sqlException.ErrorCode};}return response;}private static bool DataTableIsNotPopulated(DataTable dataTable){return dataTable == null || dataTable.Rows == null || dataTable.Rows.Count!= 1;}}I haven't included the database information(i.e. table/procs, etc) as it is not relevant or required here.