3

今天晚上我一直在玩自定义属性,看看我是否可以简化我的缓存层。我想出了以下几点:

namespace AttributeCreationTest
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
    public class Cache : Attribute
    {
        public Cache()
        {
            Length = "01h:30m";
        }

        public string Length;
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class CacheIdentifier : Attribute
    {
    }


    [Cache]
    class Class1
    {
        [CacheIdentifier]
        public int ID { get; set; }
    }

    class Class2
    {
        [CacheIdentifier]
        public bool ID { get; set; }
    }

    [Cache(Length = "01h:10m")]
    class Class3
    {
        [CacheIdentifier]
        public string ID { get; set; }
    }

    class Program
    {

        static void Main(string[] args)
        {
            var f1 = new Class1 { ID = 2 };
            var f2 = new Class2 { ID = false };
            var f3 = new Class3 { ID = "someID" };

            DoCache(f1);
            DoCache(f2);
            DoCache(f3);
        }

        public static void DoCache(object objectToCache)
        {
            var t = objectToCache.GetType();

            var attr = Attribute.GetCustomAttribute(t, typeof(Cache));

            if (attr == null) return;

            var a = (Cache)attr;
            TimeSpan span;

            if (TimeSpan.TryParse(a.Length.Replace("m", "").Replace("h", ""), out span))
            {
                Console.WriteLine("name: {0}, {1}", t.Name, span);

                ExtractCacheData(objectToCache);

                return;
            }

            throw new Exception(string.Format("The Length value of {0} for the class {1} is invalid.", a.Length, t.Name)); 
        }

        public static void ExtractCacheData(object o)
        {
            var t = o.GetType();

            foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (Attribute.IsDefined(prop, typeof(CacheIdentifier)))
                {
                    Console.WriteLine("   type: {0}, value {1}", prop.PropertyType, prop.GetValue(o));
                    break;
                }

                throw new Exception(string.Format("A CacheIdentifier attribute has not been defined for {0}.", t.Name));
            }
        }

    }
}

“缓存”属性将被充实,但我在学习 C# 的这一领域时将其保持在最低限度。我的想法是允许更轻松地缓存项目,包括指定缓存对象的时间量的简化方法。

这看起来好吗?使用这种模式将项目推送到缓存是否会对性能产生重大影响?

我还没有找到任何详细介绍这种想法的教程,所以任何建议都将不胜感激。

4

2 回答 2

2

您的实现似乎很好,但CacheAttribute对象将是每个属性声明一个,而不是每个Class1对象。换句话说,你将创建多少个对象并不重要——如果属性只被分配一次——具有Length属性的那个属性的对象将只是clr.

但是你为什么不想将DoCache一些接口传递给方法,你将定义一些方法来从对象中检索 CacheIdentifier 呢?这样的实现将更加健壮、准确、可读。当然,您需要在每个类中实现它——但我看不到在不指定其任何职责的情况下传递它有很多object好处DoCache。对于用户而言,此 API 不直观且难以理解。

另一点,如果你愿意的话,更高级 - 出于缓存目的,我建议总体上看面向方面的编程,特别是 PostSharp。这篇文章Postsharp 可以 SOLIDify 您的代码的 5 种方法:缓存为实现缓存方面提供了一个很好的案例。简而言之,PostSharp 还在编译的第二步通过 IL 编织使用属性和插入缓存行为

编辑:如果你想要漂亮和干净的模型,没有任何非域元素的扭曲,我建议另一种 AOP 技术,称为动态拦截。最流行的框架是LinFuCastle Dynamic Proxy。结果,您将通过构造函数指定依赖项,并且 IoC 容器将在解析这些依赖项时创建代理。但关键是这些模型对代理一无所知,并将在简单对象中使用它们。这有点类似于装饰者模式

于 2013-01-04T01:23:50.097 回答
2

您的用法基本正确,但您可能希望缓存结果。object您可以将类型传递给它,而不是传递您的实例。此外,为了避免每次都必须使用反射——这肯定不会加速程序的执行——你可以将类型及其相关属性保存在字典中,并在每次使用该方法时进行查找被调用,然后仅在查找失败时通过反射操作。

此外,不是强制性的,但我建议您遵循属性的命名约定

保持结果缓存的示例:

static readonly Dictionary<Type, Attribute> CacheAttributes = 
    new Dictionary<Type,Attribute>();

然后你会修改DoCache如下:

var t = objectToCache.GetType();
Attribute attr;
var success = CacheAttributes.TryGetValue(t, out attr);
if (!success)
{
    attr = Attribute.GetCustomAttribute(t, typeof (CacheAttribute));
    CacheAttributes[t] = attr;
}
if (attr == null) return;
var a = attr as CacheAttribute;
TimeSpan span;
//Continues with your code

您可以将相同的概念应用于ExtractCacheData.

于 2013-01-04T01:26:00.987 回答