Wednesday, July 27, 2011
Sunday, May 1, 2011
(cautiously) Using extension methods to make (cleaner?) code.
I’ve read allot of warning as I learned about extension methods. To me they have a similar “feel” to them as macros did in C/C++. The trap that exists is the ability to make a complicated contraptions that seem really slick when coding but aren’t actually a good thing in the long run – i.e. down the road either you or someone like you will spend more time figuring out what is going on than it’s worth.
Still, I’m using them here and there. I tell myself I’ve gone down the bad road of complexity in my past experience enough to keep it under control.
But the feeling in the back of my mind is that I’m playing with fire.
Time will tell.
Maybe I’ll come back to this in a half a year and note if I was right to be cautious.
Example Scenario: You are using a BindingSource with a DataSet and a DataGridView. You want to access the row but it’s a pain to cast twice.
(DataSet1.MyDataRowType)(((DataRowView)(bindingSource[rowIndex])).Row)
I could write a private member function but it would be a one off. I could write a public static helper member function, but I’d end up with a different style of code:
MyUtility.GetRowAt<DataSet1>.MyDataRowType>(bindingSource, rowIndex)
That’s not really bad.
And the extension method version isn’t much different.
static class MyExt
{
internal static T
DataRowAtX<T>(
this BindingSource bs, int i)
where T : class
{
return ((DataRowView)bs[i]).Row as T;
}
}
However, with the extension method, my code doesn’t switch over to the “utility call” style, which to me, brings the level of indirection, and the associated complexity more the fray than:
bindingSource.DataRowAtX<DataSet1.MyDataRowType>(e.RowIndex);
Just like macros, my code is what I want it to be right now, and I got around a limitation by leveraging a language feature providing generic extensibility.
So the question is: will I regret this?
Tuesday, February 15, 2011
Code to do a simple regex extraction
I like about the above:
- one line does the match and one line does the extraction of the match and the conversion to the target type
- to me, it reads clearly
- it seems like a good use of “var” because I don’t care much that “reg” is a GroupCollection
Display Multiple Currency Symbols in C#
If you have a money amount:
decimal money = 107.04;
And know the code for a culture:
string cultureName = “en-gb”; // display currency in pounds
Create string containing a culture specific currency:
string formattedCurrency = String.Format(CultureInfo.CreateSpecificCulture(map[cur]), "{0:C}", decimal);
The table is on msdn: http://msdn.microsoft.com/en-us/library/ks7d2abt.aspx
Then you can insert that string into HTML (tested on IE 8):
Monday, February 14, 2011
Sql Server 2008 Security/Logins
I’m using windows authentication in an intranet.
The way I am configuring permissions is:
1. Add a login for an individual or group to the instance level security.
2. Set the user mappings for databases I wish to give access, assigning roles.
From: http://www.techrepublic.com/article/understanding-roles-in-sql-server-security/1061781
Predefined database roles:
- db_owner: Members have full access.
- db_accessadmin: Members can manage Windows groups and SQL Server logins.
- db_datareader: Members can read all data.
- db_datawriter: Members can add, delete, or modify data in the tables.
- db_ddladmin: Members can run dynamic-link library (DLL) statements.
- db_securityadmin: Members can modify role membership and manage permissions.
- db_bckupoperator: Members can back up the database.
- db_denydatareader: Members can’t view data within the database.
- db_denydatawriter: Members can’t change or delete data in tables or views.
Captain’s Log
This morning I added a feature to the campaign page to copy info to clipboard.
I needed to review the details surrounding how to wire a client side click to individually rendered elements of the template.
Found How to dynamically set control IDs inside a repeater template?
How does one copy to the clipboard via JavaScript?
Found (IE only) http://www.htmlgoodies.com/beyond/javascript/article.php/3458851/Click-Its-Copied.htm
My Code: Copy-To-Clipboard client side click handler.
What can I use for a copy-to-clipboard icon?
A: http://www.iconarchive.com/show/must-have-icons-by-visualpharm/Copy-icon.html
Now the page has the functional copy icon in it:
My Code: ASP.NET logic for putting a copy-to-clipboard icon after the short description of the campaign.
Issue: The feature is not fully useful until I change the ETL to bring in the entire description as opposed to the first 255 characters only. Pros and Cons?