Click here to Skip to main content
15,850,298 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: How this can be done in Atomic way? Pin
CodingYoshi11-Mar-09 19:27
CodingYoshi11-Mar-09 19:27 
GeneralRe: How this can be done in Atomic way? Pin
samdevloper5-Mar-09 3:50
samdevloper5-Mar-09 3:50 
AnswerRe: How this can be done in Atomic way? Pin
samdevloper9-Mar-09 3:37
samdevloper9-Mar-09 3:37 
AnswerRe: How this can be done in Atomic way? Pin
samdevloper9-Mar-09 3:38
samdevloper9-Mar-09 3:38 
GeneralRe: How this can be done in Atomic way? Pin
samdevloper5-Mar-09 3:48
samdevloper5-Mar-09 3:48 
QuestionInterprocess communication Pin
carlop()3-Dec-08 23:07
carlop()3-Dec-08 23:07 
AnswerRe: Interprocess communication Pin
Eddy Vluggen16-Dec-08 23:22
professionalEddy Vluggen16-Dec-08 23:22 
QuestionAssertion vs. Exception Pin
CodingYoshi3-Dec-08 12:32
CodingYoshi3-Dec-08 12:32 
This is a simple class I am building to abstract some functionality. Pay attention to my use of assertions.

///
/// Encapsulates execution of queries.
///

public class QueryExecutor
{
/// summary
/// Returns a DataTable object populated using the specified parameters.
/// summary
/// param name="connectionString" // the connection string to use
/// param name="queryType" //
/// param name="queryText"
/// returns
public static DataTable GetTable(string connectionString, CommandType queryType, string queryText)
{
Trace.Assert(queryText != null, "QueryExecutor::GetData\nQuery must not be null.");
Trace.Assert(queryText != string.Empty, "QueryExecutor::GetData\nQuery must not be empty.");
Trace.Assert(connectionString != null, "QueryExecutor::GetData\nConnection must not be null.");
Trace.Assert(connectionString != string.Empty, "QueryExecutor::GetData\nConnection must not be empty.");

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand query = connection.CreateCommand();
query.CommandType = queryType;
query.CommandText = queryText;

SqlDataAdapter adapter = new SqlDataAdapter(query.CommandText, connection);
DataTable table = new DataTable();

try
{
adapter.Fill(table);
}
catch (SqlException)
{
ReleaseResources(adapter, connection);

throw;
}
catch (Exception)
{
ReleaseResources(adapter, connection);

throw;
}

return table;
}

private static void ReleaseResources(SqlDataAdapter adapter, SqlConnection connection)
{
adapter.Dispose();
if (connection.State == ConnectionState.Open)
connection.Close();
}
}

I am trying to fully understand the difference between assertions and exceptions. According to readings I have read, I have come to conclusion that assertions are used for errors which should never occur and thus programmers can fix it, and for class invariances. In other words it warns the programmer and helps the programmer know that the code is actually doing what it was intended to do. Exception on the other hand can not be fully avoided but they can be anticipated and gives the opportunity to deal with it somehow during runtime. For example, if the application is trying to read a file from a path provided by the user and the file does not exist, the programmer can not really do anything but throw exception and application will handle it. Then application will tell the user that file does not exist.

My questions now are:
1. In my class above, according to my understanding which I explained above, the programmer should give me a connection string at least, therefore, I am asserting that connection string can not be empty or null. This is an error which should never occur so I am using assertion instead of throwing exception. Is this ok? Wow...I just came up with a thought (funny how when you are asking a question you come up with more questions): What if the caller is reading the connection string from a file and sending it over, now I am not giving the caller the opportunity to handle the error and try again or something. But still the caller should at least verify that the string is not empty or null.

2. Another question is why should I use Trace and not Debug apart from the known fact that Trace is bundled up with release versions but Debug is not.

3. What else is wrong with my style of coding? I know it needs more work to handle TableDirect or simple query.

4. Any other suggestions?

Thanks,
AnswerRe: Assertion vs. Exception Pin
Mark Churchill3-Dec-08 15:14
Mark Churchill3-Dec-08 15:14 
GeneralRe: Assertion vs. Exception Pin
CodingYoshi3-Dec-08 18:35
CodingYoshi3-Dec-08 18:35 
GeneralRe: Assertion vs. Exception Pin
Mark Churchill3-Dec-08 19:13
Mark Churchill3-Dec-08 19:13 
QuestionApplications, patterns and technikes based on behavior analysis. Pin
Ahmed Charfeddine3-Dec-08 1:10
Ahmed Charfeddine3-Dec-08 1:10 
QuestionReleasing .Net programs Pin
Tommy4U28-Nov-08 6:27
Tommy4U28-Nov-08 6:27 
AnswerRe: Releasing .Net programs Pin
Mark Churchill30-Nov-08 15:02
Mark Churchill30-Nov-08 15:02 
GeneralWow, what ever Pin
led mike1-Dec-08 6:22
led mike1-Dec-08 6:22 
GeneralRe: Wow, what ever Pin
Paul Conrad1-Dec-08 7:01
professionalPaul Conrad1-Dec-08 7:01 
GeneralRe: Wow, what ever Pin
led mike1-Dec-08 8:54
led mike1-Dec-08 8:54 
GeneralRe: Wow, what ever Pin
Paul Conrad1-Dec-08 8:57
professionalPaul Conrad1-Dec-08 8:57 
GeneralRe: Wow, what ever Pin
led mike1-Dec-08 9:01
led mike1-Dec-08 9:01 
GeneralRe: Wow, what ever Pin
Paul Conrad1-Dec-08 9:04
professionalPaul Conrad1-Dec-08 9:04 
AnswerRe: Releasing .Net programs Pin
Paul Conrad1-Dec-08 7:03
professionalPaul Conrad1-Dec-08 7:03 
QuestionDatabase transaction boundaries in three tier Pin
AliasElias27-Nov-08 3:32
AliasElias27-Nov-08 3:32 
AnswerRe: Database transaction boundaries in three tier Pin
Joe DiNatale1-Dec-08 6:58
Joe DiNatale1-Dec-08 6:58 
GeneralRe: Database transaction boundaries in three tier Pin
AliasElias2-Dec-08 2:47
AliasElias2-Dec-08 2:47 
AnswerRe: Database transaction boundaries in three tier Pin
CodingYoshi3-Dec-08 19:28
CodingYoshi3-Dec-08 19:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.