No Free Time

Because my therapist says I need to let things out

Archive for August, 2008

I lost my phone last night

Posted by andrewmyhre on August 29, 2008

A few months ago, something wonderful happened in my life. A new network family member arrived – a shiny, new Nokia N82. I named it Lazerbeak, after one of the smaller, more tenacious Decepticons. That phone did everything. I could check email, Tweets, it would download podcasts automatically for me, it was WiFi ready. I could send images directly to Flickr, Picasa or TweetPic and video blog straight from the phone. And last night I lost it. I don’t know whether it was stolen, or if I lost it and then it was stolen. It doesn’t really matter, I’m still equally angry with myself either way.

:( I loved that phone. But at least my contract ends in 2 weeks and then I’ll be able to get a new phone.

Posted in random | Tagged: | Leave a Comment »

How to get the screen dimensions across all monitors in WPF

Posted by andrewmyhre on August 21, 2008

I seem to be needing this code in every WPF I start at the moment, so here it is in a nice reproducable form. You need to add references to System.Windows.Forms and System.Drawing in order to do this though. Shame that dependency is required – I don’t see how getting screen information is strictly related to forms, so it’d be cool to be able to do this without requiring the System.Windows.Forms reference.

Anyway, stick this somewhere in your app (I put it in the App class and assign it to a static member):

27 private static Rect GetTotalScreenArea()

28 {

29 Rect screenArea = new Rect();

30 foreach (Screen s in Screen.AllScreens)

31 {

32 if (s.Bounds.Height > screenArea.Height)

33 screenArea.Height = s.Bounds.Height;

34

35 screenArea.Width += s.Bounds.Width;

36 }

37 return screenArea;

38 }

So my App.xaml.cs looks like this:

1 using System;

2 using System.Collections.Generic;

3 using System.Configuration;

4 using System.Data;

5 using System.Linq;

6 using System.Windows;

7 using System.Windows.Forms;

8

9 namespace MyProject

10 {

11 /// <summary>

12 /// Interaction logic for App.xaml

13 /// </summary>

14 public partial class App : Application

15 {

16 public static Rect TotalScreenArea;

17

18 protected override void OnStartup(StartupEventArgs e)

19 {

20 TotalScreenArea = GetTotalScreenArea();

21

22 base.OnStartup(e);

23 }

24

25 private static Rect GetTotalScreenArea()

26 {

27 Rect screenArea = new Rect();

28 foreach (Screen s in Screen.AllScreens)

29 {

30 if (s.Bounds.Height > screenArea.Height)

31 screenArea.Height = s.Bounds.Height;

32

33 screenArea.Width += s.Bounds.Width;

34 }

35 return screenArea;

36 }

37 }

38 }

Posted in Uncategorized | 2 Comments »

Friday Randoms

Posted by andrewmyhre on August 16, 2008

Computer animation studio Pendulum have made amazing developments in facial animation. Check out Pendulum’s demo video (amazing!) and Pendulum’s website.

Wridea is an ‘Idea management service and collection of brainstorming tools’. Sounds neat, like an online document collaboration tool but tweaked a bit. Have a look, I haven’t yet.

Twist is a Twitter trend monitoring tool. See (and compare) what trends are being talked about over time – just for fun try Beijing, Olympics and Tibet :) .

And finally a Coding4Fun article: A low-cost DIY multi-touch solution. It’s only a matter of time before we do this!

(Coding4Fun is a blog by a few Microsoft employees who find weird and unusual things to do with .Net or other languages.)

And finally, a video I think we can all relate to.

Posted in random | Tagged: | Leave a Comment »

Load ASP.Net MVC Routes dynamically at runtime from a repository

Posted by andrewmyhre on August 2, 2008

This article was written for a preview version of ASP.Net MVC and is now out of date.

Source for this example

Today I read Ian Suttle’s post about loading MVC routes dynamically from a SQL database. I think it’s a cool idea, and I wanted to see if I could do the same thing with XML or a configuration file. I also wanted to introduce the factory design pattern so that people can plug in their own route repositories.

My solution comprises the following classes:

  • MvcRoute – main route DTO
  • MvcRouteParam – route parameter DTO
  • IRouteService – interface defining a service which loads route data from a repository
  • RouteServiceBase – abstract class handles most common route service operations
  • ConfigRouteService – extends RouteServiceBase,  is responsible for reading configuration settings and constructing an IRouteService
  • DynamicRoutesConfigurationHandler and DynamicRoutesConfiguration – parse the configuration

