Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("#########################################");
            Console.WriteLine("# Welcome to workshop 5 | Testable Code #");
            Console.WriteLine("#########################################");
            Console.WriteLine();

            var db = new PostgresqlDatabase();

            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "John Doe"
            });
            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "Maggie Musterman"
            });
            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "Some Dude"
            });

            // ----------------------------------------------------------------
            // Optimize the code below ...
            var billingSystem = new BillingSystem(db, new CreditCardCharger());

            billingSystem.Init(); // bad!
            billingSystem.Process();
            // ----------------------------------------------------------------

            Console.WriteLine();
            Console.WriteLine("Good bye! See you next time!");
            Console.ReadLine();
        }
Exemplo n.º 2
0
 public void Setup()
 {
     this.db            = new PostgresqlDatabase(); // try to get rid of me
     this.chargerMock   = new Mock <ICharger>();
     this.billingSystem = new BillingSystem(this.db, this.chargerMock.Object);
     this.billingSystem.Init();
 }
        static void Main(string[] args)
        {
            Console.WriteLine("#########################################");
            Console.WriteLine("# Welcome to workshop 5 | Testable Code #");
            Console.WriteLine("#########################################");
            Console.WriteLine();

            var db = new PostgresqlDatabase();

            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "John Doe"
            });
            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "Maggie Musterman"
            });
            db.Customers.Add(new Customer {
                Id = Guid.NewGuid(), Name = "Some Dude"
            });

            // Replace this by AUTOFAC
            var database      = new PostgresqlDatabase();
            var billingSystem =
                new BillingSystem(
                    new CustomerRepository(database),
                    new SubscriptionRepository(database),
                    new CreditCardCharger());

            /*
             * USE AUTOFAC HERE!!
             *
             * var billingSystem = container.Resolve<BillingSystem>();
             */
            var containerConfig = new ContainerBuilder();

            containerConfig.RegisterType <CustomerRepository>().As <ICustomerRepository>();
            containerConfig.RegisterType <SubscriptionRepository>().As <ISubscriptionRepository>();
            containerConfig.RegisterType <CreditCardCharger>().As <ICharger>();

            // this tells Autofac to only create a single instance of PostgresqlDatabase and
            // use that one instance everytime a ICustomerDatabase or ISubscriptionDatabase
            // is needed
            containerConfig
            .RegisterType <PostgresqlDatabase>()
            .As <ICustomerDatabase>()
            .InstancePerLifetimeScope();
            containerConfig
            .RegisterType <PostgresqlDatabase>()
            .As <ISubscriptionDatabase>()
            .InstancePerLifetimeScope();

            billingSystem.Process();

            Console.WriteLine();
            Console.WriteLine("Good bye! See you next time!");
            Console.ReadLine();
        }
 public BillingSystem(PostgresqlDatabase db, ICharger charger)
 {
     this.db      = db;
     this.charger = charger; // good!
     // where is the subscription-repository?
 }