Example #1
0
        public void FirstTest()
        {
            var carFactory = new Factory<Car>();
            var planeFactory = new Factory<Plane>();
            IProduct<Car> carProduct = carFactory.Create<Toyota>();
            IProduct<Plane> planeProduct = planeFactory.Create<Boeing>();
            carProduct.Operate();
            planeProduct.Operate();

            carProduct = carFactory.Create<Engine<Car>>();
            carProduct.Operate();
        }
Example #2
0
        public void CreateNew_ShouldInvokePassedCallbackWithNewEntity()
        {
            var factory = new Factory();
            var entity = factory.Create<TestEntity>(e => e.Name = "Explicit Value");

            Assert.Equal("Explicit Value", entity.Name);
        }
Example #3
0
        public void CreateNew_ShouldCreateInstanceOfTypeParameter()
        {
            var factory = new Factory();
            var entity = factory.Create<SimpleTestEntity>();

            Assert.NotNull(entity);
            Assert.IsAssignableFrom<SimpleTestEntity>(entity);
        }
Example #4
0
        public void CreateNew_ShouldPopulatePrimatives_WhenNotUsingFactoryFunction()
        {
            var factory = new Factory();
            factory.ValueObjects.RegisterFactoryFunction(() => "Expected");

            var entity = factory.Create<TestEntity>();

            Assert.Equal("Expected", entity.Name);
        }
Example #5
0
        public void CreateNew_ShouldPassItselfToRegisteredFactoryMethod_WhenFactoryMethodWantsIt()
        {
            Factory actual = null;
            var factory = new Factory();
            factory.Entities.RegisterFactoryFunction<TestEntity>((f) => { actual = f; return new TestEntity(); });

            factory.Create<TestEntity>();
            Assert.Same(factory, actual);
        }
Example #6
0
        public void CreateNew_ShouldInvokePassedCallbackWithNewEntity_WhenEntityIsCreatedWithRegisteredFunction()
        {
            var factory = new Factory();
            factory.Entities.RegisterFactoryFunction(() => new TestEntity { Name = "Initial" });

            var entity = factory.Create<TestEntity>(e => e.Name = "Explicit Value");

            Assert.Equal("Explicit Value", entity.Name);
        }
Example #7
0
        static void Main(string[] args)
        {
            var factory = new Factory("MyApp", new MyLogger());

            // Register the type.
            factory.Type("MyType", typeof(MyType));

            // Create an instance and pass in constructor arguments.
            var myFactoryCreatedObject = factory.Create<MyType>("MyType", "Hello", "Computer");

            Console.WriteLine(myFactoryCreatedObject.Message);
        }
Example #8
0
        static void Main(string[] args)
        {
            var factory = new Factory("MyApp", new MyLogger());

            // Auto register types.
            factory.AutoRegisterTypes();

            // Create an instance.
            var myFactoryCreatedObject = factory.Create<MyType>("MyType");

            Console.WriteLine(myFactoryCreatedObject.Message);
        }
 public FactoryTests_PropertySetting_WhenCreatedWithDefaultCtor()
 {
     factory = new Factory();
     entity = factory.Create<SimpleTestEntity>();
 }
Example #10
0
        public void CreateNew_ShouldThrowMissingMethodException_WhenTypeHasNoDefaultCtorAndNoCustomCreateMethod()
        {
            var factory = new Factory();

            var exception = Assert.Throws<MissingMethodException>(
                            () => factory.Create<SimpleTestEntityNoDefaultCtor>());

            var expectedMessage = string.Format(
                "Unable to create instance of {0} because the requested type does not define a parameterless constructor and no custom factory method was registered.",
                typeof(SimpleTestEntityNoDefaultCtor).FullName);

            Assert.Equal(expectedMessage, exception.Message);
        }
Example #11
0
        public void CreateNew_ShouldStillUseRegisteredFactoryFunction_WhenTypeDoesNotHaveADefaultCtor()
        {
            var factory = new Factory();
            factory.Entities.RegisterFactoryFunction(() => new SimpleTestEntityNoDefaultCtor("a"));

            var entity = factory.Create<SimpleTestEntityNoDefaultCtor>();
            Assert.NotNull(entity);
        }
Example #12
0
        public void CreateNew_ShouldUseRegisteredFactoryFunction()
        {
            var expectedEntity = new SimpleTestEntity();
            var factory = new Factory();
            factory.Entities.RegisterFactoryFunction(() => expectedEntity);

            var entity = factory.Create<SimpleTestEntity>();

            Assert.Same(expectedEntity, entity);
        }