A couple of caveats:

  • It’s not complete, it’s a proof of concept
  • It doesn’t handle ‘ignore’ routes

Settings your routes looks like this:

  

  

24 protected void Application_Start()

25 {

26 IRouteService routeService = new ConfigRouteService();

27 routeService.SetAppRoutes(RouteTable.Routes);

28 RegisterRoutes(RouteTable.Routes);

29 }

 

IRouteService is implemented thusly:

 

  

11 namespace DynamicRoutes

12 {

13 public interface IRouteService

14 {

15 List<MvcRoute> GetConfiguredRoutes();

16 RouteCollection ResetAppRoutes(RouteCollection RouteTable);

17 RouteCollection SetAppRoutes(RouteCollection RouteTable);

18 RouteCollection SetAppRoutes(RouteCollection RouteTable,

19 List<MvcRoute> ConfiguredRoutes);

20 }

21 }

 

 

Most importantly you have a method to get route data from your chosen repository (GetConfiguredRoutes) and a method to add the routes to your application RouteTable (SetAppRoutes).

 

The RouteServiceBase class implements all methods except GetConfiguredRoutes, and this should be the only method you need to implement yourself in order to set up a new route repository. For example, here’s the implementation for ConfigRouteService:

 

 

9 namespace DynamicRoutes

10 {

11 public class ConfigRouteService : RouteServiceBase

12 {

13 public override List<MvcRoute> GetConfiguredRoutes()

14 {

15 DynamicRoutesConfigurationSection configuration =

16 ConfigurationManager.GetSection(“dynamicRoutes”) as

17 DynamicRoutesConfigurationSection;

18

19 return configuration.Routes;

20 }

21 }

22 }

 

 

And here are the configuration handler classes:

 

 

7 namespace DynamicRoutes

8 {

9 public class DynamicRoutesConfigurationHandler : IConfigurationSectionHandler

10 {

11 public object Create(object parent, object configContext, System.Xml.XmlNode section)

12 {

13 DynamicRoutesConfigurationSection config = new DynamicRoutesConfigurationSection();

14 config.LoadRoutesFromConfig(section);

15 return config;

16 }

17 }

18 }

  

8 namespace DynamicRoutes

9 {

10 public class DynamicRoutesConfigurationSection : ConfigurationSection

11 {

12 public List<MvcRoute> Routes { get; set; }

13 public string TypeName { get; set; }

14 public void LoadRoutesFromConfig(XmlNode section)

15 {

16 Routes = new List<MvcRoute>();

17 TypeName = section.Attributes["type"].Value;

18 foreach (XmlNode node in section.ChildNodes)

19 {

20 if (node.Name == “route” && node.Attributes["type"].Value == “map”)

21 Routes.Add(new DynamicRouteConfiguration().LoadRouteMappingFromConfig(node));

22 }

23 }

24 }

25

26 public class DynamicRouteConfiguration

27 {

28 internal MvcRoute LoadRouteMappingFromConfig(XmlNode routeNode)

29 {

30 MvcRoute route = new MvcRoute();

31

32 route.routeName = routeNode.Attributes["name"].Value;

33 route.routePattern = routeNode.Attributes["pattern"].Value;

34 route.routeParams = new List<MvcRouteParam>();

35

36 foreach (XmlNode node in routeNode.ChildNodes)

37 {

38 if (node.Name == “param”)

39 route.routeParams.Add(LoadParamFromConfig(node));

40 }

41

42 return route;

43 }

44

45 internal MvcRouteParam LoadParamFromConfig(XmlNode paramNode)

46 {

47 MvcRouteParam param = new MvcRouteParam();

48

49 param.paramKey = paramNode.Attributes["key"].Value;

50 param.paramValue = paramNode.Attributes["defaultValue"].Value;

51

52 return param;

53 }

54 }

55 }

 

 

Finally, here’s what’s in my web.config to set up my default route:

 

 

11 <configSections>

12 <section name=dynamicRoutes type=DynamicRoutes.DynamicRoutesConfigurationHandler, DynamicRoutes/>

13 </configSections>

14

15 <dynamicRoutes>

16 <route type=ignore url={resource}.axd/{*pathInfo}/>

17 <route type=map name=Default pattern={controller}/{action}/{id}>

18 <param key=controller defaultValue=Home/>

19 <param key=action defaultValue=Index/>

20 </route>

21 </dynamicRoutes>

 

 

This all probably sounds like nonsense so download the complete source here.

Posted in mvc | Tagged: , | 10 Comments »