Tuesday, January 3, 2012

Replace task manager with process explorer

Process Explorer is considered to be a more advanced form of the Windows Task Manager. it shows you information about which handles and DLLs processes have opened or loaded

You also have the option to replace the traditional Task Manager by the Process Explorer software menu: Options-> Replace Task Manager

Sunday, December 25, 2011

Simple cache provider

Instead of using the if condition to take the data from the or from the db on each data function, i have created a simple function that uses delegate and generics,
the function improve your code concentration and פrevents code replication
the function has 3 arguments:
- cacheKey - the key name to use in the cache object
- getfromDbFunc - this function will call in case that the cache is empty
- param - the parameter will be send to getfromDbFunc function


public class CacheProvider
{
public T GetData<T, P>(string cacheKey,
Func<P, T> getfromDbFunc, object param) where T : class
{
T item = HttpRuntime.Cache.Get(cacheKey) as T;
P p = (P)param;
if (item == null)
{
item = getfromDbFunc(p);
HttpContext.Current.Cache.Insert(cacheKey, item, null,
DateTime.UtcNow.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration);
}
return item;
}
}


Example how to use:

protected void Page_Load(object sender, EventArgs e)
{
CacheProvider cacheProvider = new CacheProvider();
int userid = 17;
var user = cacheProvider.GetData<string, int>("user_" + userid, getUser, userid);
}
public string getUser(int id)
{
//TODO: retrieve user from db
return "newuser";
}
I'll be glad to get suggestions or improvements

Tuesday, November 29, 2011

My first Windows Phone app: SelfTimer++



Hi,
I am proud to introduce you my first Windows Phone app: SelfTimer++

SelfTimer++ gives a delay between pressing the shutter release and the shutter's firing.
Once you push the button, a countdown sound is emitted.
There is no setting page, setting is on camera screen
A trial version is also available with ads
Features:
- Self timer with countdown on screen and countdown sound
- Output tone: Grayscale, Sepia, Negative
- Set up shots in sequences of 2, 3, 4 or 5 photographs
- Autofocus and Tap to focus
- Resolution settings
- Silent mode
- Last capture image view

Tuesday, November 1, 2011

How to auto generate code

I think that, at least, something like 50% of our code is very structed and simple
and can be done by an auto generated code tool and there is no need
to write this code manually,
the main advantages of using auto generated code tool are:

  1. Saves time - don't waste your time on the simple actions, waste your time on the complicated algorithm

  2. Systemic change - for example if we want to add a new field to all the entities in the system
    we can do it in a one central place instead of to change each entity
  3. better code - the code is clean and simple

  4. coding standards built in - there is no need to make code reviews, the code is structed and known to everyone


in my sample below i use MyGeneration auto generated tool
that it is a very easy to use tool

lets say that i have a Persons table in my db(see the table code below)
and i want to create c# code and sql procedures for this entity


create TABLE [dbo].[Persons](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Counter] [int] NULL,
[BirthDate] [datetime] NULL,
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
) ON [PRIMARY]

GO



Code Generated
I want to create this basic c# classes that will represent this table:

  1. PersonBase - a class that contains all the fields of person

  2. IPerson - an interface that contains all the fields of person

  3. Person - a class that containd the logic of the entity

the idea is to update the template code in the MyGeneration template
that is based on this code lines:


<%
foreach (string columnName in columns)
{
columnAlias = DnpUtils.SetCamelCase(DnpUtils.TrimSpaces(table.Columns[columnName].Alias));
columnLanguageType = table.Columns[columnName].LanguageType;
%>
private <%=columnLanguageType%> _<%=columnAlias%>;
<%
}
%>

which means that the lines in the yellow parentheses are the logic to manipulate your
auto generate code and all the other lines are the auto generated code output
in this sample i make a loop through all the columns of the table and create a
private data member in c#

you may see the output of this c# code template below:





/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************
*
*
* PersonBase - A Class Representing Persons' fields/columns
*
*
/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************/


using System;
using System.Text;
using System.Data;

