Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Murray Roke 503 posts 966 karma points c-trib
    Dec 21, 2022 @ 22:41
    Murray Roke
    0

    DI Registration WithParameter

    Hi all,
    I wish to register an object with DI where one parameter comes from the container, and the other is specified at registration time.

    I have a class for which the constructor takes 2 parameters like so:

    public PriceCache(string relativeDiskCachePath, DynamicsPriceRepository priceRepo)
    

    In Umbraco 7 I could register that with the DI container like so:

            builder.RegisterType<DynamicsPriceRepository>();
            builder.RegisterType<PriceCache>()
                   .WithParameter("relativeDiskCachePath", "~/App_Data/DynamicsCache/Prices")
                   // other constructor parameter injected automatically.
                   .SingleInstance();
    

    How would I do this in Umbraco 8?

  • Murray Roke 503 posts 966 karma points c-trib
    Dec 21, 2022 @ 22:59
    Murray Roke
    0

    I got around it by making the string constructor param a type I could register instead of a string, And registered it like so:

            composition.Register<DynamicsPriceRepository>(Lifetime.Singleton);
            composition.RegisterUnique(new PriceCacheConfiguration(){RelativeDiskCachePath = "~/App_Data/DynamicsCache/Prices" });
            composition.Register<PriceCache>(Lifetime.Singleton);
    
Please Sign in or register to post replies

Write your reply to:

Draft