05 November 2008

How to get the last execution row in sql server and Cross apply


Have you ever think how to program better exception handling in your sql script, so this little article will teach how to do that, let's begin

The following query will give the last executed row in sql server

SELECT top 1 deqs.last_execution_time AS [Time], dest.text AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC


Notice : You can remove the top 1 and view all the last executed rows.

Cross & Apply Terms

The APPLY term let's you join a table to a table valued function.

No ON cluse used for this joining.

2 types of apply :

1. CROSS
Returns rows from the left side (Customers) if the table-valued-function returns rows.

2. OUTER
Returns all the rows on the left side (Customers) whether they return any rows in the table-valued-function or not. The columns that the table-valued-function returns are null if no rows are returned for the opposite row in the left side.

28 November 2007

Read this article , Dealt with top 20 GC articles.

26 November 2007

Repository, A new pattern

Most of the client -- Server application use a pattern called repository http://en.wikipedia.org/wiki/Repository.

Responsibilities :
- Cache (use it internally).
- reducing calling the server.
- Storage
- Provide base funtionllilty and abstract implementation over the repository,
- Offline warehouse.
- Middle tier between the client and server.

Repository problems,
- Decrease performance.
- Redundant calls to server.
- Not an abstract - Coupled to concrete implication.

The following new design of - ITEM SET repository, Give a new way of repository implementation.

So,

Item set


















To be continue...

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
}
}

29 May 2007

Scroll text into view in Rtf known as Rich Text Box, How to do that ?

1. The simple way, Just set the HideSelection property to false.

2. The geek way, SendMessage(this.Handle, (uint)WM_HSCROLL, (System.UIntPtr)3, (System.IntPtr)0);

The geek way can be implemented in every scrollable control.