Giter Club home page Giter Club logo

Comments (4)

MichaCo avatar MichaCo commented on May 26, 2024

Hi @capqueen
Great question. Unfortunately, for now, you'd have to register all types of cache manager you might need.

But we can improve that. InjectionFactory has an overload which takes a func which provides also the target type which should be created. With that, we could create the needed type. Like that:

container.RegisterType(typeof(ICacheManager<>), new InjectionFactory((c, t, n) => CreateManager(t)));

The CreateManager method should look similar to this:

private static object CreateManager(Type type)
{
    var genericArgs = type.GetGenericArguments();
    if (!type.IsGenericType || genericArgs.Length != 1)
    {
        throw new InvalidOperationException("nope...");
    }

    var targetType = genericArgs.First();

    return CacheFactory.Build(targetType, "myCache", cfg => cfg.WithSystemRuntimeCacheHandle("h1"));

    // or   
    // return CacheFactory.FromConfiguration(targetType, "myCache", ConfigurationBuilder.LoadConfiguration("myCfg"));
}

The problem now is that CacheFactory does not provide overloads which take the type. Instead CacheFactory has generic methods for that. It is a little bit a hassle to call those generic methods via reflection if you have to work with Type (but it is doable)

I might add those overloads if I find some time, or if someone else wants to do it, would be fine, too.

from cachemanager.

capqueen avatar capqueen commented on May 26, 2024

Thanks a lot, I'll try

from cachemanager.

MichaCo avatar MichaCo commented on May 26, 2024

I just checked in some changes to CacheFactory and released and update on nuget.org.
Now you can use it with unity like this for example:

        UnityContainer container = new UnityContainer();
        container.RegisterType(typeof(ICacheManager<>),
            new ContainerControlledLifetimeManager(),
            new InjectionFactory((c, t, n) => 
                CacheFactory.Build(
                    t.GetGenericArguments()[0], 
                    "name", 
                    cfg => cfg.WithSystemRuntimeCacheHandle("h1"))));

We have to pass t.GetGenericArguments()[0], as the type and not just t. Why?
Because the type passed in to the Func is ICacheManager<T> and we have to pass only the Tยด to theBuildorFromConfiguration` method.

Instead of CacheFactory.Build in the example above you can of course also use CacheFactory.FromConfiguration(...).

from cachemanager.

capqueen avatar capqueen commented on May 26, 2024

wow, great work~ I have a question, why not cachemanager have api like

ICacheManager.Add<T> / Set<T> ...?

is it about some design reason?

from cachemanager.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.