Search code examples
code-readabilitydryioc

Lazy<T> with DryIoc, am I doing it right?


I need to be able to use DryIoc to create Lazy objects.

Because I'm working with a legacy application which use a God object and relies on a heavy Dictionary of complexe KeyPair<string, SomeAbstractType>, I'd like to replace each value with a Lazy<SomeAbstractType> counterpart and have the application load way faster.

I've done a POCO this way, but since I'm far from mastering DryIoc, I might have overdone it.

Here's the code I hope you'll be able to read.

using System;
using System.Diagnostics;
using DryIoc;

namespace ConsoleApplication2
{
    public class Program
    {
        public class Postoned
        {
            private readonly IContainer _container;

            public Postoned(IContainer container)
            {
                _container = container;
            }

            public  Lazy<T> Create<T>()
            {
                return new Lazy<T>(() =>
                {
                    Debugger.Break();

                    return _container.Resolve<T>();

                });
            }
        }

        public class Holder
        {
            public Lazy<int> Li { get; set; }
            public Lazy<string> Ls { get; set; }

            public Holder(Lazy<int> li, Lazy<string> ls)
            {
                Li = li;
                Ls = ls;
            }
        }

        static void Main(string[] args)
        {
            var c = new Container();

            c.RegisterInstance(c);
            c.RegisterMapping<IContainer, Container>();

            c.RegisterInstance(66, Reuse.Singleton);
            c.RegisterInstance("string 66", Reuse.Singleton);
            c.Register(Made.Of(() => new Postoned(Arg.Of<IContainer>())), Reuse.Singleton);

            c.Register(Made.Of(r => ServiceInfo.Of<Postoned>(), (Postoned postoned) => postoned.Create<int>()), Reuse.Singleton);
            c.Register(Made.Of(r => ServiceInfo.Of<Postoned>(), (Postoned postoned) => postoned.Create<string>()), Reuse.Singleton);

            c.Register(Made.Of(() => new Holder(Arg.Of<Lazy<int>>(), Arg.Of<Lazy<string>>())), Reuse.Singleton);

            var holder = c.Resolve<Holder>();
            Debugger.Break();
            var li = holder.Li;
            Debugger.Break();
            var i = li.Value;

            Console.WriteLine(i);

            Console.WriteLine(holder.Ls.Value);

            Console.ReadLine();
        }
    }
}

EDIT [answer] :

It can be achieved a way simplier way XD.

using System;
using System.Diagnostics;
using DryIoc;

namespace ConsoleApplication2
{
    public class Program
    {
        public class Holder
        {
            public Lazy<int> Li { get; set; }
            public Lazy<string> Ls { get; set; }

            public Holder(Lazy<int> li, Lazy<string> ls)
            {
                Li = li;
                Ls = ls;
            }
        }

        static void Main(string[] args)
        {
            var c = new Container();

            c.RegisterInstance(c);
            c.RegisterMapping<IContainer, Container>();

            c.RegisterInstance(66, Reuse.Singleton);
            c.RegisterInstance("string 66", Reuse.Singleton);

            c.Register(Made.Of(() => new Holder(Arg.Of<Lazy<int>>(), Arg.Of<Lazy<string>>())), Reuse.Singleton);

            var holder = c.Resolve<Holder>();
            Debugger.Break();
            var li = holder.Li;
            Debugger.Break();
            var i = li.Value;

            Console.WriteLine(i);

            Console.WriteLine(holder.Ls.Value);

            Console.ReadLine();
        }
    }
}

Solution

  • DryIoc natively sypports injection of Lazy wrapper of service.

    Does it work for you?