views:

227

answers:

1

I am beginning to make friends with SubSonic 3.0.0.3 - SimpleRepository. Since you/I have to create every object class, so I made a simple code below, just to save some time.

My only question is, does my mapping are correct (DB DataType to .Net DataType)?

Here's the simple code:

public string CreateClassEntity(string ConnectionSring, string TableName)
        {
           SqlDatabase db = new SqlDatabase(ConnectionSring);

            string sqlCommand = "SELECT TOP 1 * FROM " + TableName + "";
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            DataSet subsonicDataSet = db.ExecuteDataSet(dbCommand);

            DataTable subsonicDataTable = subsonicDataSet.Tables[0].Clone();
            string classEntity = "public class " + TableName + " \n{";
            foreach(DataColumn column in subsonicDataTable.Columns)
            {
                classEntity += "\n\tpublic ";
                classEntity += column.DataType.ToString().Replace("System.", string.Empty) + " ";
                classEntity += column.ColumnName + " "; 
                classEntity += "{ get; set; }";
            }
            classEntity += "\n}";

            return classEntity;
        }

Sample target Table (SQL 2008):

CREATE TABLE [kiss].[Users](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [UserName] [nvarchar](20) NOT NULL,
    [UserPassword] [varbinary](128) NULL,
    [UserTypeID] [tinyint] NOT NULL,
    [ByPassAccessRestrictionsFlag] [bit] NOT NULL,
    [IsEnforcePasswordPolicy] [bit] NOT NULL,
    [PasswordExpirationDate] [datetime] NULL,
    [IsPwdChangeNextLogin] [bit] NOT NULL,
    [ShowLatestNewsFlag] [bit] NOT NULL,
    [SortRowNumber] [int] NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [CreatedBy] [nvarchar](20) NOT NULL,
    [UpdatedDate] [datetime] NOT NULL,
    [UpdatedBy] [nvarchar](20) NOT NULL,
    [DeletedDate] [int] NULL,
    [EntityTypeID] [int] NOT NULL
    )

Sample Class Output:

public class Users 
{
    public Int32 ID { get; set; }
    public String UserName { get; set; }
    public Byte[] UserPassword { get; set; }
    public Byte UserTypeID { get; set; }
    public Boolean ByPassAccessRestrictionsFlag { get; set; }
    public Boolean IsEnforcePasswordPolicy { get; set; }
    public DateTime PasswordExpirationDate { get; set; }
    public Boolean IsPwdChangeNextLogin { get; set; }
    public Boolean ShowLatestNewsFlag { get; set; }
    public Int32 SortRowNumber { get; set; }
    public DateTime CreatedDate { get; set; }
    public String CreatedBy { get; set; }
    public DateTime UpdatedDate { get; set; }
    public String UpdatedBy { get; set; }
    public Int32 DeletedDate { get; set; }
    public Int32 EntityTypeID { get; set; }
}