一段时间后,AutoMapper停止工作

时间:2014-10-03 10:40:29

标签: c# asp.net azure fluent-nhibernate automapper

我的Asp.Net Webforms应用程序出了问题。她作为Azure云服务运行。技术:AutoMapper + NHibernate + IoC Unity。

发布应用程序的新版本时,所有属性展平功能都正常。事实证明,在启动应用程序的特定时间之后,AutoMapper不会自动加载这些属性。我没有得到任何异常消息。只是我的DTO中的财产不收取费用。

在结构销售中 - >订单 - >客户 - >地址 - > City,映射为OrderCustomerAddressCity,此属性在将来的协商中不再加载。但销售 - >通常会加载值(属性直接在根类上)。

以下是代码:

//Domain

public class Address 
{
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string PostalCode { get; set; }
}

public class Customer 
{
    public virtual string Name { get; set; }
    public virtual string Phone { get; set; }
    public virtual Address Address { get; set; }
}

public class Order 
{
    public virtual int OrderNumber { get; set; }
    public virtual DateTime OrderDate { get; set; }
    public virtual Customer Customer { get; set; }
}

public class Sale
{
    public virtual int SaleNumber { get; set; }
    public virtual DateTime SaleDate { get; set; }
    public virtual Order Order { get; set; }
}

//Application.DTO

public class SaleDTO
{
    public int SaleNumber { get; set; }
    public DateTime SaleDate { get; set; }
    public int OrderOrderNumber { get; set; }
    public DateTime OrderOrderDate { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress1 { get; set; }
    public string CustomerAddress2 { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerPostalCode { get; set; }
}

class ERPProfile: Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<Address,AddressDTO>();   
        Mapper.CreateMap<Customer,CustomerDTO>();            
        Mapper.CreateMap<Order,OrderDTO>();    
        Mapper.CreateMap<Sale,SaleDTO>()
            .ForMember(dest => dest.CustomerName,
                       opt => opt.MapFrom(src => src.Order.Customer.Name))
            .ForMember(dest => dest.CustomerAddress1,
                       opt => opt.MapFrom(src => src.Order.Customer.Address.Address1))
            .ForMember(dest => dest.CustomerAddress2,
                       opt => opt.MapFrom(src => src.Order.Customer.Address.Address2))
            .ForMember(dest => dest.CustomerCity,
                       opt => opt.MapFrom(src => src.Order.Customer.Address.City))
            .ForMember(dest => dest.CustomerState,
                       opt => opt.MapFrom(src => src.Order.Customer.Address.State))
            .ForMember(dest => dest.CustomerPostalCode,
                       opt => opt.MapFrom(src => src.Order.Customer.Address.PostalCode));
        Mapper.AssertConfigurationIsValid();
    }
}

//Global.asax

private static IUnityContainer _container;

public static IUnityContainer Container
{
    get
    {
        return _container;
    }
    private set
    {
        _container = value;
    }
}

protected void Application_Start(object sender, EventArgs e)
{
    CreateContainer();
}

protected virtual void CreateContainer()
{
    IUnityContainer container = new UnityContainer();
    Application.IoC.Unity.Bootstrapper.Configure(container);
    Container = container;
    Application.IoC.Unity.Bootstrapper.Initialize();
}

//Application.IoC.Unity

public static class Bootstrapper
{
    public static void Configure(IUnityContainer container)
    {
        container
            .RegisterType<ICustomerRepository, CustomerRepository>()    
            .RegisterType<IOrderRepository, OrderRepository>()    
            .RegisterType<ISaleRepository, SaleRepository>()    
            .RegisterType<ICustomerAppService, CustomerAppService>()    
            .RegisterType<IOrderAppService, OrderAppService>()    
            .RegisterType<ISaleAppService, SaleAppService>()    
            .RegisterType<ISessionManager, SessionManager>()    
            .RegisterType<IUnitOfWork, UnitOfWork>();

        container.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(
            new ContainerControlledLifetimeManager());
    }

    public static void Initialize()
    {
        var typeAdapterFactory = InjectType.Inject<ITypeAdapterFactory>();
        TypeAdapterFactory.SetCurrent(typeAdapterFactory);
    }
}

//Infrastructure.CrossCutting.Adapter

public class AutomapperTypeAdapterFactory : ITypeAdapterFactory
{
    public AutomapperTypeAdapterFactory()
    {
        var profiles = AppDomain.CurrentDomain
                                .GetAssemblies()
                                .SelectMany(a => a.GetTypes())
                                .Where(t => t.BaseType == typeof(Profile));

        Mapper.Initialize(cfg =>
        {
            foreach (var item in profiles)
            {
                if (item.FullName != "AutoMapper.SelfProfiler`2")
                    cfg.AddProfile(Activator.CreateInstance(item) as Profile);
            }
        });
    }

    public ITypeAdapter Create()
    {
        return new AutomapperTypeAdapter();
    }
}

public class AutomapperTypeAdapter : ITypeAdapter
{
    public TTarget Adapt<TSource, TTarget>(TSource source)
        where TSource : class
        where TTarget : class, new()
    {
        return Mapper.Map<TSource, TTarget>(source, options => options.CreateMissingTypeMaps = true);
    }

    public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
    {
        return Mapper.Map<TTarget>(source, options => options.CreateMissingTypeMaps = true);
    }
}

public static class TypeAdapterFactory
{
    static ITypeAdapterFactory _currentTypeAdapterFactory = null;

    public static void SetCurrent(ITypeAdapterFactory adapterFactory)
    {
        _currentTypeAdapterFactory = adapterFactory;
    }

    public static ITypeAdapter CreateAdapter()
    {
        return _currentTypeAdapterFactory.Create();
    }
}

public static class ProjectionsExtensionMethods
{
    public static List<TProjection> ProjectedAsCollection<TProjection>(this IEnumerable<Entity> items)
        where TProjection : class,new()
    {
        var adapter = TypeAdapterFactory.CreateAdapter();
        return adapter.Adapt<List<TProjection>>(items);
    }
}

//Application.Context.ERPModule

public class SaleAppService: ISalesAppService
{
    //... constructor ...

    public List<SaleDTO> FilterBySaleDate(DateTime startDate, DateTime endDate)
    {
        List<SaleDTO> searchResult;
        using (var uow = CreateUnitOfWork())
        {
            try
            {
                List<Sale> data;
                data = _saleRepository.FilterBy(sale => sale.SaleDate >= startDate && sale.SaleDate <= endDate).ToList();
                searchResult = data.ProjectedAsCollection<SaleDTO>();
                uow.Commit();
            }
            catch (Exception e)
            {
                uow.Rollback();
                throw;
            }
        }
        return searchResult;
    }
}

任何线索?

0 个答案:

没有答案