Spontaneous Publicity
blogs are the new phone book

Paparazzi For Nerds

June 18, 2008 21:14 by Luke

Microspotting Being a relatively new employee at Microsoft, I have really enjoyed getting to know the culture and the wide array of people that work for Microsoft. To that end, I really enjoy the web site Microspotting which chronicles some of these interesting people. I was hooked as soon as I saw Golden Helmet and got a good laugh out of the Program Managa. Now I have to go figure out what kind of off beat thing I can do to become featured next...

BTW: I would live a free t-shirt.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Funny | Microsoft
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Hidden Gem in .net 3.5 SP1 Announcement

May 12, 2008 15:38 by Luke

There is some great stuff hidden in Time Sneath’s post announcing the WPF aspects of the .net 3.5 SPI release:

It's been a long time in coming, but we're finally adding the much-requested DataGrid control to WPF. This will ship out-of-band at first, just after we release 3.5 SP1…

...Another oft-requested control is the Office Ribbon, and I'm sure you'll be pleased to know that we're also shipping an implementation of that control, also out-of-band, before the end of the year. The ribbon will be fully implemented in WPF,

So there you have it, there is a DataGrid and a Ribbon control coming to WPF! This is really cool. Something else that is really great is it appears that the WPF team is starting to adopt the “out of band” release model that other developer tools such as Asp.net MVC are using. I think this is a huge step forward in terms of integrating customer feedback into their products.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .Net | C# | WPF
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Vote to Improve Sql Management Studio

April 29, 2008 12:49 by Luke

I rarely write feedback to product teams but today I decided that I have had enough of this Identity Specification nonsense. How often do you create tables in Sql Management Studio which have some sort of identity column which is a primary key and an auto incrementing integer. Unless I am way off base, this is probably the most common scenario when creating a new table. So why does it take so many clicks? I have to click the Primary Key button to make the column a primary key and then scroll down in the Column Properties pane and find "Identity Specification" and expand it to change the value of "Is Identity" to "Yes".  That is just way too many clicks and scrolls for me.

So I have submitted a suggestion on Microsoft connect:

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=340996

Vote for it if you agree. If you don’t agree, let me know why.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Development
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

SubSonic BlogProvider for BlogEngine.net

March 26, 2008 23:06 by Luke

One of the things I love about BlogEngine.net is that it is built with extensibility in mind. On example of this extensibility is the ability to customize the provider which it uses to retrieve and persist posts, pages, categories, etc.

xmltodatabase Currently there are two providers that are supported out of the box: The XmlProvider and the MSSQLProvider. After I moved my blog from wordpress.com to BlogEngine.net, I decided I wanted to store my posts and such in a database rather than in xml files. So I set out to get the MSSQLProvider up and running. There is not, however, any established way to convert all of your content from the XmlProvider to the MSSQLProvider. So I first attempted to create my own quick little script to select the content from one provider and insert it using the other. But this didn't work. I ran into connection management problems within the MSSQLProvider code.

SubSonic to the Rescue

Then it dawned on me: what I really wanted was an easy to use data access layer to access the Sql Server tables which I wanted to store my blog content in. Well, since I was planning on using the schema which ships with BlogEngine.net, I already had the schema created. This is the kind of thing that SubSonic is great at - generating an easy to use data access layer given a database schema.

Subsonic ships with a console application called Sonic.exe which can be used to generate the data access classes. Since the database schema in this case is fairly static, I figured I could just generate the classes once and not worry about using a BuildProvider or anything fancy like that. So, here is the command line I used to generate the classes:

sonic generate /override /server HOMIE /db BlogEngine 
/userid OMMITED /password OMMITED /out C:\code\out 
/generatedNamespace BlogEngine.Core.Providers.SubSonic /stripTableText be_

I used the stripTableText option to replace take out the 'be_' prefix from each table which is present in the Sql schema. This results in classes which are named 'Post' and 'Page' rather than 'BePost' and 'BePage'.

ssconvert

Now that I had my data access layer, it was time to write the BlogProvider.

All Your Provider Are Belong To Us

Implementing the provider was pretty straight forward. There isn't really documentation I can find about exactly how the provider should behave so I based my code of the MSSQLProvider. I will not really go over the code too much here because there is not all that much to it. Mostly, it is a matter of mapping the SubSonic generated data access objects to the BlogEngine.net types like this:

private static Post GetPost(SubSonicPost p)
{
    Post post = new Post();
    post.Id = p.PostID;
    post.Title = p.Title;
    post.Content = p.PostContent;
    post.Description = p.Description;
    post.DateCreated = p.DateCreated ?? DateTime.MinValue;
    post.DateModified = p.DateModified ?? DateTime.MinValue;
    post.Author = p.Author;
    post.IsPublished = p.IsPublished ?? false; ;
    post.IsCommentsEnabled = p.IsCommentEnabled ?? false;
    post.Raters = p.Raters ?? 0;
    post.Rating = p.Rating ?? 0;
    post.Slug = p.Slug;
    
    return post;
}

Pretty boring and repetitive code.

Configuration

In order to configure your site to use this provider you must first configure it for SubSonic and then add the SubSonic provider. First, add the SubSonic config section to your confSections in your web.config:

<section name="SubSonicService"
   type="SubSonic.SubSonicSection, SubSonic"
   requirePermission="false"
   allowDefinition="MachineToApplication"
   restartOnExternalChanges="true"/>

And here is the SubSonic section:

<SubSonicService defaultProvider="Default">
    <providers>
        <clear/>
        <add name="Default" type="SubSonic.SqlDataProvider, SubSonic" 
         connectionStringName="BlogEngine"
         generatedNamespace="BlogEngine.Core.Providers.SubSonic"
         enableTrace="false" />
    </providers>
</SubSonicService>

Lastly, you can edit the blogProvider section:

<BlogEngine>
  <blogProvider defaultProvider="SubSonicProvider">
    <providers>
      <add name="XmlBlogProvider"
       type="BlogEngine.Core.Providers.XmlBlogProvider, BlogEngine.Core"/>
      <add name="MSSQLBlogProvider"
       type="BlogEngine.Core.Providers.MSSQLBlogProvider, BlogEngine.Core"/>
      <add name="SubSonicProvider"
       type="BlogEngine.Core.Providers.SubSonicProvider, BlogEngine.Core.Providers.SubSonic"/>
    </providers>
  </blogProvider>
</BlogEngine>

One Gotcha

The one problem I ran into was that SubSonic doesn't appear to run in a medium trust environments because it uses Tracing calls which require elevated trust. This does not mix well with BlogEngine.net and running in a medium trust environment. To get around this I commented out the tracing calls from the SubSonic source code and recompiled the SubSonic.dll. This is just a workaround until I find a permanent fix. You can read this forum post for more details.

So that's it. Here is the code. Feel free to give me any feedback or questions you have.

BlogEngine.Core.Providers.SubSonic.zip

UPDATE: The problem with medium trust and SubSonic has since been fixed so I will remove the SubSonic.dll from this site and just send you to the SubSonic site for download.

kick it on DotNetKicks.com

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .Net | BlogEngine.net | C# | Development
Actions: E-mail | Permalink | Comments (16) | Comment RSSRSS comment feed

Dotnetkicks Penance

March 26, 2008 18:45 by Luke

penance I am a big fan of Dotnetkicks. I have found several interesting articles written by mostly lesser known authors (like myself) on that site. I have also received the benefits (more traffic) of getting some of my articles on the front page. As has been discussed previously, the “Dotnetkicks Effect” is very unpredictable due to the fact that there aren’t many people who regularly read the “Upcoming” section of the site. As a result, you can never really know if articles you submit to Dotnetkicks will get many views because there are so many different factors that contribute to your article being popular.

Sometimes this feeling of guilt comes over me. A little voice in my head says: “Why haven’t you visited the upcoming section of Dotnetkicks.com lately and given some kick love to your fellow unknown bloggers?” So I dutifully visit the site and read through the first 3 pages of upcoming articles to see if anything seems kick-worthy. I call this Dotnetkicks penance. Does anybody else do this? Could this possibly be the mystery contributing factor that keeps the steady flow of articles from the upcoming page to the front page? I imagine if my fellow guilty bloggers unite we could keep the signal to noise ratio on the front page at a healthy level.

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Development | Personal
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Some of My Favorite Extension Methods

March 24, 2008 11:24 by Luke

There has been some discussion about extension methods and how they should be used and when. Personally, I try to use them when it will increase the readability of my code. A common candidate for extension methods is the static helper method. These mostly take the form of something like:

public static class StringHelper
{
     public static string Truncate(string s, int maxLength);
}

And is usually consumed with something like:

string newString = StringHelper.Truncate(myString, 10);

Now, this isn't too bad, but with extension methods, the signature for this method now looks like:

public static string Truncate(this string s, int maxLength)

and is used with syntax like the following:

string newString = myString.Truncate(10);

So I hope you agree that this improves readability and understanding. And it also prevents you from sprinkling "MyHelperClass.MyMethod" calls everywhere

I thought I would share some of the extension methods that I am finding invaluable.

RenderControl

This method allows you to take and WebControl and render it as a string. This is very useful for html code generation. The method signature for the helper method is:

public static string RenderControl(WebControl control);

Since the syntax to use extension methods is slightly different than static methods, I decided to rename this method to RenderAsString:

publicstatic string RenderAsString(this WebControl control)
{
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            control.RenderControl(tw);
            return sb.ToString();
        }
    }
}

GetTableCell

This method has many overloads so you can get all of them in the attached download. I will just show one here:

public static void Add(this TableCellCollection cells,
                       string text, string cssClass, int? columnSpan)
{
    TableCell cell = new TableCell();
    cell.Text = text;
    if (!String.IsNullOrEmpty(cssClass))
        cell.CssClass = cssClass;
    if (columnSpan.HasValue)
        cell.ColumnSpan = columnSpan.Value;
    cells.Add(cell);
}

The Syntax for using this is something like:

Table table = new Table();
TableRow tr = new TableRow();
tr.Cells.Add("My Cell", "MyCssClass", 2);

Which is much better than:

tr.Cells.Add(HtmlHelper.GetTableCell("My Cell", "MyCssClass", 2));

These are just a couple of small examples of how I am finding Extension methods a great tool for making code more readable and concise. You can download the source code for both of these methods here:

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories:
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

New Server for My Blog

March 15, 2008 17:39 by Luke

Dont worry - the address and rss feed for my blog will remain the same. I have just moved from wordpress.com to hosting my own blog using BlogEngine.net. I just got too tired of the lack of customization available on wordpress.com.

I have to say, the move was much harder than I had anticipated. Getting my data out of wordpress.com and into BlogEngine.net required a bit of work. Wordpress exports your blog data in its own xml format which is not in the BlogML format that BlogEngine supports. I ended up writing some code to just parse through the wordpress xml file (using Linq to Xml of course) and insert the data straight into BlogEngine. If anybody is interested in the code, let me know and I can share it with you.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Personal
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Finding Out which Groups a User is a Member Of When Using Windows Authentication in Asp.Net

March 13, 2008 08:44 by Luke

When using Windows authentication with Asp.net, I often need to know which active directory groups a user is a member of. Now I know that you can do something like:

if (User.IsInRole("Admin"))
{
	//Give Access to Secrets
}
The problem with this is you need to know the name of the group ahead of time. And what if you are on a network where the full name of a group is not always clear. The actual group name may be "MyDomain\Admin". So I wrote up a quick way to just get a list of all the groups a user is a member of. It isn't super straight forward (as far as which types you need to cast to) so I thought I would list it out here:
public static List<string> GetGroups(RolePrincipal user)
{
	List<string> groups = new List<string>();
	WindowsIdentity identity = p.Identity as WindowsIdentity;
	foreach (IdentityReference group in identity.Groups)
	{
		NTAccount account = 
			(NTAccount)group.Translate(typeof(NTAccount));
		groups.Add(account.Value);
	}
	return groups;
}
the user of it on a web page would be something like:
List<string> groups = GetGroups(User as RolePrincipal);

Keep in mind that this is assuming you are using Windows Authentication. So the weird part of the code above is:

NTAccount account = (NTAccount)group.Translate(typeof(NTAccount));

if you do not get this step, you will just get a bunch of Active Directory IDs that won't do you much good.

Also, sorry about the long title. I just can't think of a clever title today. Maybe I should add something like "Ultimate Edition for Developers" on the end to make it extra clear.

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .Net | C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed