コード例 #1
0
        /*
         *
         *
         * A intenção do padrão Proxy é fornecer um substituto ou espaço reservado para outro objeto para controlar o acesso a ele. Um proxy, em sua forma mais geral,
         * é uma classe que funciona como uma interface para outra classe.
         *
         * participantes:
         * Proxy - mantém uma referencia que permite ao proxy acessar o objeto real; fornece uma interface idêntica ao de subject, por esse motivo ele pode substituir o objeto
         * real(Real Subject); controla o acesso ao objeto real, podendo ser responsvel pela criação e destruição;
         * Subject - define uma interface comum para RealSubject e Proxy;
         * RealSubject - define o objeto real que o proxu representa;
         *
         */

        public void Proxy()
        {
            ICalc calc = new CalcProxy();
            var   r    = calc.Somar(3, 5);

            Response.Write(r);
        }
コード例 #2
0
 public void SetUp()
 {
     calculator          = new Calculator();
     calcProxy           = new CalcProxy(calculator);
     calcProxyWithLimits =
         new CalcProxy(new Validator(-10, 10), calculator);
 }
コード例 #3
0
        public void OtherParameterInEndorsingSupport()
        {
            SymmetricSecurityBindingElement be =
                new SymmetricSecurityBindingElement();

            be.ProtectionTokenParameters =
                new X509SecurityTokenParameters();
            be.EndpointSupportingTokenParameters.Endorsing.Add(
                new MyEndorsingTokenParameters());
            Binding         b      = new CustomBinding(be, new HttpTransportBindingElement());
            EndpointAddress ea     = new EndpointAddress(new Uri("http://localhost:" + NetworkHelpers.FindFreePort()), new X509CertificateEndpointIdentity(cert));
            CalcProxy       client = new CalcProxy(b, ea);

            client.Endpoint.Behaviors.RemoveAll <ClientCredentials> ();
            client.Endpoint.Behaviors.Add(new MyClientCredentials());
            client.Sum(1, 2);
        }
コード例 #4
0
        void ProcessClient()
        {
            SymmetricSecurityBindingElement svcsbe =
                new SymmetricSecurityBindingElement();

            svcsbe.ProtectionTokenParameters =
                new X509SecurityTokenParameters(X509KeyIdentifierClauseType.Thumbprint, SecurityTokenInclusionMode.Never);

            BindingElement cintercept = new InterceptorBindingElement(null);
            CustomBinding  b_req      = new CustomBinding(svcsbe,
                                                          cintercept,
                                                          new HttpTransportBindingElement());

            b_req.ReceiveTimeout = b_req.SendTimeout = TimeSpan.FromSeconds(5);
            EndpointAddress remaddr = new EndpointAddress(
                new Uri("http://localhost:" + NetworkHelpers.FindFreePort()),
                new X509CertificateEndpointIdentity(cert));
            CalcProxy proxy = new CalcProxy(b_req, remaddr);

            proxy.ClientCredentials.ClientCertificate.Certificate = cert2;

            proxy.Sum(1, 2);
            proxy.Close();
        }
