简体   繁体   English

如何使用 Autofac 注册 NodaTime.ZonedClock

[英]how to register NodaTime.ZonedClock with Autofac

It is possible to register NodaTime.SystemClock like this: builder.Register(_ => SystemClock.Instance).As<IClock>().SingleInstance();可以像这样注册NodaTime.SystemClockbuilder.Register(_ => SystemClock.Instance).As<IClock>().SingleInstance(); , according to Pac0's comment here . ,根据此处Pac0 的评论。

How do I do the same for NodaTime.ZonedClock ?我如何为NodaTime.ZonedClock做同样的事情?

ZonedClock doesn't seem to have any singleton accessor like SystemClock does, so you have to call it's constructor. ZonedClock似乎没有像SystemClock那样的任何 singleton 访问器,所以你必须调用它的构造函数。 Using the same approach you used for SystemClock , it would look something like this:使用与SystemClock相同的方法,它看起来像这样:

builder
    .Register(_ => new ZonedClock(SystemClock.Instance, DateTimeZone.Utc, CalendarSystem.Gregorian))
    .As<IClock>()
    .SingleInstance();

You can adapt the parameters as you see fit (I added DateTimeZone.Utc and CalendarSystem.Gregorian as examples).您可以根据需要调整参数(我添加了DateTimeZone.UtcCalendarSystem.Gregorian作为示例)。

Keep in mind that, since both implement the same interface ( IClock ) you can't register both at the same time without using keyed registrations or something similar.请记住,由于两者都实现了相同的接口( IClock ),因此如果不使用键控注册或类似的东西,您就不能同时注册两者。 If you just want to use the ZonedClock however, this should not be a problem.但是,如果您只想使用ZonedClock ,这应该不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM