Monday, November 26, 2012

The State of Backbone JS

So over on the DailyJS Alex Young just posted great info on the upcoming Backbone release.

Here is a link to the original post.

To quote a few of the highlights:

There are more bug fixes and and internals, with what I hope are some optimizations.

Friday, June 1, 2012

Self restarting windows services

I recently went through an interview where the interview was running over and the interviewer was trying to kill time so he was telling me about a GDI+ handle bug in .NET. Now for those who know me and have read my published works, you know that I am a UI specialist, so my ears instantly perked up to listen to this issue.

The situation was that they were using a windows service that was creating images and printing them off or something. Anyhow since the service wasn’t a GUI app it was not constantly being started and stopped and so with time it would use up all the available handles.

The solution that this person came up with was to create another service to watch the handles in the other service and when it had to many it would start another service to restart the first service.

To clarify:

Service A creates and prints images that use up handles in GDI+

Service B watches Service A for high handle usage, on detection it starts Service C

Service C restarts Service A

Wow that’s a lot of code and work. How I would deal with it is a combination of configuration and code all in Service A.

So we have Service A keep track of the handle counts, then when we hit the thresh hold we have the service call Environment.Exit with an error code greater than 0, which seems appropriate. Then on install we configure the service to restart on error.

Voila a self restarting service, well sort of, but much less complex than the previous scenario.

Thursday, July 1, 2010

Bing Ajax Control over HTTPS or HTTP Secure

So I have not updated in awhile, why you ask? Because I have been neck deep for months on 5 large projects and one of them has been “Geo Mapping”.

 

So recently I was integrating the project into the main portal site, which runs under HTTPS. We are using the Bing API  and all I had to do was flip the URL to the Bing control from HTTP to HTTPS.

Woops, wrong there. Not entirely anyhow everything worked except that the map tiles were being pulled from HTTP still. So after hunting about for a day I found an undocumented parameter, well no documentation that I could locate.

So here are the two ways to add the Bing Ajax control to your page in HTTP or HTTPS:

HTTP:

<script src=”http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3” type="text/javascript"></script>

HTTPS:

<script src=”https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&s=1” type="text/javascript"></script>

Now on the HTTPS version notice I changed the URL to start with HTTPS, this is what pulls from the secure site over at the good Bing folks. Also notice I added the “s” parameter and set it to 1, default is 0, this tells the Bing API to get the image tiles for the map from HTTPS.

Why? you ask.

Well it seems that the API determines what protocol to use for JavaScript and other parts from the document.location.protocol, however when it comes to the map tiles it looks to a global setting that is set based on the parameter being passed.

 

Well we all make mistakes but at least I found a solution. And I hope that anyone else that runs into it will find this post. Enjoy!

Friday, July 31, 2009

ASP.NET MVC V2 Preview 1 Released

The Microsoft ASP.NET team just released the first public preview of ASP.NET MVC Version 2.  You can download it here

It includes support for:

  • Templated Helpers - allow you to automatically associate edit and display elements with data types. For example, a date picker UI element can be automatically rendered every time data of type System.DateTime is used. This is similar to Field Templates in ASP.NET Dynamic Data.
  • Areas - provide a means of dividing a large web application into multiple projects, each of which can be developed in relative isolation. This helps developers manage the complexity of building a large application by providing a way to group related controllers and views.
  • Support for Data Annotations - Data Annotations enable attaching validation logic in a central location via metadata attributes applied directly to a model class. First introduced in ASP.NET Dynamic Data, these attributes are now integrated into the default model binder and provide a metadata driven means to validating user input.

These are the big basics. However there are a number of other new teaser features in it. 

  • Default Parameter Values – Just what it says. The syntax is a little clunky but its a start in the write direction. In Visual Studio 2010 you’ll be able to do the same thing but with a less clunky syntax.
  • Binding Binary Data - ASP.NET MVC Preview 1 adds support for binding base64-encoded string values to properties of type byte[] and System.Data.Linq.Binary.  There are now two overloaded versions of Html.Hidden() that can take these data-types.  These can be useful for scenarios where you want to enable concurrency control within your application and want to roundtrip timestamp values of database rows within your forms. 

You can read more at Scott Gu’s post and Phil Haack’s MVC2 post

Tuesday, March 24, 2009

Scott Hanselman rated higher than Scott Gu