コード例 #5
0
        /// <summary>
        /// Client
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region Adapter

            Console.WriteLine("Adapter initialize ");

            Target target  = new Target();
            Target target2 = new Adaptee();

            Target target3 = new Adapter.Adapter();
            target.Request();
            Console.ReadKey();

            Console.WriteLine("Adapter Finalize " + Environment.NewLine);

            #endregion

            #region Bridge

            Console.WriteLine("Bridge initialize ");

            //Step 1
            ExportationBridge exp = new ExportationDOC();
            exp.Export();

            //Apply Brige Pattern
            Exportation exportacao = new ExportationEx();

            //Inject object
            exportacao.Implementor = new ExportationPDF();
            exportacao.Export();

            Console.WriteLine("Bridge finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Composite

            Console.WriteLine("Composite initialize");

            var form = new Form("Cadastro Clientes");
            form.Add(new Button("Incluir"));
            form.Add(new Button("Consultar"));
            form.Add(new TextBox("Nome"));
            form.Add(new TextBox("Fone"));
            form.Display();

            Console.WriteLine("Composite finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Decorator

            Console.WriteLine("Decorator initialize");

            //Step 1
            var dec = new DataSet();
            dec.Write();

            //Apply Decorator
            DataSet c = new DataSet();
            DataSetConcreteDecorator d = new DataSetConcreteDecorator();

            //Setando objeto a ser decorado
            d.setDatasetbase(c);
            d.Write();
            d.WriteXML();

            Console.WriteLine("Decorator finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Facade

            Console.WriteLine("Facade initialize");

            //Step 1
            var email = new Mail(new SMTPSettings());
            var msg   = new MailMessage(new MailFormatTXT());
            msg.Message = "Hello world";
            email.Send(msg);
            Console.ReadKey();

            //Apply Facade pattern
            var emailFachada = new Email();
            emailFachada.Send("Hello world");

            Console.WriteLine("Facade finalize");
            Console.ReadKey();

            #endregion

            #region Flyweight

            Console.WriteLine("Flyweight initialize");

            int ext = 10;

            FlyweightFactory  factory = new FlyweightFactory();
            FlyweightAbstract f1      = factory.GetFlyweight("A");
            f1.Operation(ext++);
            FlyweightAbstract f2 = factory.GetFlyweight("B");
            f2.Operation(ext++);
            FlyweightAbstract f3 = factory.GetFlyweight("C");
            f3.Operation(ext++);
            FlyweightAbstract f4 = new UnsharedConcreteFlyweight();
            f4.Operation(ext++);

            Console.WriteLine("Flyweight finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Proxy

            Console.WriteLine("Proxy initialize");

            //Step 1
            var calc = new Calc();
            var r    = calc.Somar(3, 5);
            Console.WriteLine(r.ToString());
            Console.ReadLine();

            //Aplly Proxy Pattern
            var calcProxy = new CalcProxy();
            var r2        = calcProxy.Somar(3, 5);
            Console.WriteLine(r2.ToString());

            Console.WriteLine("Proxy finalize" + Environment.NewLine);
            Console.ReadLine();

            #endregion
        }
コード例 #6
0
		public void ClientInitiatorHasNoKeysCore (bool deriveKeys, MessageProtectionOrder order)
		{
			AsymmetricSecurityBindingElement sbe =
				new AsymmetricSecurityBindingElement ();
			sbe.InitiatorTokenParameters =
				new UserNameSecurityTokenParameters ();
			sbe.RecipientTokenParameters =
				new X509SecurityTokenParameters ();
			sbe.SetKeyDerivation (deriveKeys);
			sbe.MessageProtectionOrder = order;
			TransportBindingElement tbe = new HandlerTransportBindingElement (delegate (Message input) {
				// funky, but .NET does not raise an error
				// until it writes the message to somewhere.
				// That is, it won't raise an error if this
				// HandlerTransportBindingElement does not
				// write the input message to somewhere.
				// It is an obvious bug.
				input.WriteMessage (XmlWriter.Create (TextWriter.Null));
				throw new Exception ();
			});
			CustomBinding binding = new CustomBinding (sbe, tbe);
			EndpointAddress address = new EndpointAddress (
				new Uri ("stream:dummy"),
				new X509CertificateEndpointIdentity (cert2));
			CalcProxy proxy = new CalcProxy (binding, address);
			proxy.ClientCredentials.UserName.UserName = "******";
			proxy.Open ();
			// Until here the wrong parameters are not checked.
			proxy.Sum (1, 2);
		}
コード例 #7
0
		public void VerifyX509MessageSecurityAtService ()
		{
			AsymmetricSecurityBindingElement clisbe =
				new AsymmetricSecurityBindingElement ();
			clisbe.InitiatorTokenParameters =
				new X509SecurityTokenParameters ();
			clisbe.RecipientTokenParameters =
				new X509SecurityTokenParameters ();

			AsymmetricSecurityBindingElement svcsbe =
				new AsymmetricSecurityBindingElement ();
			svcsbe.InitiatorTokenParameters =
				new X509SecurityTokenParameters ();
			svcsbe.RecipientTokenParameters =
				new X509SecurityTokenParameters ();

			CustomBinding b_req = new CustomBinding (clisbe,
				new HttpTransportBindingElement ());

			b_req.ReceiveTimeout = b_req.SendTimeout = TimeSpan.FromSeconds (10);

			CustomBinding b_res = new CustomBinding (svcsbe, new HttpTransportBindingElement ());
			b_res.ReceiveTimeout = b_res.SendTimeout = TimeSpan.FromSeconds (10);

			EndpointAddress remaddr = new EndpointAddress (
				new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()),
				new X509CertificateEndpointIdentity (cert2));
			CalcProxy proxy = null;
			ServiceHost host = new ServiceHost (typeof (CalcService));
			host.AddServiceEndpoint (typeof (ICalc), b_res, "http://localhost:" + NetworkHelpers.FindFreePort ());

			ServiceCredentials cred = new ServiceCredentials ();
			cred.ServiceCertificate.Certificate = cert;
			host.Description.Behaviors.Add (cred);
			try {
				host.Open ();

				proxy = new CalcProxy (b_req, remaddr);
				proxy.ClientCredentials.ClientCertificate.Certificate = cert;

				// FIXME: on WinFX, when this Begin method
				// is invoked before the listener setup, it
				// somehow works, while ours doesn't.
				//IAsyncResult result = proxy.BeginSum (1, 2, null, null);
				//Assert.AreEqual (3, proxy.EndSum (result));
				Assert.AreEqual (3, proxy.Sum (1, 2));
			} finally {
				if (host.State == CommunicationState.Opened)
					host.Close ();
			}
		}
コード例 #8
0
		public void OtherParameterInEndorsingSupport ()
		{
			SymmetricSecurityBindingElement be =
				new SymmetricSecurityBindingElement ();
			be.ProtectionTokenParameters =
				new X509SecurityTokenParameters ();
			be.EndpointSupportingTokenParameters.Endorsing.Add (
				new MyEndorsingTokenParameters ());
			Binding b = new CustomBinding (be, new HttpTransportBindingElement ());
			EndpointAddress ea = new EndpointAddress (new Uri ("http://localhost:37564"), new X509CertificateEndpointIdentity (cert));
			CalcProxy client = new CalcProxy (b, ea);
			client.Endpoint.Behaviors.RemoveAll<ClientCredentials> ();
			client.Endpoint.Behaviors.Add (new MyClientCredentials ());
			client.Sum (1, 2);
		}
コード例 #9
0
		public void NonEndorsibleParameterInEndorsingSupport ()
		{
			SymmetricSecurityBindingElement be =
				new SymmetricSecurityBindingElement ();
			be.ProtectionTokenParameters =
				new X509SecurityTokenParameters ();
			be.EndpointSupportingTokenParameters.Endorsing.Add (
				new UserNameSecurityTokenParameters ());
			Binding b = new CustomBinding (be, new HttpTransportBindingElement ());
			X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
			EndpointAddress ea = new EndpointAddress (new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()), new X509CertificateEndpointIdentity (cert));
			CalcProxy client = new CalcProxy (b, ea);
			client.ClientCredentials.UserName.UserName = "******";
			client.Sum (1, 2);
		}