果园将服务注入Automapper解析器

时间:2016-03-22 17:22:38

标签: c# automapper orchardcms autofac

我想知道我们当前是否可以访问在应用程序生命周期内可用的autofac容器对象。我正在尝试为在Orchard内容类型之外设置的实体设置Automapper,但我现在无法创建自定义类型解析器,因为它们需要访问autofac容器。

这是我正在尝试做的事情:

 public class OrchardShellEvents : IOrchardShellEvents
    {
        public void Activated()
        {
            Mapper.Initialize(x =>
            {
                x.ConstructServicesUsing(type => DependencyResolver.Current.GetService(type));
            });
        }

        public void Terminating()
        {
            //do nothing
        }
    }

这将允许我使用autofac服务将依赖项注入到automapper TypeConverters中,如下所示:

公共类PurchaserInfoConverter:ITypeConverter {     private readonly IRepository _stateRepo;     public PurchaserInfoConverter(IRepository stateRepo)     {         _stateRepo = stateRepo;     }

public PurchaserInfoRecord Convert(ResolutionContext context)
{
    var viewModel = context.SourceValue as PurchaserInfoViewModel;

    return new PurchaserInfoRecord
    {
        Address = viewModel.Address,
        City = viewModel.City,
        Company = viewModel.Company,
        Address2 = viewModel.Address2,
        Email = viewModel.Email,
        FirstName = viewModel.FirstName,
        LastName = viewModel.LastName,
        Phone = viewModel.Phone,
        State = viewModel.StateId.HasValue ? _stateRepo.Get(viewModel.StateId.Value) : null,
        Zip = viewModel.Zip,
        Fax = viewModel.Fax,
        Title = viewModel.Title
    };
}

}

但遗憾的是,我无法弄清楚如何访问持续应用程序生命周期的容器对象。有可能这样做吗?

0 个答案:

没有答案