February 2004 - Posts
I always forget how to properly format a connectionstring. Whether it's the correct spelling of the parameters. Or when to declare a provider and when not to. If I need a provider, what's its name. I know. I know I can look it up on msdn or online help or better yet google for it. But there is an easier way. www.connectionstrings.com. It's a simple site that does nothing but, you guessed it, give examples of connectionstrings. It's simple to remember and the answers seem to be correct. At least the ones I am looking for.
-Mathew Nolton
It's always important to close connections and readers. Aside from it being sloppy coding not to do so. Failure to do so can prevent the release of connections back to the connection pool. Here is an example of using a reader that using an OracleConnection and OracleDataReader object. I use the “using“ statement to make sure everything is cleaned up. Instead of a “using“ statement you can also use a try/finally, but the syntax of “using“ is just so much cleaner.
try
{
using(OracleConnection oracleConn = new OracleConnection(_connectionString))
{
// open connection
try{oracleConn.Open();}
catch{throw new LogonException();}
// create the sql statement....i always prefer StringBuilder over string
StringBuilder sql = new StringBuilder();
sql.AppendFormat( "select NVL(SUM(SOME_COLUMN),0) from SOME_TABLE where COLUMN_NAME=",name );
// build the command object
OracleCommand cmd = new OracleCommand(sql.ToString(),oracleConn);
cmd.CommandType = CommandType.Text;
using(OracleDataReader reader=cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
// read to get first (and only) record.
reader.Read();
// return a new object instance. i could check for DBNull, but my sqlstatement will guarantee a value.
return reader.GetDouble(0);
}
}
}
catch( LogonException )
{
// LogonException is an exception defined in my DataTier.
// just rethrow it. it is from our internal code block
throw;
}
catch( Exception ex )
{
// DataSourceException is an exception defined in my DataTier.
throw new DataSourceException( ex );
}
Random Thoughts....It has been a couple of years (back in the Clarus Software Days) since I had to give a presentation to a crowd and yesterday I gave a presentation at the Atlanta .Net User Group Meeting about using custom attribute programming and reflection to implement data validators in the middle tiers. The spirit of the validators is similar to what ASP.Net provides. It was fun. I hope to do more in the future. Doug Turnure of MS also gave a presentation about Indigo. He does a very nice job presenting to a crowd.
It's official. I am once again excited about code generation. In the past (starting in year 2000), I wrote a product called Template{X}. It used the ASP metaphor to allow you to write ASP like code. But instead of rendering html to a browser, it rendered code to your development environment. http://www.cybral.com/solutions/templatex.htm.
Frankly after more than a year or so of making little money on it, I stopped. Now that I have regrouped and been looking at the CodeDom namespace. I am once again excited. In the past, I had to create the meta layers. I still think some of this is needed but it was always a problem decompiling binary code. With COM/DCOM you could decompile typelibraries and get the interface definitions. You could also interrogate COM objects looking for the IDispatch interface. Once you had this you could get methods/properties by callign GetIdofNames. This enabled you to also achieve intellisense in your code editors. However, with the meta information provided by .Net, life seems so much easier.
My technical life should be more interesting with a fun project to help occupy my mind and time (what little i have anyway ;)).
A friend of mine sent this to me....it's a good laugh
Klingon Software Quality Assurance
The top 12 things likely to be overheard if you had Klingon programmers working for you:
I have been reviewing code and designs lately and when you have a discrete set of values that can be grouped together, you should use enumerations. Or does it really matter? Since .Net will allow you to pass in any integer value in place of an enumeration, you must still validate their values by either executing Enum.IsDefined() or some comparison operation (e.g. switch statement) to determine valid values. As Brad Abrams points out in this blog, IsDefined is expensive...and may not work as expected. So a switch statement is the only real way to validate valid values without a performance hit.
Now I still think Enum's are valuable, but you have to admit that they are far less useful when it comes to validation. Will it require that I come up with a Validating Attribute for my architecture as I do for validating other parameter types. For example:
public void UpdateBillingOption(
[RequiredItem()][CustomerAccountNumber()]string accountNumber16,
[RequiredItem()][MakeSureEnumIsValid(typeof(eBillingOption))] eBilling Option billingOption)
{
}
Will this really work?
Or should I use the converter concept and always use my custom converter classes to convert a value and throw a validation exception if it cannot make the conversion? For example:
|
eTaskCode taskCode = (eTaskCode)TypeDescriptor.GetConverter(typeof(
eTaskCode)).ConvertFrom(eTaskCode.Task.ToString()); |
|
|
-Mathew Nolton
Brad Abrams brought up an interesting point about using Enum.IsDefined. Don't use it unless you have to. It causes the CLR to reflect upon the enumeration. There is nothing necessarily wrong with this, but if you weren't expecting the performance hit...it will be a surprise.
I don't always typically blog about other blogs...but this is one worth noting if you are doing much with enums and parameter passing.
-Mathew Nolton
If you are doing this, Stop. It will cause a stackoverflow everytime ;) Just something I saw in a code review that made me laugh. It was an honest mistake...
public class MyClass
{
public string MyProperty
{
get{return MyProperty;}
set{MyProperty=value;}
}
}
MI just isn’t natural. And I don’t mean the Mission Impossible movies. I mean Multiple Inheritance. Although undertaking a mission to get it included in the .Net framework might make it a worthy title. If we take our cues from nature and evolutionary theory, you can make the parallels to the evolution of the .Net framework. Think about it. Giraffes and Lions don’t intermix. So why should our class definitions of Giraffes and Lions? What would we create anyway? Girions? Liraffes? I say again, it’s just not natural. But what about those rare moments in history when it does happen? You know, such as the time when Zues came down from Mount Olympus to create god/human-like offspring? There are those moments, but they are rare. Furthermore, we don’t know if there are failed attempts between Zues and his concubines. We only hear about the successes...such as Hercules…..But what about those failures? We just don’t hear about them.
I digress. It’s a habit of mine. I tend to ramble as I am now doing. Again! Even though, the .Net framework doesn’t support MI and it’s not natural. I want it. I want the ability to create code that makes Herculean efforts look simple. I want to derive from System.Attribute and one of my business objects in order to create a common validator for my method parameters (Hint: This is something else I will be writing about later) without the implementing the dreaded “Containment”. Don’t get me wrong, Containment is good. Containment is nice. I just don’t want it all the time. I know. I know I can implement an interface. But I don’t want this either. It doesn’t give me implementation. I want more. I want MI. How about it Microsoft? Please!
-Mathew Nolton
I know. I know. I should know better. I should use the convert method instead.
object val=1; //<-- assign an integer value...
string s=(string)val; // <-- can you say invalid cast exception....
This is really a simple mistake and one I should not have made but it is so easy to do. Instead you should;
object val=1; //<-- assign an integer value...
string s=Convert.ToString(val); //<-- much better...
-Mathew Nolton
More Posts
Next page »