Пример #1
0
        static void Main(string[] args)
        {
            var resolver = new InterfaceResolver("TinyDICConfig.txt");
            var sender   = resolver.Instantiate <IEmailSender>();

            sender.SendEmail("pippo", "pluto");
        }
Пример #2
0
        /// <summary>
        /// Handles incoming message in lidgren format.
        /// </summary>
        /// <param name="message">Message to handle.</param>
        public virtual void HandleIncomingMessage(NetIncomingMessage message)
        {
            IMessage imessage;

            try
            {
                CryptoProviderResolver.Resolve(message.SenderEndPoint)?.Decrypt(message);
                imessage = DeserializeMessage(message.ReadBytes(message.LengthBytes));
            }
            catch (Exception)
            {
                // If a message can't be deserialized / decrypted, it's fair to assume
                // it's either malicious or accidental, and so it should be ignored. We especially
                // don't want any malicious messages to raise exceptions that would crash the
                // message loop.
                return;
            }
            IRequestMessage request = imessage as IRequestMessage;

            if (request != null && InterfaceResolver.SenderEndPointIsExpectedByInterface(request))
            {
                // Modified such that interface method names that end in _ will provide the Client's IP
                object[] args2 = new object[request.Args?.Length + 1 ?? 1];
                request.Args?.CopyTo(args2, 0);
                args2[args2.Length - 1] = message.SenderEndPoint;
                request.Args            = args2;
            }
            OnReceive(imessage);
        }
Пример #3
0
        static void Main()
        {
            var resolver = new InterfaceResolver("TDIC_Configuration.txt");
            var sender   = resolver.Instantiate <IEmailSender>();

            sender.SendEmail("pippo", "pluto");
        }
Пример #4
0
        static void Main(string[] args)
        {
            var emaiSender = new InterfaceResolver("TDIC_Configuration.txt").Instantiate <IEmailSender>();

            emaiSender.SendEmail("*****@*****.**", "ciao pippo");
            Console.ReadLine();
        }
Пример #5
0
        public static void Main(string[] args)
        {
            var resolver = new InterfaceResolver("../../../Config.txt");
            var sender   = resolver.Instantiate <IEmailSender>();

            sender.SendEmail("sender", "message to send");
            Console.ReadLine();
        }
Пример #6
0
        public void UnregisteredConstructorThrowsRegistrationException()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IConstructorTestClass, ConstructorTestClassOnlyDefault>().WithConstructor(new Type[] {typeof (string)}, new object[] {""});

            // Act
            var resolve = c.Resolve<IConstructorTestClass>();

            // Assert
            Assert.Fail("An exception was not thrown when it should have been");
        }
Пример #7
0
        public void UsesDefaultConstructorWhenNotFirst()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IConstructorTestClass, ConstructorTestClassWithManyConstructors>();

            // Act
            var instance = resolver.Resolve<IConstructorTestClass>();

            // Assert
            Assert.AreEqual("default", instance.Value);
        }
Пример #8
0
        public void NonFirstConstructorWithMultipleArgumentsResolves()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IConstructorTestClass, ConstructorTestClassWithManyConstructors>()
                .WithConstructor(new[] {typeof (string), typeof (int)}, new object[] {"something", 3});

            // Act
            var instance = resolver.Resolve<IConstructorTestClass>();

            // Assert
            Assert.AreEqual("string,int", instance.Value);
        }
Пример #9
0
        public void ChainedResolverClassWithResolverConstructorResolvesNewInstance()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IChainedResolverClass, ChainedResolverClass>();
            IChainedResolverClass chainedResolverClass = new ChainedResolverClass(resolver);

            // Act
            IChainedResolverClass newResolver = chainedResolverClass.GetNewChainedResolverClass();

            // Assert
            Assert.IsNotNull(newResolver);
            Assert.AreNotSame(chainedResolverClass, newResolver);
        }
Пример #10
0
        public void FirstResolveWithNoDefaultResultsInFirstConstructor()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IConstructorTestClass, ConstructorTestClassWithNoDefault>()
                .WithConstructorValue("string1", "test")
                .WithConstructorValue("int1", 5);

            // Act
            var instance = resolver.Resolve<IConstructorTestClass>();

            // Assert
            Assert.AreEqual("string,int(first)", instance.Value);
        }
        /// <summary>
        /// Serializes message into byte array.
        /// </summary>
        /// <param name="msg">Message to serialize.</param>
        /// <returns>Serialized message.</returns>
        public byte[] Serialize(IMessage msg)
        {
            IRequestMessage request = msg as IRequestMessage;

            if (request != null && InterfaceResolver.SenderEndPointIsExpectedByInterface(request))
            {
                object[] args2 = new object[request.Args.Length - 1];
                for (int i = 0; i < args2.Length; i++)
                {
                    args2[i] = request.Args[i];
                }
                request.Args = args2;
            }
            return(_serializer.Serialize(msg));
        }
Пример #12
0
        public void GenerateBasicHotel_2x3x2()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IRoomAccess, RoomAccess>();

            var manager = new OccupancyManager_Wpf(resolver);

            // Act
            manager.GenerateBasicHotel(2, 3, 2);

            // Assert
            List<Room> rooms = manager.GetAllRooms();
            Assert.AreEqual(2*3*2, rooms.Count);
        }
Пример #13
0
        static void Main(string[] args)
        {
            var resolver = new InterfaceResolver("../../../TDIC_Configuration.txt");
            var sender   = resolver.Instantiate <IEmailSender>();

            if (sender != null)
            {
                sender.SendEmail("paperino", "foo");
            }
            else
            {
                Debug.WriteLine("Bad New Sender is NULL");
            }
            sender?.SendEmail("pippo", "di brutto");
            Console.ReadLine();
        }
Пример #14
0
        public void AnonymousInstanceResolvesToProvidedObject()
        {
            // Arrange
            var instance = new ConcreteTypeThree(28);
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeThree>().AsInstance(instance);
            instance.ChangeValue(55);

            // Act
            var resolve1 = c.Resolve<IRootType>();
            var resolve2 = c.Resolve<IRootType>();

            // Assert
            Assert.AreSame(resolve1, resolve2);
            Assert.AreSame(resolve1, instance);
            Assert.AreEqual(55, resolve1.GetFinalValue());
        }
Пример #15
0
        public void ChainedResolverClassWithResolverConstructorPropegatesResolverDownward()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IChainedResolverClass, ChainedResolverClass>();
            IChainedResolverClass chainedResolverClass = new ChainedResolverClass(resolver);

            // Act
            /*IChainedResolverClass newResolver =
                chainedResolverClass.GetNewChainedResolverClass().GetNewChainedResolverClass();*/
            IChainedResolverClass newResolver =
                chainedResolverClass.GetNewChainedResolverClass();
            newResolver = newResolver.GetNewChainedResolverClass();

            // Assert
            Assert.AreSame(resolver, newResolver.GetResolver());
            Assert.AreSame(chainedResolverClass.GetResolver(), newResolver.GetResolver());
        }
Пример #16
0
 static void Main(string[] args)
 {
     try
     {
         InterfaceResolver ir = new InterfaceResolver("MyConfig2.txt");
         IEmailSender      ES = ir.Instantiate <IEmailSender>();
         ES.SendEmail("ciao", "mattia");
         ES.SendEmail("hello", "john");
         Console.WriteLine("\n\n");
         IEmailSender ES2 = ir.Instantiate <IEmailSender>();
         ES2.SendEmail("ciao", "mattia");
         ES2.SendEmail("hello", "john");
     }
     catch (FileNotFoundException)
     {
         Console.WriteLine("pathname errato/i");
     }
 }
Пример #17
0
        public void CheckinTo10X10_first()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IRoomAccess, RoomAccess>();
            var roomAccess = resolver.Resolve<IRoomAccess>();

            roomAccess.GenerateBasicHotel(10, 10, 1);

            IReservingEngine service = new ReservingEngine(resolver);

            var newGuest = new Guest(fname: "New", lname: "Guest");

            // Act
            Room x = service.PerformAutoCheckin(newGuest);

            // Assert
            Assert.AreEqual("1,1,1", x.Location.ToString());
        }
Пример #18
0
 static void Main(string[] args)
 {
     try
     {
         var resolver = new InterfaceResolver("TDIC_Configuration.txt");
         var sender   = resolver.Instantiate <IEmailSender>();
         sender.SendEmail("*****@*****.**", "Questa e' un email di prova");
         Console.ReadLine();
     }
     catch (FileFormatException e)
     {
         Console.WriteLine("Errore file di configurazione: {0}", e.Message);
         Console.ReadLine();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Console.ReadLine();
     }
 }
Пример #19
0
        /// <summary>
        /// Serializes message into byte array.
        /// </summary>
        /// <param name="msg">Message to serialize.</param>
        /// <returns>Serialized message.</returns>
        public byte[] Serialize(IMessage msg)
        {
            MemoryStream primaryStream   = new MemoryStream();
            MemoryStream parameterStream = new MemoryStream();

            primaryStream.WriteByte(TypeToByte[msg.GetType()]);
            switch (msg.GetType().Name)
            {
            case nameof(ProtobufRequestMessage):
                ProtobufRequestMessage protobufRequest = (ProtobufRequestMessage)msg;
                if (protobufRequest.Args != null)
                {
                    bool dropIp = InterfaceResolver.SenderEndPointIsExpectedByInterface(protobufRequest);
                    for (int index = 0;
                         index < protobufRequest.Args.Length && (!dropIp || index < protobufRequest.Args.Length - 1);
                         index++)
                    {
                        object obj = protobufRequest.Args[index];
                        Serializer.SerializeWithLengthPrefix(parameterStream, obj, PrefixStyle.Fixed32);
                    }
                }
                protobufRequest.SerializableArgs = parameterStream.ToArray();
                Serializer.Serialize(primaryStream, msg);
                break;

            case nameof(ProtobufResponseMessage):
                ProtobufResponseMessage protobufResponse = (ProtobufResponseMessage)msg;
                Serializer.Serialize(parameterStream, protobufResponse.Value);
                protobufResponse.SerializableValue = parameterStream.ToArray();
                Serializer.Serialize(primaryStream, msg);
                break;

            default:
                Serializer.Serialize(primaryStream, msg);
                break;
            }
            return(primaryStream.ToArray());
        }
Пример #20
0
        public void ConstructChainedResolverClassWithResolver()
        {
            // Arrange
            var resolver = new InterfaceResolver();

            // Act
            var chainedResolverClass = new ChainedResolverClass(resolver);

            // Assert
            Assert.IsNotNull(chainedResolverClass);
        }
Пример #21
0
        public void PerformCheckinToFullHotel()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IRoomAccess, RoomAccess>();
            var roomAccess = resolver.Resolve<IRoomAccess>();

            roomAccess.GenerateBasicHotel(1, 2, 1);

            var guest1 = new Guest(fname: "Some", lname: "Body");
            string roomNumber1 = roomAccess.GetRoomNumbers()[0];
            roomAccess.AssignGuestToRoom(guest1, roomNumber1);

            var guest2 = new Guest(fname: "Noone", lname: "Special");
            string roomNumber2 = roomAccess.GetRoomNumbers()[1];
            roomAccess.AssignGuestToRoom(guest2, roomNumber2);

            IReservingEngine service = new ReservingEngine(resolver);

            var newGuest = new Guest(fname: "New", lname: "Guest");

            // Act
            Room x = service.PerformAutoCheckin(newGuest);

            // Assert
            Assert.Fail("Expected Exception was not thrown");
        }
Пример #22
0
        public void NamedSingletonResolvesToSameObject()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>("namedRegistration").AsSingleton();

            // Act
            var resolve1 = c.Resolve<IRootType>("namedRegistration");
            var resolve2 = c.Resolve<IRootType>("namedRegistration");

            // Assert
            Assert.AreSame(resolve1, resolve2);
        }
Пример #23
0
        public void WithConstructorValueBuildsWithNamedParameter()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeTwo>("named").WithConstructorValue("internalValue", 5);

            // Act
            int i = c.Resolve<IRootType>("named").GetFinalValue();

            // Assert
            Assert.AreEqual(5, i);
        }
Пример #24
0
        public void PerformCheckin()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            resolver.Register<IRoomAccess, RoomAccess>();
            var roomAccess = resolver.Resolve<IRoomAccess>();

            roomAccess.GenerateBasicHotel(8, 2, 4);

            var guest1 = new Guest(fname: "Some", lname: "Body");
            string roomNumber1 = roomAccess.GetRoomNumbers()[4];
            roomAccess.AssignGuestToRoom(guest1, roomNumber1);

            var guest2 = new Guest(fname: "Noone", lname: "Special");
            string roomNumber2 = roomAccess.GetRoomNumbers()[3];
            roomAccess.AssignGuestToRoom(guest2, roomNumber2);

            IReservingEngine service = new ReservingEngine(resolver);

            var newGuest = new Guest(fname: "New", lname: "Guest");

            // Act
            Room x = service.PerformAutoCheckin(newGuest);

            // Assert
            Assert.AreEqual("8,1,1", x.Location.ToString());
        }
Пример #25
0
        public void ChainedResolverClassWithResolverWithUnregisteredDependencyFails()
        {
            // Arrange
            var resolver = new InterfaceResolver();
            IChainedResolverClass chainedResolverClass = new ChainedResolverClass(resolver);

            // Act
            IChainedResolverClass newResolver = chainedResolverClass.GetNewChainedResolverClass();

            // Assert
            Assert.Fail("Exception expected");
        }
Пример #26
0
        public void ResolveRegisteredDelegateWithNewFunctionReturnsDistinctObjects()
        {
            // Arrange
            var c = new InterfaceResolver();
            const int i = 84;
            c.Register<IRootType, ConcreteTypeThree>().AsDelegate(() => new ConcreteTypeThree(i));

            // Act
            var resolve = c.Resolve<IRootType>();
            var resolve2 = c.Resolve<IRootType>();

            // Assert
            Assert.AreNotSame(resolve, resolve2);
            Assert.AreEqual(i, resolve.GetFinalValue());
            Assert.AreEqual(i, resolve2.GetFinalValue());
        }
