Lazy<T> 和 DryIoc,我做对了吗?
Lazy<T> with DryIoc, am I doing it right?
我需要能够使用 DryIoc 创建 Lazy 对象。
因为我正在使用一个遗留应用程序,该应用程序使用上帝对象并依赖于大量复杂的字典 KeyPair<string, SomeAbstractType>
,我想用 Lazy<SomeAbstractType>
对应物替换每个值,并且让应用程序加载速度更快。
我已经通过这种方式完成了 POCO,但由于我还远没有掌握 DryIoc,所以我可能做得太过了。
这是我希望您能够阅读的代码。
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();
}
}
}
编辑[答案]:
可以通过更简单的方式实现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();
}
}
}
DryIoc 原生支持注入 Lazy wrapper of service。
对你有用吗?
我需要能够使用 DryIoc 创建 Lazy 对象。
因为我正在使用一个遗留应用程序,该应用程序使用上帝对象并依赖于大量复杂的字典 KeyPair<string, SomeAbstractType>
,我想用 Lazy<SomeAbstractType>
对应物替换每个值,并且让应用程序加载速度更快。
我已经通过这种方式完成了 POCO,但由于我还远没有掌握 DryIoc,所以我可能做得太过了。
这是我希望您能够阅读的代码。
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();
}
}
}
编辑[答案]:
可以通过更简单的方式实现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();
}
}
}
DryIoc 原生支持注入 Lazy wrapper of service。
对你有用吗?