Tuesday, September 29, 2009

Debug Windows Mobile applications on real device

amplitude_websiteimage

Highly recommend this article - rare type of article about migrating real-world application from one platform – iPhone - to another - windows mobile, great in comparing and adopt different approaches to development. 

So one of things that I realized only when reading this article - is that we can debug Windows Mobile applications on real device, connected to desktop, not in emulator, and it’s very helpful.

It’s easy – between various emulators in default devices setting for project we have “Windows Mobile 6 Professional Device” – and it works fine.

image

Thursday, September 17, 2009

Nested (inner) object initializers

Object initializers are probably most usable feature from C# 3 in my daily work. It was adopted by me since the beginning but only today I found that its support nesting, look:

var edit = new TextEdit();
edit.Name = "passwordEdit";
edit.Properties.PasswordChar = '*';

It's simple but real code, without initializers, how we can improve it? Before today's knowledge only like this:

var edit = new TextEdit{
   Name = "passwordEdit"
}
edit.Properties.PasswordChar = '*';

Because we can't write:

var edit = new TextEdit{
   Name = "passwordEdit",
   Properties.PasswordChar = '*'
}

And now, proper syntax:

var edit = new TextEdit{
   Name = "passwordEdit",
   Properties = {
                        PasswordChar = '*'
                }
}

Awesome. I hope some day we can attach event handlers with object initializers like Miguel de Icaza proposed.

Friday, September 11, 2009

GAC GUI/.Net 2.0 Configuration

It appears that if we go to Control Panel\Administrative Tools – we found tool called Microsoft .NET Framework 2.0 Configuration.

And with this tool has GUI for work with GAC – list, register, unregister - without need to use gacutil. I think sometimes (and for some people :)) it may be very helpful – while console utils is right way, of course.

11.09

Thursday, September 3, 2009

Real world refactoring

I often find code like this in some projects:

      public bool ShowDialog(object view, string title)   
      {   
		  //some code   
  
          var window = GetWindow(control,title);   
  
		  //some code   
  
          using (container)   
              return (bool) window.ShowDialog();   
      }   
  
		/////////////////////////////////////////   
		// a lot code between   
		/////////////////////////////////////////   
  
      public void Show(Control view, string title)   
      {   
    	  //some code   
  
          var window = GetWindow(view, window);   
  
    	  //some code   
  
          window.Show();   
      }   
  
		/////////////////////////////////////////   
		// a lot code between   
		/////////////////////////////////////////   
  
      private Window GetWindow(Control control, string title)   
      {   
            var window = new Container(control);   
  
			//doings something else with container, like:   
           
        	window.Text = title;   
  
	        return window;   
      }  

I this case Show and ShowDialog have really different logic and not well suitable for functional-style as I think, that’s not the case.

The case is method GetWindow itself – it is like situation when solving one problem you creates another.

As I think developer has following code:

      public bool ShowDialog(object view, string title)   
      {   
		  //some code   
  
		  var window = new Container(control);   
  
	 	  //doings something else with container, like:   
           
          window.Text = title;
  
		  //some code   
  
          using (container)   
              return (bool) window.ShowDialog();   
      }   
  
		/////////////////////////////////////////   
		// a lot code between   
		/////////////////////////////////////////   
  
      public void Show(Control view, string title)   
      {   
    	  //some code   

		  var window = new Container(control);   
  
	 	  //doings something else with container, like:   
           
          window.Text = title;   
   
    	  //some code   
  
          window.Show();   
      }   
 

His intention was to remove code duplication and it’s right, but… But He prefer to “Extract method” GetWindow, which give us code that we have. Yes, this is one way to do, but it’s not best, because in this case when somebody wants to read or edit this code it’s a very annoying. You need to switch your view between such “Top-level” public method and that “Extracted” private methods, it’s in such big class, especially bad when you have several methods like GetWindow. You just violates SRP.

You can avoid it really easy – you have 2 alternatives:

First

Just overload constructor of Window and implement all you need there:

      public bool ShowDialog(object view, string title)   
      {   
		  //some code   
  
          var window = new Container(control,title);   
  
		  //some code   
  
          using (container)   
              return (bool) window.ShowDialog();   
      }   
  
		/////////////////////////////////////////   
		// a lot code between   
		/////////////////////////////////////////   
  
      public void Show(Control view, string title)   
      {   
    	  //some code   
  
          var window = new Container(view, window);   
  
    	  //some code   
  
          window.Show();   
      }   
  
//Container.cs:

      public class Container
      {   
			public class Container(Control control, string title, /*any other params you need to set it properly */)
			{
				//doings something you need
           
        		window.Text = title;   
  
				//doings something you need
			}
      }  

Second

The bad thing of first way is that 1) You can have no access to/can’t inherit from class Container 2) You can hides some logic in Container which not natural to class Container. If one of these, or both – create Factory:

	  IContainerFactory containerFactory; 

      public bool ShowDialog(object view, string title)   
      {   
		  //some code   
  
          var window = containerFactory.Get(control,title);   
  
		  //some code   
  
          using (container)   
              return (bool) window.ShowDialog();   
      }   
  
		/////////////////////////////////////////   
		// a lot code between   
		/////////////////////////////////////////   
  
      public void Show(Control view, string title)   
      {   
    	  //some code   
  
          var window = containerFactory.Get(view, window);   
  
    	  //some code   
  
          window.Show();   
      }   
  
// ContainerFactory.cs:

      public class ContainerFactory
      {   
			public Get(Control control, string title, /*any other params you need to set it properly */)
			{

				//doings something you need
           
	        	window.Text = title;   
  
				//doings something you need

			}
      }