So I've been watching all the Keynotes from Mix 09. Mostly because I’m to busy and to poor to go to these types of things. Scott Hanselman gave this talk about Nerd Dinner, during the talk, well actually at the start of the talk he mentioned that if you searched on Scott, Scott Guthrie would come up as 5th and he would be at 11th place. Just having to know how true to the facts that is, and because posed with an unknown I just need to know the answer. I of course Googled it.

Hanselman actually came in 6th and Scott Guthrie came in 8th. How wacky is that. If I were to totally geek out I would say that Scott and Scott need to have a light saber battle to the death for Google rating supremacy. But I won’t do that.

UPDATE: Thinking about this more, I have to say how sick is it that doing a Google search on the name “Scott”, and considering the number of people named Scott in the world and how many times the word Scott must appear on the internet, that Scott Guthrie and Scott Hanselman are in the top 10 search results in Google. I am curious as to what methodology they use as far as SEO is concerned.

Monday, March 9, 2009

Code Contracts in C#

One of the new technologies Microsoft has coming down the pipeline for .NET 4 is Code Contracts. What are code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, post-conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation.

But what they are is so much more, they bring design by contract to .NET. Traditionally one would set up some type of argument checking to verify the validity of what was passed in, and probably return some type of invalid argument exception upon failure, like the following:


public class DummyObject
{
public DummyObject(int n)
{
if(0 < n)
throw new InvalidArgumentException("n must be greater than 0.");
}
}


This isn’t the most elegant of solutions, also it is only verified at runtime. With Code Contracts, you now have static checking of your code conditions to see if you violated the contract at compile time. As far as testability this is great. But even better is that we as developers can tailor our code, to make the compiler a little bit smarter. In the code above everything would compile just fine if we initialized a new instance of our “DummyObject” with a 0, only when we ran it would we see an error. Now if we were to rewrite this with code contracts it would look like this:



public class DummyObject
{
public DummyObject(int n)
{
Contract.Requires(0 < n);
}
}


Now with static checking turned on we would see an error at compile time telling us that we have violated the contract. This is one of the simplest examples there is a great deal more that can be done. I recommend reading the Microsoft research site and look for other articles out there.

Thursday, March 5, 2009

Microsoft MVC Digg Sample

So I recently was writing an MVC sample just to familiarize myself with the Microsoft MVC Framework. One of my favorite samples recently was one done by Scott Guthrie for Silver Light. It was a Digg sample so I wanted to make one that was similar to that one. So with no further adieu.


The source code can be downloaded here.

I will be updating it with more features as I build it out and will probably be doing posts to explain the different parts, so keep checking for new entries.

Tuesday, March 3, 2009

Syntax Highlighter

So I got an email this morning asking me about the code blocks in my posts.

Hey I noticed that your code you put in your posts has changed, can you tell me what your using to do that I really want it for my blog.

Basically I had received a number of emails stating that my code was impossible to copy and paste easily, so I started writing my own syntax highlighter and then I poked my eye out! Basically I realized that someone had already done this so why was I spending my time on it. And then I set out on my search through the search result jungle of Google.

After reviewing a ton of different methods I found one I liked the best Syntax Highlighter by Alex Gorbatchev. It’s actually very slick, it post processes the tags in your page and does the decoration after it loads via JavaScript. So I don’t have to worry about any real formatting and the like just wrap my code blocks in some simple tags and done.

Anyhow go check it out and donate to him he has done a lot of work and it is really quite good. 

Monday, March 2, 2009

Microsoft MVC Tip #1

Well here is my first tip for MVC, how nice that I actually hit a head scratcher.

So recently I was doing some Model binding in MVC, and couldn’t get the binding to work. I had a simple object:

public class SimpleObject 
{
public string TestHiddenField;
public SimpleObject
{
TestHiddenField = "2";
}
}

In my Controller I was assigning the instance of my SimpleObject to my Model:


public ActionResult Index() 
{
ViewData.Model = new SimpleObject();
return View();
}

And in my markup I had this:


<%= Html.Hidden("TestHiddenField") %> 

The markup that was produced was this:



 

Now notice the value is blank. The model should have bound the value “2” to this control and that should have been the value. But without knowing it I did something stupid. I have a public field but it seems that the Model Binder expects a property with a getter and a setter. Well at least a getter.


So to fix the problem I had to fix my SimpleObject:


