Saturday, 28 May 2011

Asp.net Mvc 3, Razor and RenderAction

When playing with the Razor syntax for view creation in Asp.net MVC you need to remember its not quite a direct translation between it and the web forms view engine and it is a different syntax. I was frustrated by it earlier in the week and without internet connection continued couldn’t resolve it instantly. It comes down to the syntactic sugar which Razor gives you.

When it comes to methods on the Html helper you can’t use the syntactic sugar by itself you do need to give Razor some parentheses.

Web forms view engine:

<% Html.RenderAction("About"); %>

Razor view engine:

@{ Html.RenderAction("About"); }

not @Html.RenderAction("About")

Thursday, 26 May 2011

GotConnection v0.2

There are a few changes and tweaks from v0.1 in this release. It was after a mini code review by my mate Will who had some suggestions on the connectivity testing which I have incorporated. I’ve also updated the way the options can be passed in to closer match the original way I wanted it to work.

Update to connectivity testing

Instead of specifying if you are connected or not this release has been updated to try and connect automatically and if no connection is available then use the in development settings. When you go live this can be turned off so if there is an issue with Twitter in a production environment it will return an empty string. This will need to tested for in the consuming code but a small price to pay for testing connectivity. This is driven by a configuration switch:

<add key="GotConnection.Twitter.InDevelopment" value="true" />

While testing the code the request for data from Twitter is relatively speedy so putting this functionality into a try/catch didn’t feel like a bad thing.

Update usage

The original plan was to allow the options to be specified with an anonymous object in much the same way in which you can override defaults in jQuery functions by passing in a json object. This release has now been updated to allow for this for the same options as in v0.1. I will look to expand to the rest of the Twitter API Timeline options in later releases.

Using default options:

GotConnection.ITwitter twitter = GotConnection.ConnectTo.Twitter();
var result = twitter.TimeLine("WestDiscGolf");

Specify options:

GotConnection.ITwitter twitter = GotConnection.ConnectTo.Twitter();
var result = twitter.TimeLine("WestDiscGolf", new { count = 5 });

Valid options:

format = Defines the format in which you want the result to return. The limitation is still currently only json for offline development; default = json.

count = Defines the number of status records to return in the call; default = 5

include_rts = Specifies if native retweets should be returned in the number of status updates returned; default = true.

Released

The updated code can be found on github. As always any questions or suggestions then please let me know. Also if anyone does find this useful then please let me know if you’re using it and what for.

Monday, 23 May 2011

GotConnection v0.1

As crazy as it sounds with the amount of connectivity out in the wild these days there are still times when you need to do web based development when you’re not connected to the internet. This in itself isn’t usually an issue except for when you’re developing some web service integrations eg. a Twitter feed integration in a website and have no way of accessing the Twitter API. This has been the main inspiration behind this little project as I’m disconnected from the outside world for a couple of hours a day on my commute to and from work and sometimes I want to integrate with Twitter.

So I present GotConnection v0.1.

“How do I use it?”

The concept is pretty simple. You include the library into your asp.net web forms or asp.net MVC or any .net project and where you would write the code to create the web request to extract out the json from Twitter service server side you do this instead …

ITwitter twitter = GotConnection.ConnectTo.Twitter(new { option = “blah” });
var result = twitter.TimeLine(“WestDiscGolf”, 3);

The result returned is the json representation of the timeline data for WestDiscGolf and the last 3 tweets. Simples.

The interesting part comes when you are working offline and have no access to the internet. To continue to develop against the returned json data you don’t need to do any testing in code, or change any of your consuming code at all. All you need to do is update an application setting in the projects web.config file to say that the component isn’t online anymore. And that’s it.

Limitations

The only feature at the moment is the ability to get a users timeline in json format from the Twitter API. This is mainly because this is the only functionality that I require at the moment. The offline data understands the number of tweets to return, but in this version that’s it.

Future versions

I’d like to expand this project to allow for more options in the Twitter time line functionality, including support for the different formats it can return. I’d like to expand it out to other services which have read only data feeds such as blogger, facebook, generic rss/atom feeds etc. Future versions will use the Options class further. The idea behind the Options class is to allow for passing in options/defaults through as an anonymous object much like jquery defaults are set.

I’m not interested in making this a component which becomes a library to log in/out, post etc. to services such as Twitter as there are many out there already. This is purely there for developing against a read only service when you’re not online.

Where can I get it?

Well it is up on github. This is the first time I’ve uploaded any of my code to github so feel free to look around, fork the code and change it all, add in extras etc. If you do find this useful and add any extras in then please let me know and I’ll see about adding them into the main fork. Probably in the next version or 2 I’ll look at putting it on NuGet but will have to wait and see.

Any questions or issues or ideas or comments then please tweet me @WestDiscGolf

Enjoy!