static void Main(string[] args) { var resolver = new InterfaceResolver("TinyDICConfig.txt"); var sender = resolver.Instantiate <IEmailSender>(); sender.SendEmail("pippo", "pluto"); }
/// <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); }
static void Main() { var resolver = new InterfaceResolver("TDIC_Configuration.txt"); var sender = resolver.Instantiate <IEmailSender>(); sender.SendEmail("pippo", "pluto"); }
static void Main(string[] args) { var emaiSender = new InterfaceResolver("TDIC_Configuration.txt").Instantiate <IEmailSender>(); emaiSender.SendEmail("*****@*****.**", "ciao pippo"); Console.ReadLine(); }
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(); }
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"); }
public void UsesDefaultConstructorWhenNotFirst() { // Arrange var resolver = new InterfaceResolver(); resolver.Register<IConstructorTestClass, ConstructorTestClassWithManyConstructors>(); // Act var instance = resolver.Resolve<IConstructorTestClass>(); // Assert Assert.AreEqual("default", instance.Value); }
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); }
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); }
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)); }
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); }
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(); }
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()); }
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()); }
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"); } }
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()); }
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(); } }
/// <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()); }
public void ConstructChainedResolverClassWithResolver() { // Arrange var resolver = new InterfaceResolver(); // Act var chainedResolverClass = new ChainedResolverClass(resolver); // Assert Assert.IsNotNull(chainedResolverClass); }
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"); }
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); }
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); }
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()); }
public void ChainedResolverClassWithResolverWithUnregisteredDependencyFails() { // Arrange var resolver = new InterfaceResolver(); IChainedResolverClass chainedResolverClass = new ChainedResolverClass(resolver); // Act IChainedResolverClass newResolver = chainedResolverClass.GetNewChainedResolverClass(); // Assert Assert.Fail("Exception expected"); }
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()); }
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()); }
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")); }
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()); }
public void ResolveAnonymousRegistrationWithNoDefinedConstructors() { // Arrange var c = new InterfaceResolver(); c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>(); // Act var m = c.Resolve<IRootType>(); // Assert Assert.AreEqual(0, m.GetFinalValue()); }
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()); }
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); }
public ChainedResolverClass(InterfaceResolver resolver) { Resolver = resolver; }
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()); }
public ChainedResolverClass() { Resolver = new InterfaceResolver(); Resolver.Register<IChainedResolverClass, ChainedResolverClass>(); }
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"); }
public void InstantiateReservingServiceClassWithResolver() { // Arrange var resolver = new InterfaceResolver(); // Act IReservingEngine service = new ReservingEngine(resolver); // Assert Assert.IsNotNull(service); }
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"); }
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()); }
public void ClearRegistrationClearsPreviouslyRegisteredClass() { // Arrange var c = new InterfaceResolver(); c.Register<IRootType, ConcreteTypeWithNoDefinedConstructors>(); c.ClearRegistrations(); // Act var resolve = c.Resolve<IRootType>(); // Assert Assert.Fail(); }