Ejemplo n.º 1
0
        private static IMapper GetMapper(Container container)
        {
            var mce = new MapperConfigurationExpression();

            mce.ConstructServicesUsing(container.GetInstance);


            mce.AddMaps(repoType.Assembly);
            mce.AddExpressionMapping();

            var mc = new MapperConfiguration(mce);

            mc.AssertConfigurationIsValid();

            return(new Mapper(mc, t => container.GetInstance(t)));
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(c => c.EnableEndpointRouting = false);
            services.AddOData();

            services.AddMvc(op =>
            {
                foreach (var formatter in op.OutputFormatters.OfType <ODataOutputFormatter>().Where(it => !it.SupportedMediaTypes.Any()))
                {
                    formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.mock-odata"));
                }

                foreach (var formatter in op.InputFormatters.OfType <ODataInputFormatter>().Where(it => !it.SupportedMediaTypes.Any()))
                {
                    formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.mock-odata"));
                }
            })
            .AddJsonOptions(op => { op.JsonSerializerOptions.IgnoreNullValues = true; });

            services.AddSingleton <IMapper>((sp) =>
            {
                var mce = new MapperConfigurationExpression();
                mce.ConstructServicesUsing(sp.GetService);

                mce.AddProfile(new EventProfile());
                mce.AddExpressionMapping();

                var mc = new MapperConfiguration(mce);
                mc.AssertConfigurationIsValid();

                return(new Mapper(mc, t => sp.GetService(t)));
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Some App", Version = "v1"
                });
            });

            services.AddDbContext <ReproContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("Default"));
            });
        }
Ejemplo n.º 3
0
        private static IMapper GetMapper(Container container)
        {
            var mce = new MapperConfigurationExpression();

            mce.ConstructServicesUsing(container.GetInstance);

            var profiles = _repoType.Assembly.ExportedTypes
                           .Where(type => !type.IsAbstract && _profileType.IsAssignableFrom(type))
                           .Select(x => Activator.CreateInstance(x))
                           .Cast <Profile>();

            mce.AddProfiles(profiles);
            mce.AddExpressionMapping();

            var mc = new MapperConfiguration(mce);

            mc.AssertConfigurationIsValid();

            return(new Mapper(mc, t => container.GetInstance(t)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Configures auto mapper.
        /// </summary>
        /// <param name="services"></param>
        private void ConfigureMapper(IServiceCollection services)
        {
            IMapper GetMapper(IServiceProvider serviceProvider)
            {
                var mce = new MapperConfigurationExpression();

                mce.ConstructServicesUsing(serviceProvider.GetService);

                mce.AddMaps(repoType.Assembly);
                mce.AddExpressionMapping();

                var mc = new MapperConfiguration(mce);

                // Throws an exception should any mapping be invalid.
                mc.AssertConfigurationIsValid();

                return(new Mapper(mc, t => serviceProvider.GetService(t)));
            }

            services.AddSingleton(provider => GetMapper(provider));
        }
Ejemplo n.º 5
0
        public static IMapper GetMapper()
        {
            var mce = new MapperConfigurationExpression();

            mce.ConstructServicesUsing(Activator.CreateInstance);

            var profileType = typeof(Profile);
            var profiles    = typeof(Repositorio <>).Assembly.ExportedTypes
                              .Where(type => !type.IsAbstract && profileType.IsAssignableFrom(type))
                              .Select(x => Activator.CreateInstance(x))
                              .Cast <Profile>()
                              .ToArray();

            mce.AddProfiles(profiles);
            mce.AddExpressionMapping();

            var mc = new MapperConfiguration(mce);

            mc.AssertConfigurationIsValid();

            return(new Mapper(mc));
        }