c# - 如何将服务注入(inject)AutoMapper?

标签 c# asp.net-mvc unity3d automapper inversion-of-control

我正在使用 AutoMapper 将实体映射到 ViewModel。实体的属性之一是日期时间。我想使用 TimeZone 将该日期时间转换为本地日期时间。 TimeZone 存储在用户的 session 对象中。我已经有了 ISessionManager,它可以从用户 session 中检索信息。 我不确定如何将此 ISessionManager 注入(inject) AutoMapper

在下面的代码中,我如何将 ISessionManager.CurrentTimeZone 属性传递给 FromUTCToLocal() 方法?

    public class SessionManager:ISessionManager
    {
       public TimeZoneInfo CurrentTimeZone
       {
          return (TimeZoneInfo)Session["CurrentTimeZone"];
       }
    }


    public static class DateTimeExtenstions
    {
        public static DateTime FromLocalToUTC(this DateTime dateTime, TimeZoneInfo timeZone)
        {
            // Here instead of taking timeZone as parameter i can
            // use servicelocator to get instance of ISessionManager
            // like var timeZone = ServiceLocator.GetInstance<ISessionManager>()
            // However i don't want to use servicelocator pattern since
            // everywhere else i am using IOC
            // also it doesnt make sense to use ServiceLocator inside extension method

            return TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone);
        }        
    }

    public class AutoMapperConfig
    {
        public static IMapper Config()
        {
            var config = new MapperConfiguration(cfg =>
            {
                //Create all maps here               
                cfg.CreateMap<MyEntity, MyViewModel>()
                    .ForMember(vm => vm.CreatedDateTime,
                            y => y.MapFrom(entity => entity.CreatedOn.FromUTCToLocal()))
            });

            return config.CreateMapper();
        }
    }   


    public static class UnityConfig
    {
        public static void RegisterComponents(IMapper mapper)
        {
            var container = new UnityContainer();

            container.RegisterType<ISessionManager, SessionManager>(new HierarchicalLifetimeManager());

            container.RegisterInstance(mapper);            

            UnityServiceLocator locator = new UnityServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => locator);
        }
    }

   public class MvcApplication : System.Web.HttpApplication
   {
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        UnityConfig.RegisterComponents(AutoMapperConfig.Config());                
    }
   }

最佳答案

我认为这是一种反模式,因为映射器应该是愚蠢的,因此您可能想要抵制在其中放置过多逻辑的诱惑。查询数据是您的域实现的责任,但如果您直到现在都没有使用 ServiceLocator(尤其是在扩展方法中),那么您不使用 ServiceLocator 是正确的。

也许您应该丰富您映射的域对象,而不是在映射过程中尝试添加缺失的信息 (TimeZoneInfo)。

关于c# - 如何将服务注入(inject)AutoMapper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421092/

相关文章:

c# - 自修改 C# (MSIL) 代码?

c# - 如何在 asp.net webforms 中正确实现 PRG 模式

c# - 如何在 Web Api 的主体中传递派生的 JObject 类

jquery - 有没有办法使用 MVC 4 验证来实现 Twitter Bootstrap?而不是使用jquery?

c# - 服务总线 : Can't receive any message in first 15-20 sec, 然后就可以工作了

c# - Request.UserAgent 和 Request.Browser 有什么区别?

c# - 安装了两个网格包: The call is ambiguous between the following methods or properties

c# - 倒计时文本字段在倒计时期间未更新

c# - 将文件位置集合添加到列表的正确方法是什么?

unity3d - 为什么我不能播放预览我的简单 Unity 场景?