namespace My.Framework.Core.Application
{
[Serializable()]
public class PersonBase : IMyObject
{
#region MyGeneration Auto-Generated Code

#region fields
private int _id;
private string _name;
private int? _counter;
private DateTime _birthDate = DateTime.MinValue;

#endregion

#region properties

public int Id
{
get { return _id; }
set { _id = value;}
}

public string Name
{
get { return _name; }
set { _name = value;}
}

public int? Counter
{
get { return _counter; }
set { _counter = value;}
}

public DateTime BirthDate
{
get { return _birthDate; }
set { _birthDate = value;}
}
#endregion

#region Constructors

public PersonBase(){}
public PersonBase(IDataReader reader)
{

this._id = (int)Convert.ChangeType(reader["ID"], typeof(int));
if (!reader.IsDBNull(reader.GetOrdinal("Name")))
{
this._name = (string)Convert.ChangeType(reader["Name"], typeof(string));
}
if (!reader.IsDBNull(reader.GetOrdinal("Counter")))
{
this._counter = (int)Convert.ChangeType(reader["Counter"], typeof(int));
}
if (!reader.IsDBNull(reader.GetOrdinal("BirthDate")))
{
this._birthDate = (DateTime)Convert.ChangeType(reader["BirthDate"], typeof(DateTime));
}
}
public PersonBase(int id, string name, int? counter, DateTime birthDate)
{
this._id = id;
this._name = name;
this._counter = counter;
this._birthDate = birthDate;
}

#endregion

#region IMyObject Members

public DateTime Created
{
get { return DateTime.MinValue; }
set { throw new NotImplementedException(); }
}

public DateTime Deleted
{
get { return DateTime.MinValue; }
set { throw new NotImplementedException(); }
}

public DateTime Updated
{
get { return DateTime.MinValue; }
set { throw new NotImplementedException(); }
}

#endregion

#endregion
}
}




/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************
*
*
* IPerson - An Interface representing Persons
*
*
/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************/


using System;
using System.Text;
using System.Data;
using My.BusinessLogic.DataAccess;
using My.Framework;
using My.Framework.Core.Application;
using My.BusinessLogic.Application;

namespace My.BusinessLogic
{
public interface IPerson
{
#region MyGeneration Auto-Generated Code

#region properties
int Id{get;set;}
string Name{get;set;}
int? Counter{get;set;}
DateTime BirthDate{get;set;}
#endregion

#endregion
}
}




/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************
*
*
* Person BL's Class Code
*
*
/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************/


using System;
using System.Text;
using System.Data;
using My.BusinessLogic.DataAccess;
using My.Framework;
using My.Framework.Core.Application;
using My.BusinessLogic.Application;

namespace My.BusinessLogic
{
[Serializable]
public class Person : PersonBase,IPerson
{
#region MyGeneration Auto-Generated Code

#region Constructors

public Person() : base()
{}
public Person(IDataReader reader) : base(reader)
{}


public Person(int id, string name, int? counter, DateTime birthDate) :
base(id, name, counter, birthDate) {}

#endregion


#region ISchemaDataObject Members

public bool Save(ISession session)
{
if (Id == -1) //Insert
{
Id = session.GetDataSource().Insert(this);
}
else //Update
{
session.GetDataSource().Update(this);
}
return true;
}

public bool Delete(ISession session)
{
throw new NotImplementedException("Delete wasn't generated, kindly implement it please.");
}

#endregion


#endregion
}
}






