26 June 2007

Think on public resource file, instead of using the resfgen tool which does no have the advatages of the ResXFileCodeGenerator, Here a link, the ultimate tool of creating resource as strongly type class.

20 June 2007

Check if url is alive, some code

private static bool IsURLAlive(string strURL)
{

using (WebClient myWebClient = new WebClient())
{
char[] buf = new char[128];
try
{
using ( Stream myStream = myWebClient.OpenRead(strURL) )
{
StreamReader sr = new StreamReader(myStream);
sr.ReadBlock(buf , 0 , 127);
myStream.Close();
}
}
catch ( WebException webe )
{
return false;
}
return true;
}

}

11 June 2007




Inter process communication with events and delegates

Architecture Design


The Remote Object Process Requires A Reference To Each Class That Subscribe To The Event. In order for a remote server object to wire up an event (or register a callback) with a client, the server object needs to know a little bit about the client. This requires the server object to have a reference to each client that will subscribe to its events. This seems like a huge limitation for remote events. Imagine creating a server object to be used over remoting from many different types of client objects, and being required to add a reference to each of these client objects in the server object. Additionally, we may want to instantiate remote objects from within a client executable, and have the executable handle the remote event. Unfortunately, we cant add an executable as a reference to our server object.
So, having a client executable subscribe to remote objects events might seem impossible. However, there is a good way to get around this limitation with the use of a small event helper class, or event shim.
The idea is to create a small class that has a basic purpose of subscribing to remote objects events and then raising its own event. At first, this might seem like we are only postponing our event problem by propagating it up one level, but were really not. Remember, the real problem is our server object needs a reference to the client that will be subscribing to its event.
By placing a reference to this small event helper class in both the client and server assemblies, we circumvent the problem.
The server can see the object that will be subscribing to its event (the event helper) so it will know how to wire up the delegate callback for the event, and because our client has a reference to this small helper object it is able to subscribe to events on it.
IPC Event Design






Command Pattern Example
Recently i asked to add a functionality to an existed code platform, some of the functionality execute before the operational platform and some after.
This leads to massive change all over the code, in order to refine the change i use the command pattern

The Solution

Create an interface which define the command call it IWrapCommand, Define the functions as command, part of the functions refer as before function and the other as after function.
The OperationWrapper use to wrap the code platform with the commands.
The before command execute in the constructor and the after command execute in the dispose.
It preferred to use the using(){} code pattern to wrap the code platform.
Here the code,
namespace OperationWrapping{ public interface IWrapCommand { void Execute(); }}

using System;
using System.Collections.Generic;
using System.Text;

namespace OperationWrapping
{
/// <summary>
/// The class add the abillity of wrapping a operational process with
/// commands before the operation process executin and after.
/// </summary>
public class

OperationWrapper : IDisposable
{
#region Data members
private List< IWrapCommand > m_commandsAfter = null;
#endregion

public OperationWrapper(IWrapCommand i_commandBefore ,
IWrapCommand i_commandAfter)
{
if (i_commandAfter != null)
{
m_commandsAfter = new List< IWrapCommand >();
m_commandsAfter.Add(i_commandAfter);
}

if (i_commandBefore != null)
{
i_commandBefore.Execute();
}
}

public OperationWrapper(List<IWrapCommand> i_commandsBefore ,
List<IWrapCommand> i_commandsAfter)
{
m_commandsAfter = i_commandsAfter;

if (i_commandsBefore != null)
{
foreach (IWrapCommand m_command in i_commandsBefore)
{
m_command.Execute();
}
}
}

///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public void Dispose()
{
if (m_commandsAfter != null)
{
foreach (IWrapCommand m_command in m_commandsAfter)
{
m_command.Execute();
}
}

GC.SuppressFinalize(this);
}
}
}


namespace OperationWrapping
{
public interface IWrapCommand
{
void Execute();
}
}

AlfaCommand alfaCommand = new AlfaCommand();
DirtyCommand dirtyComamnd = new DirtyCommand();

using (OperationWrapper m_OperationDirty = new OperationWrapperalfaCommand , dirtyComamnd))
{
if (alfaCommand.IsContinue)
{
// the old code wrapped
}
}