Zurück tutorials.de > Programming > .NET > .NET Application und Service Design

 
 
Hallo und herzlich willkommen! Tutorials.de ist eine Hilfe-Community mit dem Motto User helfen Usern. Als Gast verfügst Du über Schreibrechte in unseren Foren und Blogs. Du kannst dich aber gerne auch kostenlos registrieren und Teil unserer Gemeinschaft werden! Viel Spaß & Erfolg bei der Vermehrung deines Wissens :-)

Themen: 242.975 | Beiträge: 1.352.293 | Mitglieder: 169.418 (Stand 28.01.10) | Fragen zur Nutzung von Tutorials.de? Nutzungsregeln | Kontaktformular | Impressum
 
 
tutorials.de Buch-Verschenkaktion

  AntwortAntworten (über Gastzugang)    
  AntwortAntworten (über Gastzugang)    
 
LinkBack (1) Themen-Optionen Ansicht
Alt 11.07.06, 20:19   Link/s auf einer externen Seite. Klicken um diese/n anzuzeigen. #1 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.155
Renommee-Modifikator: 60
Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft

Dynamic Proxy unter .Net

Hallo!

Hier mal ein kleines Beispiel wie man unter .Net Dynamic Proxies in C# erzeugen kann:

csharp Code:
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
using System;
using System.Collections.Generic;
using System.Text;
using RealProxy = System.Runtime.Remoting.Proxies.RealProxy;
using IMessage = System.Runtime.Remoting.Messaging.IMessage;
using ReturnMessage = System.Runtime.Remoting.Messaging.ReturnMessage;
using IMethodCallMessage = System.Runtime.Remoting.Messaging.IMethodCallMessage;
using TargetInvocationException = System.Reflection.TargetInvocationException;
 
namespace De.Tutorials.Training
{
    class DynamicProxyExample
    {
 
 
        public static void Main(string[] args)
        {
            IBusinessService businessService = new SimpleProxy(typeof(IBusinessService), new BusinessServiceImpl()).GetTransparentProxy() as IBusinessService;
            Console.WriteLine(businessService.BusinessOperation("Proxy"));
        }
    }
 
    class SimpleProxy : RealProxy
    {
 
        object target;
 
        public SimpleProxy(Type type, object target)
            : base(type)
        {
            this.target = target;
        }
 
        public override IMessage Invoke(IMessage message)
        {
            IMethodCallMessage methodCallMessage = (IMethodCallMessage)message;
            try
            {
                Console.WriteLine("Before: " + methodCallMessage.MethodName);
 
                object result = target.GetType().GetMethod(methodCallMessage.MethodName, (Type[])methodCallMessage.MethodSignature).Invoke(target, methodCallMessage.InArgs);
 
                Console.WriteLine("After: " + methodCallMessage.MethodName);
 
                return new ReturnMessage(result, null, 0, methodCallMessage.LogicalCallContext, methodCallMessage);
            }
            catch (TargetInvocationException targetInvocationException)
            {
                return new ReturnMessage(targetInvocationException, methodCallMessage);
            }
 
        }
    }
 
    interface IBusinessService
    {
        string BusinessOperation(string args);
    }
 
    class BusinessServiceImpl
        : IBusinessService
    {
        public string BusinessOperation(string args)
        {
            Console.WriteLine("Hallo " + args);
 
            return args;
        }
    }
}

Ausgabe:
Code:
Before: BusinessOperation
Hallo Proxy
After: BusinessOperation
Proxy
Gruß Tom
__________________
Java rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
  Thomas Darimont ist gerade online  
 
Alt 09.07.08, 15:28   #2 (permalink)
Mitglied Brokat
 
Registriert seit: May 2007
Ort: Riedstadt (Hessen)
Beiträge: 350
Renommee-Modifikator: 7
limago sorgt für eine eindrucksvolle Atmosphäre

AW: Dynamic Proxy unter .Net

Hallo Thomas,

unter Java kann ich die Proxies schachteln. Wenn ich das hier mache, fliegt eine Exception(Object does not match target type). Gibt es dafür eine Lösung?