public class SimpleObject 
{
public string TestHiddenField{ get; set; };
public SimpleObject
{
TestHiddenField = "2";
}
}

So in conclusion it seems that under the covers MVC Model Binder looks for properties and not fields on the object. This is interesting since I would think that it would look for both. Though it is a good practice to use properties rather than fields I just didn’t expect it.

Friday, February 27, 2009

New Blog Skin!

Yes once again I have changed the skin of my blog. This time to something a lot more practical for what I am doing with the blog. This for those that do not know is Cogitation, a Subtext skin. But it’s widely used by technical writers and bloggers. It’s supports a lot of text and a flowing layout that is quite good, and though I hate using a cookie cutter template my old template was not as good as I would have liked. So here I am I’ll probably modify it to give it my own little flare, but till then enjoy the layout and the content to follow.

Thursday, February 26, 2009

Question Mark is a Nullable Type

One of the questions I keep seeing is what is int? or DatTime? and the like?

Well in .NET 2.0 the Nullable Type was introduced and that is what those type declarations mean. So what is a Nullable type?

 

Nullable types have the following characteristics:

  • Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

  • The syntax T? is shorthand for Nullable<(Of <(T>)>), where T is a value type. The two forms are interchangeable.

  • Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; or double? d = 4.108. However, a nullable type can also be assigned the value null: int? x = null.

  • Use the Nullable<(Of <(T>)>)..::.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();

  • Use the HasValue and Value read-only properties to test for null and retrieve the value, for example if(x.HasValue) j = x.Value;

    • The HasValue property returns true if the variable contains a value, or false if it is null.

    • The Value property returns a value if one is assigned. Otherwise, a System..::.InvalidOperationException is thrown.

    • The default value for HasValue is false. The Value property has no default value.

    • You can also use the == and != operators with a nullable type, for example, if (x != null) y = x;

  • Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;

  • Nested nullable types are not allowed. The following line will not compile: Nullable<Nullable<int>> n;

 

Thats about all of it. More information can be found on MSDN

Wednesday, February 18, 2009

Blogging: Style or Content, how will you be judged?

So I spend a lot of time thinking about my blog and wondering,”Should I blog more?” And that leads me to thinking about  the style of my blog. My current style aka theme works well for my needs though there are a few quirks with it, yup look for a future redesign. But what’s more important the style or the content. Can you have poor content but such good style that the users come back and stare thinking, “It’s so beautiful!” with a little drool rolling down their chin? Can that get you through? I don’t honestly know.

 

I do notice however that among the software engineers and coders, or as I called them back in the old days, computer programmers, there seems to be a very broad scope of style and content. Some blogs have great style and great content, these are clearly the winners and will be judge mercifully by the robotic masters, they are among us now. Some have horrible style, and call themselves minimalistic blogs, I call them lazy blogs. I think if your a programmer talking about the subject of web development or whatever you should have a blog that at least puts on a bit of a show for the readers. But that aside, the content is very good Martin Fowlers blog is a good example of this. I went to his blog looked and chuckled at how sad it looked, but the content is solid, also he calls it a bliki.

 

But I wonder again for those that are not familiar with his work, would they just surf away to a new blog without looking? Anyhow I have compiled a list of some of the top programmer blogs out there. So you can look for yourself. All of these have great content, some have great style, and some if not most have both. Where do you stack up?

 

  1. Joel on Software (Joel Spolsky)
  2. Coding Horror (Jeff Atwood)
  3. Seth's Blog (Seth Godin)
  4. Paul Graham: Essays (Paul Graham)
  5. blog.pmarca.com (Marc Andreessen)
  6. Rough Type (Nicholas Carr)
  7. Scott Hanselman's Computer Zen (Scott Hanselman)
  8. Martin Fowler's Bliki (Martin Fowler)
  9. Rands in Repose (Michael Lopp)
  10. Stevey's Blog Rants (Steve Yegge)

 

Enjoy!

Tuesday, February 3, 2009

Infragistics Quince: UX Patterns Explorer

If your into patterns like I am you will most likely love this.

 

Half the problems with patterns is trying to figure out what ones you need, what ones you want, and how they all fit together. From an application architecture stand point this usually involves lots of reading and developing a large knowledge base that honestly can set your brain to overload. That is why I think the UX patterns explorer is a great tool. Being able to have a slick UI that makes for a great UX, I mean come on if your going to make a UX patterns explorer it better look cool and give a great UX, right! It gives an easy way to identify what your looking to do and then presents you with the pattern and practical examples of implementation.  I mean that’s great. So I tip my virtual hat at the Infagistics guys.

 

 image

 

