Example #1
0
        public void TestZipCodeFactoryWorks()
        {
            // Act
            InMemoryZipCodeService provider = ZipCodeProviderFactory.ZipCodeProvider;

            // assert
            Assert.NotNull(provider);

            ZipCodeInfo appletonZip = provider.GetZipCode("54911");

            Assert.NotNull(appletonZip);
            Assert.Equal("APPLETON", appletonZip.City);
        }
        public static void Configure()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            // Zip Code Service
            ZipCodeReader reader         = new ZipCodeReader();
            String        filename       = Path.Combine(HttpRuntime.BinDirectory, "zipcodes.csv");
            var           zipCodeService = new InMemoryZipCodeService(reader.LoadZipCodes(filename));

            builder.RegisterInstance <IZipCodeService>(zipCodeService).As <IZipCodeService>();

            //builder.RegisterType<WeatherUndergroundDriver>().As<IWeatherClient>();
            //builder.RegisterType<NoaaWeatherDriver>().As<IWeatherClient>();


            // For Decorators
            // http://docs.autofac.org/en/latest/advanced/adapters-decorators.html
            // Register the services to be decorated. You have to
            // name them rather than register them As<IWeatherClient>()
            // so the *decorator* can do the registration for As<IWeatherClient>()

            //builder.RegisterType<WeatherUndergroundDriver>().Named<IWeatherClient>("weather");
            builder.RegisterType <NoaaWeatherDriver>().Named <IWeatherClient>("weather");

            // Register the decorator. The decorator uses the named registrations to get the items to wrap.
            builder.RegisterDecorator <IWeatherClient>(
                (c, inner) => new WeatherClientCacheDecorator(inner),
                fromKey: "weather");



            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }