Just yesterday, I uploaded an application that I had created to Mosso. Some effort had gone into cleaning up the URL’s so that they would be search engine friendly. Cleaning the URL’s up also allowed the site to work effectively with or without AJAX enabled.
The module that I decided to use was urlrewriter.net (this was not by choice. I would have liked to have been able to use the new IIS 7 module recently released, but that would have meant Mosso would have put it up on their servers. No, that would be too simple to do!). It is simple, clean and easy to understand. I followed the instructions provided by ScottGu on his post here under approach 3. Nice and simple. Well so I thought…

I tested out my application on my localhost using Vista and all was running smoothly. Next I uploaded it to Mosso, and surprise surprise, I get a 404 Not Found when I try and access one of my clean url’s.

So I double checked to make sure that I had not done anything wrong. Nope, all was well and all paths were easy to understand and I didn’t do any fancy RegExp that could be throwing things off. So I looked on Google to see if anyone else had come accross the same problem. Well to my surprise, with all amounts of keywords, I didn’t find anyone who had written the answer for me (I am sure that after I post this that I will find one).

So, I figured, I should go to Mosso and explain to them that the example that they had provided (because they have yet to implement the IIS 7 module) via ScottGu’s website was not working on their servers. After over an hour online with their support I was still no closer to an answer. So they passed it on to their “admin” to try and resolve the problem. The next morning, the problem had indeed been resolved.

Their response via email was:

It looks like the application pool needed to be set to integrated.
Please let us know if you are still experiencing trouble. Have a great day!

Well, that is clearly something that was not possible via their management interface. So for anyone experiencing this problem, it is not you, it is them. Simply ask support to fix up the damn application pool and you will be on your way.

I have recently been studying design patterns in the hope to improve my code not only from a readability standpoint but from a functional perspective as well. I figured the best way to learn something properly is to write about it. So here I go on the Observer Pattern.

I found a simple yet concise definition on data & object factory:

Define a one-to-many dependency between objects so that when one object changes state, all its dependants are notified an updated automatically. [link]

I then took this and thought it was best to come up with an analogy to remember this by more simply. So here I go (Maybe it won’t work, or maybe you have a simpler idea. Please let me know):

Twitter is a good example of this pattern. When you follow someone you are a dependant on that person (and visa versa when someone is following you). When the person being followed updates their status, feed, micro-blog (or whatever name they have for it), then all dependants, ie: those who are following, are also notified of the update and have the opportunity to react to it.

That was as simple as I can get. Basic principle found in every day life really. We could also liken it to a blog that updates its subscribers. From that we need to delve into the parts of the pattern to understand it and write code around the pattern. There are only a few things that need to be remembered.

  • Subject (the person being followed).
  • Observer (the person following).

As with everything in programming we try and abstract away as much as possible to understand it better and to improve reuse. So there are going to be interfaces and abstract classes in this pattern when used properly (IMHO). Lets start by creating a simple Subject Class which contains a List or ArrayList of Observers.

Subject Class

   1:  public abstract class Subject {
   2:     List<IObserver> observers { get; set; }
   3:  }

This looks a bit bare. Lets flesh it out with the next step. The logical thing is to realise that once we make a tweet we want to be able to notify the follower that we have updated our status. Ok, that’s fine, but there are two other things that we might want to do. We might want to let someone else follow us (attach), or we might want to block someone who is already following us (detach). So we need to add some code to show that is possible.

   1:  public void Attach() { }
   2:  public void Detach() { }
   3:  public void Notify() { }

Super easy. Now we just need to add some simple code to these methods to make them do something.

   1:      public abstract class Subject {
   2:          List<IObserver> observers { get; set; }
   3:          public Subject() {
   4:              observers = new List<IObserver>();
   5:          }
   6:          public void Attach(IObserver observer) {
   7:              observers.Add(observer);
   8:          }
   9:          public void Detach(IObserver observer) {
  10:              observers.Remove(observer);
  11:          }
  12:          public void Notify() { 
  13:              foreach(IObserver observer in observers){
  14:                  observer.Update();
  15:              }
  16:          }
  17:      }

One thing that I am forgetting is the IObserver interface. It is completely simple and contains one method:

   1:  public interface IObserver {
   2:     void Update();
   3:  }

