Ejemplo n.º 1
0
 static EfUowDigests()
 {
     AutoMapperConfig.Register(); //При первом создании Uow, будет настроенн AutoMapper
     try
     {
         AutoMapperConfig.AssertConfigurationIsValid();//Если настройки мапиинга не валидны будет выбрашенно исключение
     }
     catch (Exception e)
     {
         throw;
     }
 }
Ejemplo n.º 2
0
        protected void Application_Start()
        {
            log4net.Config.XmlConfigurator.Configure();
            AreaRegistration.RegisterAllAreas();
            // FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var automapperConfig = new AutoMapperConfig().RegisterMappings();

            automapperConfig.AssertConfigurationIsValid();

            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
            builder.RegisterFilterProvider();
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterModule <AutofacWebTypesModule>();
            builder.Register(c => automapperConfig.CreateMapper()).As <IMapper>();
            builder.RegisterType <CustomerRepository>().As <ICustomerRepository>();
            builder.RegisterType <CustomModelValidator>().As <IModelValidator>();
            builder.RegisterType <LogRepository>().As <ILogRepository>();

            builder.Register(c => new ChannelFactory <IProductCommand>("BasicHttpBinding_IProductCommand"));
            builder.Register(c => new ChannelFactory <ICustomer>("BasicHttpBinding_ICustomer"));
            builder.Register(c => new ChannelFactory <IProductQuery>("BasicHttpBinding_IProductQuery"));

            builder.Register(c =>
            {
                var factory             = c.Resolve <ChannelFactory <IProductCommand> >();
                var credentialBehaviour = factory.Endpoint.Behaviors.Find <ClientCredentials>();
                credentialBehaviour.UserName.UserName = ConfigurationManager.AppSettings["ProductCommand.Username"];
                credentialBehaviour.UserName.Password = ConfigurationManager.AppSettings["ProductCommand.Password"];
                return(factory.CreateChannel());
            }
                             ).As <IProductCommand>().UseWcfSafeRelease();


            builder.Register(c =>
            {
                var factory             = c.Resolve <ChannelFactory <ICustomer> >();
                var credentialBehaviour = factory.Endpoint.Behaviors.Find <ClientCredentials>();
                credentialBehaviour.UserName.UserName = ConfigurationManager.AppSettings["Customer.Username"];
                credentialBehaviour.UserName.Password = ConfigurationManager.AppSettings["Customer.Password"];
                return(factory.CreateChannel());
            }).As <ICustomer>().UseWcfSafeRelease();


            builder.Register(c =>
            {
                var factory             = c.Resolve <ChannelFactory <IProductQuery> >();
                var credentialBehaviour = factory.Endpoint.Behaviors.Find <ClientCredentials>();
                credentialBehaviour.UserName.UserName = ConfigurationManager.AppSettings["ProductQuery.Username"];
                credentialBehaviour.UserName.Password = ConfigurationManager.AppSettings["ProductQuery.Password"];
                return(factory.CreateChannel());
            }).As <IProductQuery>().UseWcfSafeRelease();


            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            ModelBinders.Binders.Add(typeof(HomeModel), new HomeModelBinder());
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //This goes in the startup
            var automapperConfig = new AutoMapperConfig().RegisterMappings();

            //Every single member has correlation with destination type.
            //It may not be the right one (since there are always exception cases),
            //but it at least tests that every property is moved from source type to destination.
            //will throw exception if not valid.
            automapperConfig.AssertConfigurationIsValid();

            //Get foo and bar entities  from database
            var fooEntity = new FooEntity()
            {
                EmployeeFName = "Foo", EmployeeLName = "Fooster"
            };
            var barEntity = new BarEntity()
            {
                PersonFirstName = "Bar", PersonLastName = "Cluster"
            };

            // auto map Entity to DTO
            var dtoFooModel = Mapper.Map(fooEntity, new FooDto(), typeof(FooEntity), typeof(DTO.FooDto));
            var dtoBarModel = Mapper.Map(barEntity, new BarDto(), typeof(BarEntity), typeof(DTO.BarDto));

            // Auto Map DTO Back to Entities
            var fooDTOFromUI = new FooDto()
            {
                EmployeeFName = "FooBackToEntity", EmployeeLName = "From Foo DTO"
            };
            var barDTOFromUI = new BarDto()
            {
                PersonFName = "BarBackToEntity", PersonLName = "FROM Bar DTO"
            };

            var entityFooModel = Mapper.Map(fooDTOFromUI, new FooEntity(), typeof(FooDto), typeof(FooEntity));
            var entityBarModel = Mapper.Map(barDTOFromUI, new BarEntity(), typeof(BarDto), typeof(BarEntity));

            //Get AddressEntity from database
            var addressEntity = new AddressEntity()
            {
                Street = "1751 Granger Cir",
                City   = "Castle Rock",
                State  = "CO",
                Zip    = "80109"
            };

            // auto map Entity to DTO
            var addressDto = new AddressDisplayOnlyDto();

            Mapper.Map(addressEntity, addressDto, typeof(AddressEntity), typeof(AddressDisplayOnlyDto));
            var p = addressDto.CompleteAddress;

            Console.WriteLine(p);
            Console.ReadKey();


            //Get Person and Address entity from database
            PersonEntity personEntity = new PersonEntity()
            {
                FirstName     = "Eric",
                LastName      = "Norton",
                PersonAddress = addressEntity
            };

            //Map person entity and personEntity.Address to personDto
            var dtoPersonModel = Mapper.Map(personEntity, new PersonDto(), typeof(PersonEntity), typeof(PersonDto));



            //Get Person DTO from UI
            PersonDto personDto = new PersonDto()
            {
                FName   = "Eric",
                LName   = "Nordin",
                Street  = "345 West Green St",
                City    = "Castle Rock",
                State   = "CO",
                ZipCode = "44565"
            };

            //Get Person DTO from UI and map back to person entity and address entity
            PersonEntity entityPersonModel = Mapper.Map(personDto, new PersonEntity(), typeof(PersonDto), typeof(PersonEntity)) as PersonEntity;
        }