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);
%>