/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************
*
*
* SQL - Stored Procedures
*
*
/**************************************************************************************************
/**************************************************************************************************
/**************************************************************************************************

Get From the 2nd util..


and download the full template here



Sql Generated
I want to create this basic sql procedure that will represent this table:

  1. SP_Person_Insert - a procedure that insert a new person

  2. SP_Person_Update - a procedure that update an existing person

  3. SP_Person_Delete - a procedure that delete an existing person

  4. SP_GetPerson_ByID - a procedure that return the requested person

here the idea is a little bit different
the idea is to create a list of fields (for example: [Name], [Counter], [BirthDate])
and list of values (for example: @Name, @Counter, @BirthDate)
and to use it in the output of the template
i recommend you to download the full template from here
and to customize it as you wish

you may see the output of this sql procedure template below:


<%
//------------------------------------------------------------------------------
// SQL_StoredProcs.jgen
// Last Update : 2/21/2004
//
// Be sure to rename this template if you plan to customize it, MyGeneration
// Software will update this sample over time.
//------------------------------------------------------------------------------
//
// This template generates 3 stored procedures
//
// 1) [TableName]Update
// 2) [TableName]Insert
// 3) [TableName]Delete
//
// There is script in the "Interface Code" tab that pops up a dialog so you can tell this tempate
// where to save the files and what tables you want to generate stored procedures for. So, the
// logic at a very high level looks like this:
//
// For Each TableName in Select Tables
// objTable = database.Tables.Item(TableName)
// Generate the 3 stored procs for objTable
// Save file
// Next
//
// However, all of the script ends up in the Output tab and you can copy this right into
// Sql QueryAnalyzer and execute it. It's a pretty smart template, it knows to make
// Identity Columns output parameters to return them, the same holds true for computed
// Columns. It knows how to use PrimaryKeys in WHERE clauses and not to update them
// in the UpdateStored Proc, if you have a TimeStamp it will do the comparison for you and
// so on. This template alone can save you tons of time, and at anytime you can regenerate
// them as tables change.
//------------------------------------------------------------------------------
// Justin Greenwood
// MyGeneration Software
// justin.greenwood@mygenerationsoftware.com
// http://www.mygenerationsoftware.com/
//------------------------------------------------------------------------------

// collect needed data/objects and put them in local variables
var databaseName = input.Item("cmbDatabase");
var tablenames = input.Item("lstTables");
var database = MyMeta.Databases.Item(databaseName);

// Filename info
var filepath = input.item("txtPath");
if (filepath.charAt(filepath.length - 1) != '\\') filepath += "\\";

// The buffer that will hold all the output for rendering.
var buffer = "";

for (var i = 0; i < tablenames.Count; i++)
{
var tablename = tablenames.item(i);
var tableMeta = database.Tables.Item(tablename);

// Single name
var tableNameSingle = DnpUtils.SetPascalCase(DnpUtils.TrimSpaces(tableMeta.Alias));;

if (tableNameSingle.match("ies$") == "ies") // ends with
tableNameSingle = tableNameSingle.substr(0, tableNameSingle.length - 3) + "y";
else if (tableNameSingle.match("s$") == "s") // ends with
tableNameSingle = tableNameSingle.substr(0, tableNameSingle.length - 1);


// Build the filename
var filename = filepath + "sql_procs_" + tablename + ".sql"

var insertProcName = "SP_" + tableNameSingle + "_Insert";
var insertParams = "";
var insertFields = "";
var insertValues = "";
var insertAutoKeyCode = "";
var insertComputedCode = "";

var updateProcName = "SP_" + tableNameSingle + "_Update";
var updateParams = "";
var updateSet = "";
var updateWhere = "";

var deleteProcName = "SP_" + tableNameSingle + "_Delete";
var selectProcName = "SP_Get" + tableNameSingle + "_ByID";
var deleteParams = "";
var deleteWhere = "";

var paramName = "";

var hasComputedFields = false;
var hasTimestamp = false;

// Loop through all the columns of the table
for (var j = 0; j < tableMeta.Columns.Count; j++)
{
column = tableMeta.Columns.Item(j);
paramName = column.Name.split(' ').join('')

// If the datatype is not a timestamp, add it to the insert statement
if (column.DataTypeName == "timestamp")
{
hasTimestamp = true;
}
else if (!column.IsComputed)
{
if (insertParams != "")
{
insertParams += ",\r\n";
}
if (insertFields != "")
{
insertFields += ",\r\n";
insertValues += ",\r\n";
}

insertParams += "\t@" + paramName + " " + column.DataTypeNameComplete

if ((column.DataTypeName == "uniqueidentifier") && (column.IsInPrimaryKey) && (tableMeta.PrimaryKeys.Count == 1))
{
insertParams += " = NEWID() OUTPUT";
}
else if (column.IsNullable || column.IsAutoKey || column.IsComputed)
{
insertParams += " = NULL";

if (column.IsAutoKey || column.IsComputed)
{
insertParams += " OUTPUT";
}
}

if (!column.IsAutoKey && !column.IsComputed)
{
insertFields += "\t\t[" + column.Name + "]";
insertValues += "\t\t@" + paramName;
}
}

if (column.IsAutoKey)
{
insertAutoKeyCode += "\tSELECT @" + paramName + " = SCOPE_IDENTITY();\r\n";
}

if (column.IsComputed)
{
if (insertComputedCode == "")
{
hasComputedFields = true;
insertComputedCode += "\tSELECT ";
}
else
{
insertComputedCode += ", \r\n";
}
insertComputedCode += "\t\t@" & paramName & " = [" & column.Name & "]"
}

if (!column.IsComputed)
{
if (updateParams != "")
{
updateParams += ",\r\n";
}

updateParams += "\t@" + paramName + " " + column.DataTypeNameComplete;
if (column.IsNullable || column.IsComputed || column.DataTypeName == "timestamp")
{
updateParams += " = NULL";

if (column.IsComputed)
{
updateParams += " output";
}
}


if (column.IsInPrimaryKey || column.DataTypeName == "timestamp")
{
if (updateWhere != "")
{
updateWhere += " AND\r\n";
}

if (column.DataTypeName == "timestamp")
{
updateWhere += "\t\tTSEQUAL(" + column.Name + ", @" + paramName + ")";
}
else
{
updateWhere += "\t\t[" + column.Name + "] = @" + paramName;
}
}

if (!column.IsComputed && !column.IsAutoKey && column.DataTypeName != "timestamp")
{
if (updateSet != "")
{
updateSet += ",\r\n";
}

updateSet += "\t\t[" + column.Name + "] = @" + paramName;
}

if (column.IsInPrimaryKey)
{
if (deleteParams != "")
{
deleteParams += ",\r\n";
deleteWhere += " AND\r\n";
}
deleteParams += "\t@" + column.Name + " " + column.DataTypeNameComplete;
deleteWhere += "\t\t[" + column.Name + "] = @" + paramName;
}
}

}
%>USE [<%= database.Name %>]
GO

--|--------------------------------------------------------------------------------
--| [<%= insertProcName %>] - Insert Procedure Script for <%= tablename %>
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[<%= insertProcName %>]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [dbo].[<%= insertProcName %>]
GO

CREATE PROCEDURE [dbo].[<%= insertProcName %>]
(
<%= insertParams %>
)
AS
SET NOCOUNT ON

INSERT INTO [<%= tablename %>]
(
<%= insertFields %>
)
VALUES
(
<%= insertValues %>
)
<%= (insertAutoKeyCode == "" ? "" : "\r\n" + insertAutoKeyCode) %><%

if (hasComputedFields)
{
insertComputedCode += "\r\n\tFROM [" + tablename + "]\r\n";
insertComputedCode += "\tWHERE " + deleteWhere + ";\r\n";
}

%>
RETURN @@Error
GO

--|--------------------------------------------------------------------------------
--| [<%= updateProcName %>] - Update Procedure Script for <%= tablename %>
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[<%= updateProcName %>]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [dbo].[<%= updateProcName %>]
GO

CREATE PROCEDURE [dbo].[<%= updateProcName %>]
(
<%= updateParams %>
)
AS
SET NOCOUNT ON

UPDATE [<%= tablename %>]
SET
<%= updateSet %>
WHERE
<%= updateWhere %>
<% if (hasTimestamp) { %>
IF @@ERROR > 0
BEGIN
RAISERROR('Concurrency Error',16,1)
END
<% } %>
RETURN @@Error
GO

--|--------------------------------------------------------------------------------
--| [<%= deleteProcName %>] - Update Procedure Script for <%= tablename %>
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[<%= deleteProcName %>]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [dbo].[<%= deleteProcName %>]
GO

CREATE PROCEDURE [dbo].[<%= deleteProcName %>]
(
<%= deleteParams %>
)
AS
SET NOCOUNT ON

DELETE
FROM [<%= tablename %>]
WHERE
<%= deleteWhere %>

RETURN @@Error
GO



--|--------------------------------------------------------------------------------
--| [<%= selectProcName %>] - Update Procedure Script for <%= tablename %>
--|--------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[<%= selectProcName %>]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [dbo].[<%= selectProcName %>]
GO

CREATE PROCEDURE [dbo].[<%= selectProcName %>]
(
<%= deleteParams %>
)
AS
SET NOCOUNT ON

SELECT *
FROM [<%= tablename %>]
WHERE
<%= deleteWhere %>

RETURN @@Error
GO

<%
// Save this set of procedures to disk
output.save(filename, false);
buffer += output.text;
output.clear();
}

output.write(buffer);
%>

Thursday, October 13, 2011

How to increase session timeout on iis7

Many people ask me how to increase the session time on their web site
so here is the answer how to do that on iis7(make all the changes from the iis view):

  1. Application Pool => Advanced Settings => Process Model => Idle Time-out

  2. Sites => whatever web needs to be set => ASP => Session Properties => Time-out

  3. Sites => whatever web needs to be set => Configuration Editor => system.web/sessionState => timeout

  4. Sites => whatever web needs to be set => Configuration Editor => system.web/roleManager => cookieTimeout

Saturday, August 13, 2011

Configure a basic build server

Automation is power in software development
the exact name is continuous integration
i want to be able in one click to get latest version, build, run test and publish
this click makes me feel that i have a control on my project
in this post i'll show a basic configuration of build server
that includes:

  1. Get latest version form your source control

  2. Build your project

  3. Run your automatic testing

  4. Send an email notification with the status of the project (failed/success)


to this configuration we just have to add build trigger (schedule, on check in,...)
we also have to add an email notification to get status of the project to my email inbox


note: to run this sample you have to include your project and your test project in the same solution
you may see a sample project exactly in this structure here

my build server software is TeamCity
the build server configuration is very easy and includes 4 steps:


step 1:
-------

General Settings: nothing special in the first step, just supply a name for your project
Sample_step1


step 2:
-------

Version Control Settings: create and attach a new VCS root,
add checkout rule with the value of +:. (include all files)
and set your checkout directory (your local directory)
Sample_step2


step 3:
-------

Choose your build runner: this step inculde two sub-steps,
choose a build runner and set your test project location
the simplest runner is visual studio solution,
set your solution file and set your target and configuration fields

to set your test project you have to check the check box of run MSTest
set the path to MSTest.exe and set the path to your project test file under
List assembly files field
Sample_step3


step 4:
-------

Build Triggering: choose your build triggering,
i use a nightly scheduled build
Sample_step4


that's all now you have a build server!
just set your email notification in the project settings
a future work will be project publish with web config transforms
i hope i won't be too lazy and publish a post on it

Saturday, July 16, 2011

My Branching and Merging Strategy

Hi,
I would to share with you my way to the best practices for development version management
we always have to keep an isolated environment that is equal to the production environment because we want to keep the ability to make patches to the production environment while the other developers are working on the development iteration

my way is based on two important rules:
  1. Production environment must be equal to the main development environment for enabling production bug fixes over the iteration development

  2. The iteration development will be only in the branch development environment


I'll explain it by a simple scenario as yo can see in the chart below:

chart


legend
So, the scenario is:
1. create a branch development version from the main development version, this action occurred only once
2. in case that you have to fix a minor bug and to update the patch to the production environment, take it from the main development version
3. when the development iteration is over, we have to make merge from the main development version to the branch development version because of the production patch
4. we can update the production environment from the branch development version
5. after the version update we have to make merge from the branch development version to the main development version because we have to keep the main development version equal to the production environment

from this step we have to repeat steps 2-5 for each development iteration

one important tip:
when making the branch action, don't overwrite the configuration files, keep the target version