如何使用装饰器模式(Castle Windsor)在typedfactory上正确设置缓存?

时间:2013-06-08 19:47:20

标签: caching castle-windsor decorator typed-factory-facility

我正在使用Castle Windsor,我正在尝试使用装饰器模式在TypedFactory上设置缓存。它工作正常,直到我试图处理Windsor容器(关闭应用程序时)。基本上,我的问题是当我试图处理CachedFactory时,TypedFactory已经被处理掉了。

以下是我的问题的简化示例:

using System;
using System.Collections.Generic;
using System.Threading;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Setting up the container
            var container = new WindsorContainer();
            container.AddFacility<TypedFactoryFacility>();
            container.Register(
                Component.For<IItemFactory>().ImplementedBy<CachedItemFactory>(), //decorator pattern
                Component.For<IItemFactory>().AsFactory(),
                Component.For<IItem>().ImplementedBy<Item>().LifestyleTransient()
                );

            // Resolving
            var itemFactory = container.Resolve<IItemFactory>();

            // Desired behavior. Works as expected.
            IItem item1 = itemFactory.Create("Item1");
            IItem item2 = itemFactory.Create("Item2");
            IItem anotherItem1 = itemFactory.Create("Item1");
            Console.WriteLine("Item1 == Item2: {0}", item1 == item2); //false
            Console.WriteLine("Item1 == anotherItem1: {0}", item1 == anotherItem1); //true

            // Here is my problem. It throws ObjectDisposedException from _itemFactory in the Dispose function of CachedItemFactory
            container.Dispose();

            Console.WriteLine("End of program");
            Console.ReadKey();
        }
    }

    public interface IItem
    {
        string Name { get; }
    }

    public class Item : IItem
    {
        public Item(string name)
        {
            Name = name;
            Thread.Sleep(1000); //It takes time to create this object
        }

        public string Name { get; private set; }
    }

    public interface IItemFactory
    {
        IItem Create(string name);
        void Release(IItem item);
    }

    public class CachedItemFactory : IItemFactory, IDisposable
    {
        private readonly Dictionary<string, IItem> _cache = new Dictionary<string, IItem>();
        private readonly IItemFactory _itemFactory;

        public CachedItemFactory(IItemFactory itemFactory)
        {
            _itemFactory = itemFactory;
        }

        public IItem Create(string name)
        {
            if (!_cache.ContainsKey(name))
                _cache.Add(name, _itemFactory.Create(name));
            return _cache[name];
        }

        public void Release(IItem item)
        {
        }

        public void Dispose()
        {
            foreach (var item in _cache)
            {
                _itemFactory.Release(item.Value);
            }
            _cache.Clear();
        }
    }
}

我做错了什么想法?任何概念(架构)错误?

我尝试了以下但没有成功:

  1. 在CachedItemFactory之前注册TypedFactory(并将CachedItemFactory标记为IsDefault):not working,same error
  2. 手动实施基本工厂,而不是使用TypedFactory:同样的问题
  3. 这有效:

    1. 手动实现基本工厂,在CachedItemFactory之前注册它并将CachedItemFactory标记为IsDefault ......但感觉不对(脆弱)......
    2. 有任何意见吗?

      非常感谢!

1 个答案:

答案 0 :(得分:0)

您正在将缓存和类型记录注册为单例(隐式)。 如果您将TypeFactory注册为Transient,它可能(不完全确定)在缓存的工厂之后立即处理。

然而,由于您只是尝试清理工厂的已解析组件,您也可以简单地省略Dispose中的代码,因为键入的工厂将在发布时释放所有组件。