Пример #27
0
        public void ResolveRegisteredDelegateWithSpecificObjectReturnsSameObject()
        {
            // Arrange
            var c = new InterfaceResolver();
            const int i = 84;
            var d = new ConcreteTypeThree(i);
            c.Register<IRootType, ConcreteTypeThree>().AsDelegate(() => d);

            // Act
            var resolve = c.Resolve<IRootType>();
            var resolve2 = c.Resolve<IRootType>();

            // Assert
            Assert.AreSame(resolve, resolve2);
            Assert.AreEqual(i, resolve.GetFinalValue());
            Assert.AreEqual(i, resolve2.GetFinalValue());
        }
Пример #28
0
        public void ResolveAnonymousSubDependency()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>();
            c.Register<IDisplay, NodeDisplay>();

            // Act
            var m = c.Resolve<IDisplay>();

            // Assert
            Assert.AreEqual("$0.00", m.Format("C2"));
        }
Пример #29
0
        public void ResolveRegisteredDelegate()
        {
            // Arrange
            var c = new InterfaceResolver();
            const int i = 84;
            c.Register<IRootType, ConcreteTypeThree>().AsDelegate(() => new ConcreteTypeThree(i));

            // Act
            var resolve = c.Resolve<IRootType>();

            // Assert
            Assert.AreEqual(typeof (ConcreteTypeThree), resolve.GetType());
            Assert.AreEqual(i, resolve.GetFinalValue());
        }
Пример #30
0
        public void ResolveAnonymousRegistrationWithNoDefinedConstructors()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>();

            // Act
            var m = c.Resolve<IRootType>();

            // Assert
            Assert.AreEqual(0, m.GetFinalValue());
        }
Пример #31
0
        public void RegisteringClassResolvesCorrectly()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>();

            // Act
            var resolve = c.Resolve<IRootType>();

            // Assert
            Assert.AreEqual(typeof (ConcreteTypeWithNoDefinedConstructors), resolve.GetType());
        }
Пример #32
0
        public void AnonymousNonSingletonDoNotResolveToSameObject()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>().AsSingleton();

            // Act
            var resolve1 = c.Resolve<IRootType>();
            var resolve2 = c.Resolve<IRootType>();

            // Assert
            Assert.AreSame(resolve1, resolve2);
        }
Пример #33
0
 public ChainedResolverClass(InterfaceResolver resolver)
 {
     Resolver = resolver;
 }
Пример #34
0
        public void ResolveSameNamedRegistrationOnDifferentInterfacesWithNoDefinedConstructors()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>("named");
            c.Register<IRootType2, ConcreteType2WithNoDefinedConstructors>("named");

            // Act
            var m = c.Resolve<IRootType>("named");
            var n = c.Resolve<IRootType2>("named");

            // Assert
            Assert.AreEqual(0, m.GetFinalValue());
        }
Пример #35
0
 public ChainedResolverClass()
 {
     Resolver = new InterfaceResolver();
     Resolver.Register<IChainedResolverClass, ChainedResolverClass>();
 }
Пример #36
0
        public void UnregisteredNamedClassWithoutRegisteredAnonThrowsException()
        {
            // Arrange
            var c = new InterfaceResolver();

            // Act
            var resolve = c.Resolve<IRootType>("unregisteredName");

            // Assert
            Assert.Fail("An exception was not thrown when it should have been");
        }
Пример #37
0
        public void InstantiateReservingServiceClassWithResolver()
        {
            // Arrange
            var resolver = new InterfaceResolver();

            // Act
            IReservingEngine service = new ReservingEngine(resolver);

            // Assert
            Assert.IsNotNull(service);
        }
Пример #38
0
        public void UnregisteredNamedClassWithRegisteredAnonThrowsException()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>();

            // Act
            var resolve = c.Resolve<IRootType>("unregisteredName");

            // Assert
            Assert.Fail("An exception was not thrown when it should have been");
        }
Пример #39
0
        public void RegisterAsDelegate()
        {
            // Arrange
            var c = new InterfaceResolver();

            // Act
            InterfaceResolver.Registration registration = c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>().AsDelegate(() => null);

            // Assert
            Assert.AreEqual(typeof (InterfaceResolver.Registration), registration.GetType());
        }
Пример #40
0
        public void ClearRegistrationClearsPreviouslyRegisteredClass()
        {
            // Arrange
            var c = new InterfaceResolver();
            c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>();
            c.ClearRegistrations();

            // Act
            var resolve = c.Resolve<IRootType>();

            // Assert
            Assert.Fail();
        }