Monday, March 9, 2009
Hot to clear the Online Client Cache for xbap applications
rundll32 %windir%\system32\dfshim.dll CleanOnlineAppCache
Warning: this will clean out all online client cache.
Tuesday, January 6, 2009
Inversion of Control or IoC
Inversion of Control or IoC is a design pattern. This pattern is much like the Strategy Design pattern from the GOF and the Dependency Injection pattern. I like this pattern for a few reasons. First: it allows for good separation of concerns. Second: if you are doing test driven development, this pattern is a must and last: if your implementation of an interface must be changed at runtime, this pattern is needed to receive the concrete object created by an object factory.
The IoC pattern is a minimum of three parts:
1. The concrete service class. This class is the primary interface to the client. I always suffix this class with service. Here you will define the main workflow. Methods in this class will invoke methods on the interface and perform the core logic of the service.
2. The provider interface. This gets passed to the service class when it is created. I always suffix this interface with provider.
3. The concrete provider. This class implements the provider interface. I always suffix this class with provider.
The following is an example of a simple IoC design pattern. This service provides the ability to perform home automation tasks.
public interface IHomeAutomationProvider
{
void TurnOn( int deviceId );
void TurnOff( int deviceId );
void DimLight( int deviceId, int percent );
}
public class HomeAutomationProvider : IHomeAutomationProvider
{
public void TurnOn( int deviceId )
{
// Turn on
}
public void TurnOff( int deviceId )
{
// Turn off
}
public void DimLight( int deviceId, int percent )
{
// Dim light
}
}
public class HomeAutomationService
{
private IHomeAutomationProvider _Provider;
public HomeAutomationService( )
:this( new HomeAutomationProvider())
{
}
public HomeAutomationService( IHomeAutomationProvider provider )
{
_Provider = provider;
}
public void TurnOn( int deviceId )
{
_Provider.TurnOn( deviceId );
}
public void TurnOff( int deviceId )
{
_Provider.TurnOff( deviceId );
}
public void DimLight( int deviceId , int percent )
{
_Provider.DimLight( deviceId , percent );
}
}
Notice the overloaded constructors on the service. This allows for ease of use if not using a factory to provide the provider and allows the service to have a default provider. If you are using TDD, and you do not have access to the API that performs the home automation task, or you do not want you lights going on and off when running tests you would simply create a new test or mock provider and pass it in to the service when performing the test.
References:
http://en.wikipedia.org/wiki/Strategy_pattern
http://martinfowler.com/articles/injection.html
http://www.dnrtv.com/default.aspx?showNum=126
Monday, November 17, 2008
The importance of SQL Check Constraints
http://msdn.microsoft.com/en-us/library/ms179491.aspx
Wednesday, November 5, 2008
A fast and easy console shell - Part 2
public void Exeucte()
{
Console.WriteLine( “Service to start:”);
string serviceName = Console.ReadLine();
StartService( serviceName );
}
Pretty easy! This way also allows you to enter and display defaults in case the user does not enter a value or just wants to use the default.
Monday, October 27, 2008
A fast and easy console shell - Part 1
1) The first step is to create a base command class:
public abstract class ConsoleCommand
{
public ConsoleCommand ( )
{
}
public void Execute( string parms )
{
ExecuteCore( parms );
}
protected abstract void ExecuteCore( string parms );
}
2) Create a class level attribute.
[AttributeUsage(AttributeTargets.Class)]
public class ConsoleCommandAttribute : Attribute
{
public ConsoleCommandAttribute( string commandName )
{
CommandName = commandName.ToUpper();
}
public string CommandName { get; set; }
}
3) Create a new console command.
[ConsoleCommandAttribute ("Run sample" )]
public class RunSampleCommand : ConsoleCommand
{
public RunSampleCommand ( )
{
}
protected override void ExecuteCore( string parms )
{
Console.WriteLine( "Command executed" );
}
}
4) The next step is to find and load all of the commands when the application starts up.
static void LoadCommands( )
{
Assembly ass = Assembly.GetEntryAssembly( );
var query = ( from t in ass.GetTypes( )
where
Attribute.IsDefined( t ,
typeof(ConsoleCommandAttribute) ,
false ) == true
select t ).ToList( );
foreach ( Type type in query )
{
ConsoleCommandAttribute att =
Attribute.GetCustomAttribute( type ,
typeof(ConsoleCommandAttribute) ,
false ) as ConsoleCommandAttribute;
// You could create the type here or wait until it is ask for. The latter is the better option.
ConsoleCommand cmd =
(ConsoleCommand)Activator.CreateInstance( type );
CommandList.Commands.Add( att.CommandName , cmd );
}
}
}
5) Next handle the command the user entered.
String cmd = Console.ReadLine();
cmd = cmd.ToUpper();
if ( CommandList.Commands.ContainsKey( cmd ) )
{
try
{
CommandList.Commands [ cmd ].Execute( string.Empty );
}
catch ( Exception ex )
{
Console.WriteLine( ex.Message );
}
}
else
{
Console.WriteLine( "Command not found." );
}
}
Once the command has finished, you will need to call back to the method that waits for the user to enter a new command.
In part two, I will talk about how to handle parameters without string parsing.