Wednesday, July 29, 2009

ASP.NET 3.5 WebConfigurationManager

ASP.NET 3.5 Web Project Connection Strings
This is just a quick sample of how to get a connection string in an ASP.NET 3.5 Web Project.
Assuming you've set up a Project Application Setting of Type (Connection String) you would have the following settings throughout.
References include
System.Configuration (c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Configuration.dll)
Web.Config includes
<configuration>
<!-- ... More Stuff ... -->
<connectionstrings>
<add name="DatabaseConnection" connectionstring="Data Source=SQLServerName;Initial Catalog=DatabaseName;Uid=SQLLogin;Pwd=pa$$w0rd;">
</connectionstrings>
<!-- ... More Stuff ... -->
</configuration>
Code Behind Includes
private sub RunStoredProcedureSample()
    Using SqlClientConnection As New SqlConnection(WebConfigurationManager.ConnectionStrings("DatabaseConnection").ConnectionString())
        If Not SqlClientConnection.State = ConnectionState.Open Then
            SqlClientConnection.Open()
        End If
        Using SqlClientSqlCommand As New SqlCommand("spDoSomthing", SqlClientConnection)
            With SqlClientSqlCommand
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .Clear
                    .AddWithValue("@Param1", Convert.ToString(Session("Something")))
                    .AddWithValue("@Param2", Something2)
                    .AddWithValue("@Param3", Convert.ToString(Session("ESomething3")))
                    .AddWithValue("@Param4", Something4)
                End With
                .ExecuteNonQuery()
            End With
        End Using
    End Using
End Sub
This has been the recommended way to deal with getting settings since .Net 2.0 but as most developers, I kept writing .NET 1.0 and/or .NET 1.1 based code even in .Net 2.0.
I know of entire .NET 2.0 certification programs instructed by individuals who have no working or academic experience actually using the .NET 2.0 framework specifications. Microsoft apparently is not concerned enough about the integrity of their certified training partners to enforce and/or police such things. Of course, if you have had the great misfortune of attempting to attend any of Microsoft’s “advanced” partner application and/or development webinars in the 2008/2009 sessions, you would have little doubt as to the lapse of quality control.
Anyway, since I cannot correct the system, maybe I can be of some assistance to others who are endeavoring to improve their own professional integrity through some small but often unaddressed basics that seem to always get lost in the discussions.
Now against the .NET 3.5 framework many of the deprecations have actually taken effect.

Monday, July 13, 2009

IIS ASP.NET Tab - No 3.x?

While preparing to publish a workaround to drop front page extensions, I ran into an interesting quandary which I found excellently explained in the following post from Rick Stahl's Web Log on March 24, 2008 @ 12:36 pm from Maui, Hawaii Where's my .NET 3.5 (on IIS), Dude? in which he explains why you wont see a ASP.NET version 3.x in IIS properties ASP.NET Tab.
I’m just thankful he is keeping notes.

Thursday, July 09, 2009

The DRY Principle

The DRY Principle…
http://en.wikipedia.org/wiki/Don't_repeat_yourself
Quote from discussions:
There is no reason that one would "impose standards based on DRY compliance."
DRY is a general principle, not a law that is usually codified and strictly enforced.
Aka “Single Point of Truth”; “Single Point of Maintenance”


Quote on Refactoring:
Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that you are allowed to copy and paste the code once, but that when the same code is replicated three times, it should be extracted into a new procedure. The rule was introduced by Martin Fowler in Refactoring[1] and attributed to Don Roberts.

Duplication in programming is a bad practice because it makes the code harder to maintain. When the rule encoded in a replicated piece of code changes, whoever maintains the code will have to change it in all places correctly. This process is error-prone and often leads to problems. If the code exists in only one place, then it can be easily changed there.


While refactoring application systems that are primarily VB6 and VB.NET 1.1 code written under the VB .NET 2.0 and 3.5 Frameworks to utilize the most efficient tools provided by these newer Frameworks, I find the greatest gains in manageability, efficiency, memory footprint, overall application size, and comprehensive extraction of business rules still comes from enforcing good practices based on fundamental principals.

Utilizing VS Code Analysis (2005 & 2008) and Code Metrics 2008 are good assistants even after the basics are covered, but one rudimentary fact remains unchanged - if you cannot get a team wide buy-in on practices (not just principles), you are wasting time and energy.

Friday, July 03, 2009

The Snail and the Rose-bush

The Snail and the Rose-bush
From:
Everyman’s Library
Edited By Ernest Rhys
Children’s Books
Fairy Tales From Hans Christian Andersen
Illustrated By The Brothers Robinson
Around a garden was a fence of hazel-bushes, and beyond that were fields and meadows, with cows and sheep; but in the centre of the garden stood a Rose-bush in full bloom. Under it lay a Snail, who had a great deal in him, according to himself. “Wait till my time comes,” said he; “I shall do a great deal more tan to yield roses, or to bear nuts, or to give milk as cows do.”
“I expect an immense deal from you,” said the Rose-bush. “May I ask when it is to come forth?”
“I shall take my time,” replied the Snail. “You are always in such a hurry with your work, that curiosity about it is never excited.”
The following year the Snail lay, almost in the same spot as formerly, in the sunshine under the Rose-bush; it was already in bud, and the buds had begun to expand into full-blown flowers, always fresh, always new. And the Snail crept half out, stretched forth its feelers, and then drew them in again.
“Everything looks just the same as last year; there is no progress to be seen anywhere. The Rose-bush is covered with roses – it will never get beyond that.”
The summer passed, the autumn passed; the Rose-bush had yielded roses and buds up to the time that the snow fell. The weather became wet and tempestuous, the Rose-bush bowed down towards the ground, the Snail crept into the earth.
A new year commenced, the Rose-bush revived, and the Snail came forth again.
“You are now only an old stick of a Rose-bush,” said he; “you must expect to wither away soon. You have given the world all that was in you. Whether that were worth much or not, is a question I have not time to take into consideration; but this is certain, that you have not done the least for your own improvement, else something very different might have been produced by you. Can you deny this? You will soon become only a bare stick. Do you understand what I say?”
“You alarm me,” cried the Rose-bush. “I never thought of this.”
“No, you have never troubled yourself with thinking much. But have you not occasionally reflected why you blossomed, and in what way you blossomed – how in one way and not in another?”
“No,” answered the Rose-bush; “I blossomed in gladness, for I could not do otherwise. The sun was so warm, the air so refreshing; I drank of the clear dew and the heavy rain; I breathed – I lived! There came up from the ground a strength to me, there came a strength from above. I experienced a degree of pleasure, always new, always great, and I was obliged to blossom. It was my life; I could not do otherwise.”
“You have had a very easy life,” remarked the Snail.
“To be sure, much has been granted to me,” said the Rose-bush, “but no more will be bestowed on me now. You have one of those meditative, deeply thinking minds, one so endowed that you will astonish the world.”
“I have by no means any such design,” said the Snail. “The world is nothing to me. What have I to do with the world? I have enough to do with myself, and enough in myself.”
“But should we not in this earth all give our best assistance to others – contribute what we can? Yes! I have only been able to give roses; but you – you who have got so much – what have you given to the world? What will you give it?”
“What have I given? What will I give? I spit upon it! It is good for noting! I have no interest in it. Produce your roses – you cannot do more than that – let the hazel bushes bear nuts, let the cows give milk! You have each of you your public; I have mine within myself. I am going into myself, and shall remain there. The world is nothing to me.”
And so the Snail withdrew into his house, and closed it up.
“What a sad pity it is!” exclaimed the Rose-bush. “I cannot creep into shelter, however much I might with it. I must always spring out, spring out into roses. The leaves fall off, and they fly away on the wind. But I saw one of the roses laid in a psalm-book belonging to the mistress of the house; another of my roses was placed on the breast of a young and beautiful girl, and another was kissed by a child’s soft lips in an ecstasy of joy. I was so charmed at all this: it was a real happiness to me – one of the pleasant remembrances of my life.”
And the Rose-bush bloomed on in innocence, while the Snail retired into his slimy house – the world was nothing to him!
Years flew on.
The Snail had returned to earth, the Rose-bush had returned to earth; also the dried rose-leaf in the psalm-book had disappeared, but new rose-bushes bloomed in the garden, and new snails were there; they crept into their houses, spitting – the world was nothing to them!
Shall we read their history too? It would not be different.