If your inclined I would recommend giving it a look.

ASP.NET MVC RC Refresh

While there is much to be excited about in MVC RC1, there were two changes introduced in the RC that broke some scenarios which previously worked in the Beta.

 

So they have just released the MVC RC Refresh to address these bugs. You can read more about it over at Haacked but the important thing the bug fixes are starting to roll out. You really have to appreciate the response times of the team.

Thursday, January 29, 2009

The Double Question Mark Operator

The double question mark operator, also called the null-coalescing operator, is one of the many new and often unknown things in .NET 2008. It is a very slick operator and very useful. Microsoft states that it

 

is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

 

Its usage is as follows:

 

class NullCoalesce
{
    static int? GetNullableInt()
    {
        return null;
    }
    static string GetStringValue()
    {
        return null;
    }
    static void Main()
    {
        // ?? operator example.
        int? x = null;
        // y = x, unless x is null, in which case y = -1.
        int y = x ?? -1;
        // Assign i to return value of method, unless
        // return value is null, in which case assign
        // default value of int to i.
        int i = GetNullableInt() ?? default(int);
        string s = GetStringValue();
        // ?? also works with reference types. 
        // Display contents of s, unless s is null, 
        // in which case display "Unspecified".
        Console.WriteLine(s ?? "Unspecified");
    }
}

Monday, January 26, 2009

Windows Live Writer Blogging Made Easier

Recently I started to look at Windows 7 and I wanted to look at the new Microsoft Live Messenger. So when I downloaded and started to install everything I notice it asked if I wanted to install Windows Live Writer. Having looked at a number of blog posting apps and deciding that they were all lacking I was hesitant to install it. But I like to try new things before deciding if I like them or not so I let the installer rip.

 

To my surprise I have been more than impressed. Having worked in Microsoft Office, like many of us, I am fairly tied to the keyboard layout and hot keys, as well as I write to my blog and I like to see how my posts will look in respect to my layout and template.

 

The initial configuration asked me for a few details about my blog and some information for logging into it. Then it downloaded my template and articles and all kinds of stuff, all of which only took seconds, and I was ready to go. So I typed out a simple test and hit the preview tab and I was just stunned, staring me in the face was a perfect preview of my blog and my post. Perfectly rendered. Which if you know blogger their own preview tab doesn’t do that good of a job. I would think since I have a Blogger blog and not a Microsoft owned and operated one, that I would have sub par integration. But it seems to integrate better than the built in blogger one.

 

Another great feature is the fact that it is a rich client editor so I can do spell checking and the like with full office integration which is what I would expect.

 

It even has a tagging section at the bottom allowing me to pick the tags for the post from my existing set, or add a new one if I like.

 

One thing that is lacking is the fact that I can’t add custom tags, similar to block quote tags which are included with a simple click, I do have some custom tags for code blocks, which though it would be convenient it is no show stopper for this editor.

 

Anyway If you are an avid Blogger or contributor to the blogosphere I would recommend Windows Live Writer above all the other blog editors to date.

Friday, January 9, 2009

Zoom Your Desktop with ZoomIt

Recently I had to give a presentation in a room that just had an LCD screen mounted to the wall. The problem was that the screen was small for the size of the room and many people couldn't see the screen clearly and that made it fairly problematic.

After the presentation it came up that this was a problem and would continue to be a problem. And someone mentioned that many of the Microsoft presenters use a tool that zooms the entire desktop onto the mouse position. So I set out to find this tool and of course I should have know that it came out of the many great tools from Sysinternals, now part of Microsoft, written by Mark Russinovich.

The tool is called ZoomIt and now that I have it I will add it to my arsenal of great and useful tools.

It does more than just zoom, it also allows you to draw on your desktop which is great for highlighting things that you want to draw attention to. And it has a timer mode that displays a count down that you can set, for those all important breaks.

Thursday, November 20, 2008

Client Side Values for the CheckBoxList

I have run into the problem so many times now that I have finally, after years of waiting for Microsoft to fix it, broke down and fixed it myself.

The problem that many people run into with the CheckBoxList in todays world of Ajax is that it does not render the value attribute on the client side. So you can't access it through javascript. This is actually intentional as the attributes are removed prior to rendering, well probably more of an oversite than intentional.