Grüße

Limago
__________________
I didn't write this; a very complex macro did.
  limago ist offline  
 
Alt 09.07.08, 15:55   #3 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.155
Renommee-Modifikator: 60
Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft Thomas Darimont hat eine strahlende Zukunft

AW: Dynamic Proxy unter .Net

Hallo,

das kann man auch unter .Net ohne Probleme Hier ein Beispiel mit Transparent Proxies:
csharp Code:
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
using System;
using System.Collections.Generic;
using System.Diagnostics;
 
using IMethodCallMessage = System.Runtime.Remoting.Messaging.IMethodCallMessage;
using RealProxy = System.Runtime.Remoting.Proxies.RealProxy;
using ReturnMessage = System.Runtime.Remoting.Messaging.ReturnMessage;
 
namespace De.Tutorials.Training
{
    public class DynamicProxyExample
    {
 
        public static void Main(string[] args)
        {
            IDataModel<string> simpleDataModel = (IDataModel<string>)new DataModelProxy(typeof(IDataModel<string>)).GetTransparentProxy();
            simpleDataModel.Data = "Bubu";
            Console.WriteLine(simpleDataModel is IDataModel<string>);
            Console.WriteLine(simpleDataModel.Data);
            Console.WriteLine(simpleDataModel.GetType().FullName);
 
 
            IDataModel<IDataModel<string>> wrappedDataModel = (IDataModel<IDataModel<string>>)new DataModelProxy(typeof(IDataModel<IDataModel<string>>)).GetTransparentProxy();
            wrappedDataModel.Data = simpleDataModel;
 
            Console.WriteLine("####");
            Console.WriteLine(wrappedDataModel is IDataModel<IDataModel<string>>);
            Console.WriteLine(wrappedDataModel.Data == null);
            Console.WriteLine(wrappedDataModel.Data.Data);
            Console.WriteLine(wrappedDataModel.GetType().FullName);
 
        }
 
        public class DataModelProxy : RealProxy
        {
            IDictionary<string, object> propertyDictionary;
 
            public DataModelProxy(Type type)
                : base(type)
            {
                this.propertyDictionary = new Dictionary<string, object>();
            }
 
            public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage aMessage)
            {
                IMethodCallMessage methodInvocationMessage = (IMethodCallMessage)aMessage;
 
                if (methodInvocationMessage.MethodName.Equals("GetType"))
                {
                    return new ReturnMessage(this.GetType(), null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage);
                }
 
                if (methodInvocationMessage.MethodName.StartsWith("set_"))
                {
                    string propertyName = GetPropertyNameFor(methodInvocationMessage.MethodName);
                    this.propertyDictionary[propertyName] = methodInvocationMessage.Args[0];
                    return new ReturnMessage(methodInvocationMessage.Args[0], null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage);
                }
                else if (methodInvocationMessage.MethodName.StartsWith("get_"))
                {
                    string propertyName = GetPropertyNameFor(methodInvocationMessage.MethodName);
                    if (this.propertyDictionary.ContainsKey(propertyName))
                    {
                        return new ReturnMessage(this.propertyDictionary[propertyName], null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage);
                    }
                    else
                    {
                        return new ReturnMessage(null, null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage);
                    }
                }
                else
                {
                    return new ReturnMessage(null, null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage);
                }
            }
 
            private string GetPropertyNameFor(string propertyName)
            {
                return propertyName.Substring(4);
            }
        }
 
        public interface IDataModel<TData>
        {
            TData Data
            {
                get;
                set;
            }
        }
    }
}

Witzig, dass man nach und nach die Java Jünger im .Net Teich plantschen sieht...
Aber wie gesagt, es ist wichtig beides zu kennen!

