11-24-2010, 05:29 AM
First question solution can be found in attachment.
Code:
1) Write down any custom built C++ Class which you think is not cohesive (or low cohesive).
Public I Enumerable<Employee> GetEmployeesFromDb_LowCohesion()
{
// instantiate the links
using(SqlConnection connection = newSqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_KEY].ConnectionString))
using(SqlCommand command = new SqlCommand("spGetEmployee", connection)) // instantiate the command
{
command.CommandType = CommandType.StoredProcedure;
// try to open the links
try
{
connection.Open();
}
catch (InvalidOperationException ex)
{
// log exemption
throw;
}
catch (SqlException ex)
{
// log exemption
throw;
}
catch (Exception ex)
{
// log exception
throw;
}
// perform the reader & read the results
using(SqlDataReader reader = command.ExecuteReader())
{
Int32 m_NameOrdinal, m_DeptOrdinal;
// obtain the ordinals
try
{
m_NameOrdinal = reader.GetOrdinal(NAME);
m_DeptOrdinal = reader.GetOrdinal(DEPARTMENT);
}
catch(System.IndexOutOfRangeException ex)
{
// log exemption
throw;
}
Collection<Employee> result = new Collection<Employee>();
// interpret the results
while(reader.Read())
{
try
{
Employee emp = new Employee();
emp.Name = reader.IsDBNull(m_NameOrdinal) ? String.Empty: reader.GetString (m_NameOrdinal);
emp.Department = reader.IsDBNull (m_NameOrdinal) ? String.Empty : reader.GetString(m_DeptOrdinal);
result.Add(emp);
}
catch (IndexOutOfRangeException ex)
{
// Log the exemption
throw; // rethrow or handle gracefully here
}
catch (Exception ex)
{
// Log the universal exemption
throw; // rethrow or handle gracefully here
}
}
return result;
}
}
}
