namespace UnityDynaCache
{
    using System;

    using DynaCache;

    using Microsoft.Practices.Unity;

    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer()
                .RegisterInstance(typeof(IDynaCacheService), new MemoryCacheService())
                .RegisterType(typeof(IMyService), Cacheable.CreateType<MyService>())
                .RegisterType(typeof(IMyRepository), Cacheable.CreateType<MyRepository>());

            var service = container.Resolve<IMyService>();

            Console.WriteLine(service.DoSomething(123));
        }
    }

    public interface IMyService
    {
        string DoSomething(int value);
    }

    public class MyService : IMyService
    {
        private readonly IMyRepository repository;

        public MyService(IMyRepository repository)
        {
            this.repository = repository;
        }

        public string DoSomething(int value)
        {
            return this.repository.DoSomething(value);
        }
    }

    public interface IMyRepository
    {
        string DoSomething(int value);
    }

    public class MyRepository : IMyRepository
    {
        [CacheableMethod(100)]
        public virtual string DoSomething(int value)
        {
            return value.ToString();
        }
    }
}

Last edited Jun 27, 2014 at 10:28 PM by MikeGoatly, version 2

Comments

Divakershukla Aug 14, 2015 at 12:16 PM 
am getting following error when I use this with Unity.

{"Resolution of the dependency failed, type = \"UnityDynaCache.IMyService\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: ArgumentNullException - Value cannot be null.\r\nParameter name: source\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving UnityDynaCache.MyService,(none) (mapped from UnityDynaCache.IMyService, (none))\r\n Resolving parameter \"repository\" of constructor UnityDynaCache.MyService(UnityDynaCache.IMyRepository repository)\r\n Resolving CacheableMyRepository,(none) (mapped from UnityDynaCache.IMyRepository, (none))\r\n"}

Please help.... or attach a sample code...

MikeGoatly Sep 9, 2013 at 8:21 PM 
Thanks for that - I've just pushed out a version of DynaCache that includes the CreateType(Type) overload.

mong Sep 5, 2013 at 11:07 PM 
Here's how I've been using Dynacache with Autofac:

// on startup:
var builder = new ContainerBuilder();
builder.RegisterType<MemoryCacheService>().As<IDynaCacheService>().SingleInstance();
builder.RegisterType(Cacheable.CreateType<MyRepository>()).As<IMyRepository>();
var container = builder.Build();


ps. Can we have a `CreateType(Type type)` method, so I can do this:
var assembly = typeof(MyRepository).Assembly;
foreach (var type in assembly.GetTypes().Where(t => t.Name.EndsWith("Repository")))
{
builder.RegisterType(Cacheable.CreateType(type)).AsImplementedInterfaces()
}