Es gibt natürlich zahlreiche andere Möglichkeiten Dynamic Proxies zu erzeugen. Transparent Proxies sind ziemlich schwer zu debuggen, da man oft die Meldung bekommt, dass das Objekt nicht ausgewertet werden kann, da es sich um einen Remote Proxy handelt... hab die genaue Meldung nicht mehr im Kopf.
Eine Alternative zu den ätzenden transpoarent Remoting Proxies sind beispielsweise Castles Dynamic Proxies, Springs Dynamic Proxies oder eigene Proxies mit System.Reflection.Emit (machen wir in der Firma auch selbst... und die kann man sogar ordentlich Debuggen )

Schau mal hier:
http://www.tutorials.de/forum/net-ap...c-proxies.html
http://www.tutorials.de/forum/net-ap...pring-net.html

Gruß Tom
__________________
Java rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
  Thomas Darimont ist gerade online  
 
Alt 10.07.08, 17:10   #4 (permalink)
Mitglied Brokat
 
Registriert seit: May 2007
Ort: Riedstadt (Hessen)
Beiträge: 350
Renommee-Modifikator: 7
limago sorgt für eine eindrucksvolle Atmosphäre

AW: Dynamic Proxy unter .Net

Zitat:
Zitat von Thomas Darimont Beitrag anzeigen
Witzig, dass man nach und nach die Java Jünger im .Net Teich plantschen sieht...
Aber wie gesagt, es ist wichtig beides zu kennen!
Ja, ja so ist es wohl

Schön Dich auch hier hier zu finden und vielen Dank für Deine prompte Antwort. Die Spring Proxies hatte ich auch schon gefunden, aber ich brauche eine Lösung mit "Bordmitteln".

Die TransparentProxies sind in der Tat nicht so schön, aber für mein Problem völlig ausreichend.

Viele Grüße

Limago
__________________
I didn't write this; a very complex macro did.
  limago ist offline  
 
 
 
Lesezeichen:


Themen-Optionen
Ansicht
LinkBacks (?)
 
Erstellt von Für Typ Datum
Proxy-Pattern: Beschreibung und Beispiele - Norbert Eder - Living .NET - Kommentare Dieses Thema Refback 07.04.07 13:23

Ähnliche Themen
 
Thema Autor Forum Antworten Letzter Beitrag
Dynamic Proxy in Python Thomas Darimont CGI, Perl, Python, Ruby 0 14.05.07 13:16
Proxy unter Debian Sascha Schirra Netzwerke 5 30.09.05 19:20
Proxy in vb.net Tortilla .NET Archiv 1 11.08.04 14:01
Proxy unter Suse Linux 7.2 phantom Linux & Unix 0 03.03.02 16:37
Proxy-Server-Einrichtung unter SuSe Linux 7.3 Arne Buchwald Linux & Unix 5 30.01.02 22:37
» Tools
 
tutorials.de-Tools tutorial.de-Suchfeld tutorial.de-Widget tutorial.de-RSS-Feed tutorial.de-Banner
» Neue Links
 
Hits: 101
»
JHT's Planetary...
(Cinema 4D-Objekte)
Hits: 223
»
Tageslicht ohne GI
(Cinema 4D-Tutorials)
Hits: 114
»
Puzzle
(Cinema 4D-Tutorials)
Hits: 83
»
Lacreme
(Cinema 4D-Tutorials)
Hits: 163
»
Liquid Light
(Cinema 4D-Tutorials)
» Aktuelle Umfrage
 
Bist du mit der Geschwindigkeit der Tutorials.de-Website zufrieden?
Ja, es putzt mir glatt den Staub vom Bildschirm! - 75,00%
60 Stimmen
Nein, ich denke da muss noch nachgebessert werden... - 25,00%
20 Stimmen
Stimmen gesamt: 80
Du darfst bei dieser Umfrage nicht abstimmen.

 

Alle Zeitangaben in WEZ +1. Es ist jetzt 09:04 Uhr.


Powered by vBulletin® Version 3.8.4 (Deutsch) & vBadvanced CMPS v.3.2.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.0 ©2009, Crawlability, Inc.
Alle Rechte vorbehalten ©2000 - 2010 tutorials.de
Design by Mark, CSS by Maik & Sven Mintel
Seite generiert in 0,43403 Sekunden mit 26 queries