Skip to content

Not able to invoke OnEntry and OnSucess method #16

@Rahul9179

Description

@Rahul9179

I am using KingAOP for caching purpose. so I am creating a CustomAttribue class and override the OnEntry and OnSucess method.

I am sharing codes we have used in our application. Can you please suggest me how can invoke these methods.

[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public class CacheAttribute : OnMethodBoundaryAspect
{
// This field will be set by CompileTimeInitialize and serialized at build time, then deserialized at runtime.
private string cacheKey;
private List dependentTableNames;
private static readonly ConcurrentDictionary<string, object> wosDataCache = new ConcurrentDictionary<string, object>();

    public CacheAttribute(WOSDbTable dependentTableName, params WOSDbTable[] dependentTableNames)
    {
        this.dependentTableNames = new List<WOSDbTable>();
        this.dependentTableNames.Add(dependentTableName);
        this.dependentTableNames.AddRange(dependentTableNames.Select(tableName => tableName));
    }

      public override void OnEntry(MethodExecutionArgs args)
    {
        object readValue;
        wosDataCache.TryGetValue(cacheKey, out readValue);
        if (readValue != null)
        {
            args.ReturnValue = readValue;
            args.FlowBehavior = FlowBehavior.Return;
        }
    }
    
    public override void OnSuccess(MethodExecutionArgs args)
    {
        if (new DBConnectionCore().RegisterSqlDependency(SqlDependencyChanged, dependentTableNames).Result)
        {
            wosDataCache.TryAdd(cacheKey, args.ReturnValue);
        }
    }

}

public class StaticDataContext : IStaticDataContext, IDynamicMetaObjectProvider where T : IStaticDataModel
{
[Cache(WOSDbTable.LanguageMaster, WOSDbTable.TranslationKeyMaster, WOSDbTable.TranslationMaster)]
public async Task<List> GetStaticDataAsync()
{
DBConnectionCore dbConnectionCore = new DBConnectionCore();
using (IDataReader dataReader = await dbConnectionCore.ExecuteReaderAsync(WOSDbStoredProc.USP_GetStaticData).ConfigureAwait(false))
{
return await dataReader.ToFlatModelAsync().ConfigureAwait(false);
}
}

    public DynamicMetaObject GetMetaObject(Expression parameter)
    {
        return new AspectWeaver(parameter, this);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions