Example #1
0
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["CommerceObjectContext"].ConnectionString;

            var productRepository  = new SqlProductRepository(connectionString);
            var basketRepository   = new SqlBasketRepository(connectionString);
            var discountRepository = new SqlDiscountRepository(connectionString);

            var discountPolicy = new RepositoryBasketDiscountPolicy(discountRepository);

            var basketService = new BasketService(basketRepository, discountPolicy);

            var currencyProvider = new CachingCurrencyProvider(new SqlCurrencyProvider(connectionString), TimeSpan.FromHours(1));

            if (controllerType == typeof(BasketController))
            {
                return(new BasketController(basketService, currencyProvider));
            }

            if (controllerType == typeof(HomeController))
            {
                return(new HomeController(productRepository, currencyProvider));
            }

            return(base.GetControllerInstance(requestContext, controllerType));
        }
 public void SutIsDiscountRepository(SqlDiscountRepository sut)
 {
     // Fixture setup
     // Exercise system
     // Verify outcome
     Assert.IsAssignableFrom<DiscountRepository>(sut);
     // Teardown
 }
 public void SutIsDiscountRepository(SqlDiscountRepository sut)
 {
     // Fixture setup
     // Exercise system
     // Verify outcome
     Assert.IsAssignableFrom <DiscountRepository>(sut);
     // Teardown
 }
Example #4
0
        public void OriginalCommerceExampleFragment(string connectionString)
        {
            var discountRepository =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(
                    discountRepository);
        }
 public void ProductsIsNotNull(SqlDiscountRepository sut)
 {
     // Fixture setup
     // Exercise system
     var result = sut.Products;
     // Verify outcome
     Assert.NotNull(result);
     // Teardown
 }
Example #6
0
        public void OriginalCommerceExampleFragment(string connectionString)
        {
            var discountRepository =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(
                    discountRepository);
        }
Example #7
0
        public void TwoConsumersSharingOneDependency(string connectionString)
        {
            var repository =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(repository);

            var campaign = new DiscountCampaign(repository);
        }
        public void ProductsIsNotNull(SqlDiscountRepository sut)
        {
            // Fixture setup
            // Exercise system
            var result = sut.Products;

            // Verify outcome
            Assert.NotNull(result);
            // Teardown
        }
Example #9
0
        public void TwoConsumersSharingOneDependency(string connectionString)
        {
            var repository =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(repository);

            var campaign = new DiscountCampaign(repository);
        }
Example #10
0
        public void TwoConsumersRequiringTheSameDependencyButConsumingSeperateInstance(string connectionString)
        {
            var repositoryForPolicy =
                new SqlDiscountRepository(connectionString);
            var repositoryForCampaign =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(
                    repositoryForPolicy);

            var campaign =
                new DiscountCampaign(repositoryForCampaign);
        }
Example #11
0
        public void TwoConsumersRequiringTheSameDependencyButConsumingSeperateInstance(string connectionString)
        {
            var repositoryForPolicy =
                new SqlDiscountRepository(connectionString);
            var repositoryForCampaign =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(
                    repositoryForPolicy);

            var campaign =
                new DiscountCampaign(repositoryForCampaign);
        }
Example #12
0
        internal static BasketManager ResolveManager()
        {
            BasketRepository basketRepository =
                new SqlBasketRepository(BasketContainer.connectionString);
            DiscountRepository discountRepository =
                new SqlDiscountRepository(BasketContainer.connectionString);

            BasketDiscountPolicy discountPolicy =
                new RepositoryBasketDiscountPolicy(discountRepository);

            IBasketService basketService =
                new BasketService(basketRepository, discountPolicy);

            return(new BasketManager(basketService));
        }
Example #13
0
        public IController ResolveHomeController()
        {
            var connStr = ConfigurationManager.
                          ConnectionStrings["CommerceObjectContext"].
                          ConnectionString;
            // acts as a locally scoped singleton
            var repository = new SqlDiscountRepository(connStr);

            // instance of repository is only shared with objects in this graph,
            // and not others
            var discountCampaign = new DiscountCampaign(repository);
            var discountPolicy   = new RepositoryBasketDiscountPolicy(repository);

            return(new HomeController(discountCampaign, discountPolicy));
        }
        public IController ResolveHomeController()
        {
            var connStr = ConfigurationManager
                          .ConnectionStrings["CommerceObjectContext"]
                          .ConnectionString;
            var repository =
                new SqlDiscountRepository(connStr);

            var discountCampaign =
                new DiscountCampaign(repository);
            var discountPolicy =
                new RepositoryBasketDiscountPolicy(repository);

            return(new HomeController(discountCampaign, discountPolicy));
        }
Example #15
0
        protected virtual DiscountRepository ResolveDiscountRepository()
        {
            var repository = HttpContext.Current
                             .Items["DiscountRepository"]
                             as DiscountRepository;

            if (repository == null)
            {
                var connStr = ConfigurationManager
                              .ConnectionStrings["CommerceObjectContext"]
                              .ConnectionString;
                repository = new SqlDiscountRepository(connStr);
                HttpContext.Current
                .Items["DiscountRepository"] = repository;
            }

            return(repository);
        }
        protected override IController GetControllerInstance(
            RequestContext requestContext, Type controllerType)
        {
            // Creates the dependencies
            string connectionString =
                ConfigurationManager.ConnectionStrings
                ["CommerceObjectContext"].ConnectionString;

            var productRepository =
                new SqlProductRepository(connectionString);
            var basketRepository =
                new SqlBasketRepository(connectionString);
            var discountRepository =
                new SqlDiscountRepository(connectionString);

            var discountPolicy =
                new RepositoryBasketDiscountPolicy(discountRepository);

            var basketService =
                new BasketService(basketRepository, discountPolicy);

            var currencyProvider = new CachingCurrencyProvider(
                new SqlCurrencyProvider(connectionString),
                TimeSpan.FromHours(1));

            // returns when the appropriate controller is found
            if (controllerType == typeof(BasketController))
            {
                return(new BasketController(
                           basketService, currencyProvider));
            }
            if (controllerType == typeof(HomeController))
            {
                return(new HomeController(
                           productRepository, currencyProvider));
            }

            // handles types not specifically requested for by using their default constructor.
            // This uses bastard injection, and should be avoided in your own code base when possible
            return(base.GetControllerInstance(requestContext, controllerType));
        }
        protected virtual DiscountRepository ResolveDiscountRepository()
        {
            // checks if desired instance exists, if so it uses it
            // keeps a singleton lifestyle for all shared dependencies
            var repository = HttpContext.Current.
                             Items["DiscountRepository"] as DiscountRepository;

            // if desired instance doesn't exist create that instance
            // and associate it with the current web request
            if (repository == null)
            {
                var connStr = ConfigurationManager
                              .ConnectionStrings["CommerceObjectContext"]
                              .ConnectionString;
                repository = new SqlDiscountRepository(connStr);
                HttpContext.Current
                .Items["DiscountRepository"] = repository;
            }

            return(repository);
        }