1

我正在开发一个使用来自 Nuget 的 Automapper、工作单元、实体框架 5、ASPNET MVC 4、WebApi 和 Windsor Castle 的应用程序。

我不确定是否应该映射它或者它应该由 Automapper 自动映射

public IEnumerable<StoreDto> Get()
{
    return Uow.Stores.GetAll().OrderBy(s => s.Name);
}

Uow.Stores.GetAll().OrderBy(s => s.Name)返回一个IOrderedQueryable<Store>.

我收到错误消息

无法将表达式类型 IOrderedQueryable 转换为类型 IEnumerable

我应该做一个 foreach 并将每个返回的对象转换GetAll为AutomapperStoreDto的方法吗?Mapper.MapAutomapper 不会为我转换它吗?

这就是我使用 Automapper 以及注册它的方式。请让我知道是否应该/可以改进。

AutomapperInstaller.cs

 public class AutoMapperInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            Mapper.Initialize(x => x.ConstructServicesUsing(container.Resolve));

            RegisterProfilesAndResolvers(container);
            RegisterMapperEngine(container);
        }

        private void RegisterMapperEngine(IWindsorContainer container)
        {
            container.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));
        }

        private void RegisterProfilesAndResolvers(IWindsorContainer container)
        {
            // register value resolvers
            container.Register(AllTypes.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());

            // register profiles
            container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>());
            var profiles = container.ResolveAll<Profile>();

            foreach (var profile in profiles)
            {
                Mapper.AddProfile(profile);
            }
        }

WebWindsorInstaller

internal class WebWindsorInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component
                .For<RepositoryFactories>()
                .ImplementedBy<RepositoryFactories>()
                .LifestyleSingleton());

            container.Register(Component
                .For<IRepositoryProvider>()
                .ImplementedBy<RepositoryProvider>()
                .LifestylePerWebRequest());

            container.Register(Component
                .For<IProjUow>()
                .ImplementedBy<ProjUow>()
                .LifestylePerWebRequest());

            container.Register(Classes
                .FromAssemblyContaining<Api.StoresController>()
                .BasedOn<IHttpController>()
                .If(t => t.Name.EndsWith("Controller"))
                .LifestylePerWebRequest());

            RegisterMapping(container, store);
        }

        private void RegisterMapping(IWindsorContainer container, IConfigurationStore store)
        {
            Mapper.CreateMap<Store, StoreDto>();
        }
}

Store.cs

public class Store
    {
        public Store()
        {
            this.Branches = new List<Branch>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public System.Data.Spatial.DbGeography Location{ get; set; }

        public virtual ICollection<Branch> Branches{ get; set; }
    }

StoreDto.cs(现在和商店一样)

public class StoreDto
    {
        public StoreDto()
        {
            this.Branches = new List<BranchDto>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public System.Data.Spatial.DbGeography Location{ get; set; }

        public virtual ICollection<BranchDto> Branches{ get; set; }
    }

这就是我调用安装程序的方式IocConfig.cs

Container = new WindsorContainer()
                .Install(new AutoMapperInstaller())
                .Install(new ControllersInstaller());

我不确定我是否应该包括在内

.AddFacility<FactorySupportFacility>()
4

0 回答 0