Of course you have the values on the server side, but that is because they are stored in control state and rebound when the control is recreated on postback.

The following code extends the CheckBoxList and fixes the RenderItem method to render a value attribute for the checkboxes in the list. I kept the changes minimal just so I didn't have to fix data binding and viewstate, so the control works exactly the same as it always has except it renders values on the client. I hope this helps someone out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Security.Permissions;


namespace System.Web.UI.WebControls{
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.LinkDemand,
Level = AspNetHostingPermissionLevel.Minimal),
ToolboxData("<{0}:WorkingCheckBoxList runat=\"server\" />")]
public class CheckBoxListWithClientValues : CheckBoxList
{
private CheckBox _controlToRepeat;
private bool _cachedIsEnabled;
private bool _cachedRegisterEnabled;

public CheckBoxListWithClientValues()
{
this._controlToRepeat = new CheckBox();
this._controlToRepeat.EnableViewState = false;
this._controlToRepeat.ID = "0";
this.Controls.Add(this._controlToRepeat);
}

protected override void RenderItem(ListItemType itemType,
int repeatIndex,
RepeatInfo repeatInfo,
System.Web.UI.HtmlTextWriter writer)
{
if (repeatIndex == 0)
{
this._cachedIsEnabled = base.IsEnabled;
this._cachedRegisterEnabled = ((this.Page != null) && base.IsEnabled);
}

ListItem item = this.Items[repeatIndex];
this._controlToRepeat.Attributes.Clear();

if (item.Attributes.Count > 0) //has attributes
{
foreach (string str in item.Attributes.Keys)
{
this._controlToRepeat.Attributes[str] = item.Attributes[str];
}
}
this._controlToRepeat.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
this._controlToRepeat.Text = item.Text;
this._controlToRepeat.Checked = item.Selected;
this._controlToRepeat.EnableViewState = true;
this._controlToRepeat.Enabled = this._cachedIsEnabled && item.Enabled;
this._controlToRepeat.InputAttributes.Add("value", item.Value); //add the value attribute to be rendered
this._controlToRepeat.RenderControl(writer);
}
}
}


A side not here is that it still renders an HTML table just the same as it always has. Many argue that that is a bad idea, and an unordered list is better, I agree with the later, unordered lists are smaller, more managable, and how a list of anything should be rendered.

Tuesday, September 9, 2008

Thoughts on Chrome

With the release of the new Chrome browser we see a lot of chatter on the web. Every blogger, including myself, is dribbling out one comment or another about the new wonder browser, it's flaws, strengths, and it's future.

Many writers are saying that with it's new found vulnerabilities, it will be hard pressed to take enterprise share away from Microsoft and it's browser, Internet Explorer. And even though many think that this may usher in a new browsing age, or more likely shake Microsoft up enough to get them adding long over due features and improvements to Internet Explorer. I as a developer see another browser that I must test my sites against for compatibility, even though it is based on WebKit.

I think the real problem is that many people are overlooking the entirety of what is really starting to happen. And that is web browsers are about to take a step forward on the evolutionary path towards a true framework. Things have gone on to long with expanding a design that was done quickly based on a standard for displaying documents. It was never intended to do what it currently can and most of the newer functionality was put in place quickly at a rapid pace based on the consumer demand and the developer need to meet those demands.

Chrome is a new design and a step towards the next generation of browsers. But it is still a browser, with lots of improvements. Tabs running in their own process space, add faster JavaScript, thanks to V8, hidden tile bar when maximized to take full advantage of desktop real estate. It's a beautiful piece of work.

So what is the future of Chrome and the Web Browser. I don't know, only time will tell. But I think we are about to see the next incarnation of the OS debates all over again.

Welcome to "The Browser Wars"

Friday, September 5, 2008

New Blog Layout

For the few readers I have and for those who arrive from somewhere out on the internet. You may have noticed that I have a new blog layout. This is due to the fact that I am gearing it up to make large detailed posts. So I wanted to maximize the real estate of the blog to better suite my needs and my future effots to make this blog into a successful forum for presenting my ideas, thoughts, and general coding tricks.

I will probably be going through and reformatting a number of my past posts to support the new layout, as I have built it to make my life as a writer easier, the previous template required alot of editing of fonts and paragraph re-structuring to look correct.

Anyhow enjoy the new layout.