I got an really bad idea 🙂
When exploring extension methods in vb I suddenly thought what about making an extension method on string to execute data access code.
<Extension()> Function Execute(ByVal s As String) As Data.DataTable
'make code here to access the database ..
'read connectionstring from the .config file
End Function
Then in your website you could do something like this..
<%For Each dr In "select * from product".Execute.Rows%>
some HTML output here..
<%Next%>
I know this is NOT the way to do it, but in really simple applications, or in prototypes it would be really simple and straightforward.
Any comments?
I think you’ve answered your own question – it’s not a great idea. Extension methods are useful in many cases for the sake of convenience, but semantic inconsistency/confusion outweights that here.
What you are doing is creating an extension method that pretends to be a function (method) that acts on all strings, but actually is only meaningful in the narrow context of SQL queries on your database. This sort of mismatch of semantic scope suggests an extension method isn’t appropiate here, though a static helper method would be perfectly fine.