Refactor Code

  • Gravatar
    How to Cache Parameter based Method Results

    by azamsharp on 8/14/2008 8:13:12 PM
  • Recently, I posted about how to cache the result of the method using the custom attributes. One of the difficulty I was having was to cache the result of the parameter based method. This means GetCustomerById(1) should be cached separately then the GetCustomerById(2). Below you can find partial implementation which cache the results of the method with no parameters. For the complete post check out the following URL: http://azamsharp.com/Posts/75_Implementing_CacheCall_Attribute_to_Cache_Method_Results.aspx
  • public static TResult Invoke<TSource,TResult>(TSource service,string methodName,object[] parameters)
    {
    CacheCallAttribute cacheCallAtt = null;

    string key = String.Format("{0}_{1}", (string) HttpContext.Current.Session["UniqueId"],
    methodName);

    MethodInfo method = service.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
    object[] customAtt = method.GetCustomAttributes(false);

    foreach (object obj in customAtt)
    {
    cacheCallAtt = obj as CacheCallAttribute;

    if (cacheCallAtt == null) continue;

    object result = method.Invoke(service, parameters);

    if (HttpRuntime.Cache[key] == null)
    {
    HttpRuntime.Cache.Insert(key, result, null, DateTime.Now.AddSeconds(cacheCallAtt.Duration), TimeSpan.Zero);
    }
    }

    if (cacheCallAtt == null) return default(TResult);

    return (TResult) HttpRuntime.Cache[key];
    }
  • Refactor it!
  • Gravatar
    Code to Generate Physical Data Model
    by usmanazmat on 8/18/2008 11:36:43 PM
  • namespace QueryAnalyzer
    {
    public partial class PDM : Form
    {
    public PDM()
    {
    InitializeComponent();
    }

    private void btnPDM_Click(object sender, EventArgs e)
    {
    bool AddRelations = false;
    StringBuilder sb = new StringBuilder();
    sb.Append(HTMLConstantsForPDM.HEADER);

    int SchemaCounter = 0;


    List<string> lstSchema = Database.GetAllSchemas();
    Dictionary<string, List<Classes.Table>> hashTable = Database.GetAllTablesNew();
    Dictionary<string, List<Classes.Column>> hashColumn = Database.GetAllColumnsforPDM();
    Dictionary<string, List<Classes.Relations>> hashRelations = Database.GetAllRelations();

    // this Function Merge 4 different Schema to AIL Schema may be their was some error in schema name
    SchemaCounter = MergeModulesInAIL(sb, SchemaCounter, lstSchema, hashTable, hashColumn, hashRelations, AddRelations);


    // Logic is quite Simple Create Module Definition i-e represented by, Schema, List Entities in it and then all Attributes Entities Wise and then Relations in one Schema

    foreach (string Schema in lstSchema)
    {
    SchemaCounter++;
    CreateModuleDefinition(sb, SchemaCounter, Schema);

    foreach (Classes.Table table in hashTable[Schema])
    {
    CreateEntityRow(sb, table);
    }

    sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModule); // ->


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    CreateAttributesModuleDefinition(sb, SchemaCounter, Schema);

    foreach (Classes.Table table in hashTable[Schema])
    {
    CreateEntityDefinition(sb, table);

    foreach (Classes.Column column in hashColumn[Schema + table.TableName])
    {
    CreateAttributeRow(sb, column);

    }
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntity); // 9
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (AddRelations)
    {
    if (hashRelations.ContainsKey(Schema))
    {
    CreateRelationsDefinition(sb, SchemaCounter, Schema);

    foreach (Classes.Relations relations in hashRelations[Schema])
    {
    CreateRelationRow(sb, relations);
    }

    //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModule); // 7
    }
    }
    }

    sb.Append(HTMLConstantsForPDM.FOOTER);


    if (sfd.ShowDialog() == DialogResult.OK)
    {
    using (StreamWriter sw = new StreamWriter(sfd.FileName))
    {
    sw.Write(sb.ToString());
    }
    }
    }

    #region MergeModulesInAIL
    private static int MergeModulesInAIL(StringBuilder sb, int SchemaCounter, List<string> lstSchema, Dictionary<string, List<Classes.Table>> hashTable, Dictionary<string, List<Classes.Column>> hashColumn, Dictionary<string, List<Classes.Relations>> hashRelations, bool AddRelations)
    {
    string ModuleAIL = "AIL";
    SchemaCounter++;
    string establishment = lstSchema[0];
    lstSchema.RemoveAt(0);

    string firms = lstSchema[0];
    lstSchema.RemoveAt(0);

    string ngo = lstSchema[0];
    lstSchema.RemoveAt(0);

    string socities = lstSchema[0];
    lstSchema.RemoveAt(0);



    // Get All the Tables of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES
    List<Classes.Table> TableList = new List<Table>();
    TableList.AddRange(hashTable[establishment]);
    TableList.AddRange(hashTable[firms]);
    TableList.AddRange(hashTable[ngo]);
    TableList.AddRange(hashTable[socities]);

    // Get All the Relations of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES
    List<Classes.Relations> RelationList;

    if (AddRelations)
    {
    RelationList = new List<Relations>();
    RelationList.AddRange(hashRelations[establishment]);
    RelationList.AddRange(hashRelations[firms]);
    RelationList.AddRange(hashRelations[ngo]);
    RelationList.AddRange(hashRelations[socities]);
    }

    // Get All the Columns of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES

    List<Classes.Column> ColumnList = new List<Column>();

    foreach (Classes.Table table in TableList)
    {
    ColumnList.AddRange(hashColumn[table.Schema + table.TableName]);

    }


    CreateModuleDefinition(sb, SchemaCounter, ModuleAIL);

    foreach (Classes.Table table in TableList)
    {
    CreateEntityRow(sb, table);

    }

    sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModule); // ->


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    CreateAttributesModuleDefinition(sb, SchemaCounter, ModuleAIL);


    foreach (Classes.Table table in TableList)
    {
    CreateEntityDefinition(sb, table);

    foreach (Classes.Column column in ColumnList)
    {
    // Generate 5.2 Identified Attributes
    if (table.TableName == column.TableName && table.Schema == column.Schema)
    {
    CreateAttributeRow(sb, column);
    }
    }

    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntity); // 9
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (AddRelations)
    {
    //if (RelationList.Count > 0)
    //{
    // CreateRelationsDefinition(sb, SchemaCounter, ModuleAIL);

    // foreach (Classes.Relations relations in RelationList)
    // {
    // CreateRelationRow(sb, relations);
    // }

    // //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModule); // 7
    //}
    }


    return SchemaCounter;
    }

    #endregion MergeModulesInAIL

    #region Relation Row
    private static void CreateRelationRow(StringBuilder sb, Classes.Relations relations)
    {
    // 5.3 Identified Relations
    //sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeEntitiesName); // 4
    //sb.Append(relations.EntityRelation); // ->
    //sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeRelationName); // 5
    //sb.Append(relations.RelationName); // ->
    //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterRelationName); // 6
    }
    #endregion Relation Row

    #region Create Attribute Row
    private static void CreateAttributeRow(StringBuilder sb, Classes.Column column)
    {
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeAttributeName); // 6
    sb.Append(column.ColumnName); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeDataType); // 7
    sb.Append(column.DataType); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeAttributeType); // 8
    sb.Append(column.Key); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterAttributeType); // 9
    }
    #endregion Attribute Row

    #region Attribute Module Definition
    private static void CreateAttributesModuleDefinition(StringBuilder sb, int SchemaCounter, string SchemaName)
    {
    // Attributes
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeNumber); // 1
    sb.Append(SchemaCounter); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterNumber_BeforeModuleName); // 2
    sb.Append(SchemaName); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterNumber_AfterModuleName); // 3
    }
    #endregion Attribute Module Definition

    #region Create Entity Row
    private static void CreateEntityRow(StringBuilder sb, Classes.Table table)
    {
    // Generate 5.1 Identified Entities
    sb.Append(HTMLConstantsForPDM.ENTITIES_BeforeEntityName); // 4
    sb.Append(table.TableName); // ->
    sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityName); // 5
    //sb.Append(table.EntityNumber); // ->
    //sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityNumber); // 6
    //sb.Append(table.Type); // ->
    //sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityType); // 7
    }
    #endregion Create Entity Row

    #region Create Relations Definition
    private static void CreateRelationsDefinition(StringBuilder sb, int SchemaCounter, string SchemaName)
    {
    // Relations
    //sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeNumber); // 1
    //sb.Append(SchemaCounter); // ->
    //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterNumber_BeforeModuleName); // 2
    //sb.Append(SchemaName); // ->
    //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModuleName); // 3
    }
    #endregion Create Relations Definition

    #region Create Entity Definition
    private static void CreateEntityDefinition(StringBuilder sb, Classes.Table table)
    {
    // Generate 5.2 Identified Attributes
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeEntityName); // 4
    sb.Append(table.TableName); // ->
    sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntityName); // 5
    }
    #endregion Create Entity Definition

    #region Create Module Definition
    private static void CreateModuleDefinition(StringBuilder sb, int SchemaCounter, string ModuleName)
    {
    // Generate Document for each module
    // 5. ARMS Module

    // Schama
    sb.Append(HTMLConstantsForPDM.MODULE_BeforeNumber); // 1
    sb.Append(SchemaCounter); // ->
    sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_BeforeModuleName); // 2
    sb.Append(ModuleName); // ->
    sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_AfterModuleName_BeforeDescModuleName); // 3
    sb.Append(ModuleName); // ->
    sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_AfterModuleName_AfterDescModuleName); // 4

    // Entities
    sb.Append(HTMLConstantsForPDM.ENTITIES_BeforeNumber); // 1
    sb.Append(SchemaCounter); // ->
    sb.Append(HTMLConstantsForPDM.ENTITIES_AfterNumber_BeforeModuleName); // 2
    sb.Append(ModuleName); // ->
    sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModuleName); // 3
    }
    #endregion Create Module Definition

    }
    }
  • Gravatar
    plz ignore the above code ..... I didn't know the use of this site .... i assumed that it will refactor my code and i was just testing it, is there any way to delete it?
    by usmanazmat on 8/18/2008 11:42:33 PM
Please log in to refactor the code! Login