Well, now that we have our abstract Subject class and our IObserver interface we are ready to implement the pattern into something a little more concrete. So let’s go ahead and do that now. Let’s create a simple Concrete Subject class:

   1:  public class OurSubject<T> : Subject {
   2:     public T SubjectState { get; set; }
   3:  }

That is as simple as I can find. Then we move onto implementing the IObserver interface. Lets work with that:

   1:      public class OurObserver<T> : IObserver {
   2:          public OurSubject<T> Subject { get; set; }
   3:          public T _observerState;
   4:   
   5:          public OurObserver(OurSubject<T> subject){
   6:              Subject = subject;
   7:          }
   8:          public void Update() {
   9:              _observerState = Subject.SubjectState;
  10:          }
  11:      }

So really all that is happening there is the OurObserver is being updated every time that OurSubject changes state. Lets run the below code (with a small mod) and see what we can see:

   1:  OurSubject<string> subject = new OurSubject<string>();
   2:  subject.Attach(new OurObserver<string>(subject));
   3:  subject.Attach(new OurObserver<string>(subject));
   4:  subject.SubjectState = "Changing state";
   5:  subject.Notify();

Our output is as follows:

image

Where the code at the end is the hashcode of the object. So we can see that it is clearly working right the way that we want it to.

Lets do something a little more along the lines of the analogy that we used before hand. Lets create a Twitterer (subject, poster) and a Twitteree (follower) and see what we can see. Using the abstract Subject class and the IObserver interface we can do the following (it is not very pretty and could be vastly improved):

   1:     public class Twitterer : Subject {
   2:          private string _currentTweet;
   3:          public string CurrentTweet {
   4:              get { return _currentTweet; }
   5:              set { _currentTweet = value; Notify(); } 
   6:          }
   7:          public void Tweet(string tweet) {
   8:              CurrentTweet = tweet;
   9:          }
  10:      }
  11:   
  12:      public class Twitteree : IObserver {
  13:          public Twitterer OurHero { get; set; }
  14:          public string myName { get; set; }
  15:          public Twitteree(string name) {
  16:              this.myName = name;
  17:          }
  18:          public void Update() {
  19:              Console.WriteLine("Twitteree {0} received a tweet", this.myName);
  20:          }
  21:      }

Then when we run it we get:

image

This is the most basic of introductions that I can do and as my understanding increases of design patterns I am sure that more insightful posts will follow.

For reference purposes please see:

Recently I went through the trouble of trying to get a LinqDataSource to bind an "ALL" into a dropdownlist. This was necessary because I was using my DropDownList to filter my GridView. I took the Categories table from the Northwind Database for my sample.

I found an easy way to do it and then by mistake I found an even more surprising easy way to do it. I shall start with the more legitimate way (at least the one that makes some sense to me).

First things first, add a LinqDataSource to your page and a simple DropDownList. Then bind the DropDownList to the LinqDataSource.  Then add an event to the DataBound event of the DropDownList. Then simple add the following code:

   1:  protected void DropDownList1_DataBound(object sender, EventArgs e) {
   2:     ((DropDownList)sender).Items.Insert(0, new ListItem("All", "-1", true));
   3:  }

Then that will allow an item to be inserted into the first position within the list. This is not the entire problem. Because the DropDownList is being used as a filter for the GridView (which has a separate LinqDataSource in this example), we need to instruct the other LinqDataSource what to do with this new value "-1". So in essence we need to tell it to remove the filter, because we now want all records back. So on the Selecting event of the LinqDataSource we need to remove the Where parameter that is being filtered. So, lets give an example:

   1:  protected void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
   2:    if (DropDownList1.SelectedValue == "-1") {
   3:      e.WhereParameters.Remove("CategoryID");
   4:    }
   5:  }

That’s all there is to it. Now onto the other method.

Method Weird

I experimented and wondered whether it would be possible to insert an empty "ALL" item into the returned result set before they were bound to the actual DropDownList. So this would need to be performed on the Selected event of the LinqDataSource bound to the DropDownList. It turns out that the LinqDataSourceStatutEventArgs has a propery called "Result" which contains the results of the selected query. Ah ha, I thought, lets put it there by adding two lines of code:

   1:  List<Item> allItems = (List<Item>)e.Result;
   2:  allItems.Insert(0, (new Item() { ID = -1, ItemName = "All" }));

Or something to that effect. I ran the website and no luck, the new item added did not appear in the DropDownList. I thought, maybe there was something wrong with the way I had worked the code, but I couldn’t find any fault. Then by a happy mistake I added a DataBinding event to the DropDownList. Now, I thought I needed to put something in there, but I ran the code without anything in the DataBinding event and by miracle there was an All in the DropDownList.

image

That is all there is to it. Hopefully this is helpful for someone.

I read an interesting quote over the weekend where the sage of Omaha, Warren Buffet, gives a thought provoking view on Gold. He says

It gets dug out of the ground in Africa, or someplace. Then we melt it down, dig another hole, bury it again and pay people to stand around guarding it. It has no utility. Anyone watching from Mars would be scratching their head.

I thought this was an interesting view to take. All things considered, what he says makes total sense. I guess it all comes back to perceived value. The same goes with housing. I may value someone’s house 50% less than a real estate agent, but that is the value that it is equated in my mind. Subjective as it may be. But then we have to look at the collective perception of the majority of the population and Gold has a higher value than most other metals. As we can see with the price of Gold versus just 6 years ago.

Early man must have liked the shine!! What do you think?

Tags:

I have recently been using the LinqDataSource and the GridView control together to speed up my development time. On one project that I have I used the LinqDataSource in conjunction with the FormView control. It has been extremely useful, especially for updating data in a somewhat error free way.

However, I have a lot of Nullable DateTimes in my database and consequently in my LinqDataContext. When I leave say my DateCompleted field empty and then click update on the FormView control, I get the following error:

server error in application

Now this was a little bit of a problem for me. I thought there must be something wrong with my LinqDataContext. So I checked my database, sure enough, DateCompleted is nullable and within my LinqDataContext everything was fine. So what the hell!!

Quickly I looked into the capabilities of the LinqDataSource and found that update parameters are passed to the underlying LinqDataContext and it turns out that the values don’t just default to NULL when they are handed over. It is up to the individual programmer to set this up. So in order to succeed all you have to do is add the following:

   1:  <UpdateParameters>
   2:      <asp:Parameter Name="DateCompleted" ConvertEmptyStringToNull="true" />
   3:  </UpdateParameters>

Within the UpdateParameters we need to insert a parameter that corresponds to the offending field (LinqDataContext name) and then set the ConvertEmptyStringToNull = true. Which need to be performed for all offending fields. Which seems like a pain in the bum (I thought they would have put it as a property of the LinqDataSource itself to allow you to default all fields to NULL if they are emtpy, but hey.. what do I know), and indeed is. But at the end of the day, you have achieved the end goal and no more errors. It will turn your empty string into a null and then everybody is happen. Such a simple thing. Hopefully this has been helpful for someone.

Recently I have been neglecting my posting duties. I am not entirely sure why that is (probably because I feel I have nothing interesting to say), but alas that is probably for another post.
I helped a friend out with a transfer of their Wordpress blog across from one hosting environment to another and during the [...]

Tags:

Well it took a while to find something that I was happy with, but I finally did. But even though the theme is almost finished, I am still not entirely sure that it is the theme that I want to go with. Oh well, at the end of the day, it is [...]

I was making a simple mistake when it came to

gadgets.util.registerOnLoadHandler()

I was ommitting placing the () at the end of the function to be called.
Anyway, now that that has been resolved, I have simply placed all the config.init code from the previous post in the method that is called on gadget.util.registerOnLoadHandler.
This makes things a little [...]

The saga continues….
Unfortunately it appears that Opensocial and ASP.NET do go together somewhat, however there are a few hiccups along the way. One deals primary with the Opensocial API code that is generated by hi5. After my last post, I went through and tried to debug the errors that I had and that others had [...]

Being a c# ASP.NET developer, I wanted to develop an opensocial application that could be developed with server side code so that I could easily interact with my services that I have on my server and also so that I can more importantly interact with my database directly, without using the gadgets.io.makeRequest function made available.
It [...]