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

Jubiläums-Countdown 23.02 23.03 23.04 23.05 23.06 23.07 23.08 23.09


4 kostenlose Bücher bei unserer Buch-Verschenkaktion 03/2010
  AntwortAntworten (über Gastzugang)    
  AntwortAntworten (über Gastzugang)    
 
Themen-Optionen Ansicht
Alt 11.11.06, 14:48   #1 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.164
Renommee-Modifikator: 61
Thomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende Zukunft

Getypte Datenmodelle nur mit Interfaces und Dynamic Proxies

Hallo!

Hier mal ein Beispiel wie man mit Hilfe von DynamicProxies ganz einfach Interface-basierte getypte Datenmodelle zur Laufzeit erzeugen kann. (In dem Beispiel hält man das Datenmodell im Hintergrund in einem allgemeinen IDictionary<string,object>, mit dem man durch das Datenmodell-Interface, welches vom DynamicProxy umgesetzt wird, Typsicher zugreifen kann).
Ich verwende hierzu das DynamicProxy Framework des Castle-Projects ( http://www.castleproject.org/dynamicproxy/). Es wäre jedoch auch möglich mit standard .Net mitteln (Reflection.Emit) eigene DynamicProxy Implementierungen zu erzeugen oder man "mißbraucht" den Remtoing-RealProxy:
http://www.tutorials.de/forum/net-ap...ighlight=Proxy



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.  
  93.  
  94.  
using System;
using System.Collections.Generic;
using System.Text;
using ProxyGenerator = Castle.DynamicProxy.ProxyGenerator;
using IInterceptor = Castle.DynamicProxy.IInterceptor;
 
namespace De.Tutorials.Training
{
    public class DataModelProxyFactoryExample
    {
        public static void Main(string[] args)
        {
            IDataModel dataModel = DataModelProxyFactory.CreateDataModelProxyFor<IDataModel>();
            dataModel.Data = "Hallo";
            dataModel.Value = 10;
            Console.WriteLine(dataModel.Data);
            Console.WriteLine(dataModel.Value);
        }
    }
 
/// <summary>
/// A simple DataModel Interface
/// </summary>
    public interface IDataModel
    {
        String Data
        {
            get;
            set;
        }
 
        int Value
        {
            get;
            set;
        }
    }
 
    /// <summary>
    /// A Generic DataModel Proxy Factory
    /// </summary>
    public class DataModelProxyFactory
    {
        public static TargetType CreateDataModelProxyFor<TargetType>()
        {
            ProxyGenerator proxyGenerator = new ProxyGenerator();
            return (TargetType)proxyGenerator.CreateProxy(typeof(TargetType), new DataModelWrapper(typeof(TargetType)), new object());
        }
    }
 
    /// <summary>
    /// A simple DataModel wrapper
    /// </summary>
    public class DataModelWrapper : IInterceptor
    {
 
        private IDictionary<string, object> dictionary;
 
        private Type wrappedType;
 
        public DataModelWrapper(Type wrappedType)
        {
            this.wrappedType = wrappedType;
            this.dictionary = new Dictionary<string, object>();
        }
 
        #region IInterceptor Members
 
        public object Intercept(Castle.DynamicProxy.IInvocation invocation, params object[] args)
        {
            if (this.wrappedType.Equals(invocation.Method.DeclaringType))
            {
                if (invocation.Method.Name.StartsWith("get_"))
                {
                    return dictionary[GetPropertyNameFor(invocation.Method.Name)];
 
                }
                else if (invocation.Method.Name.StartsWith("set_"))
                {
                    dictionary[GetPropertyNameFor(invocation.Method.Name)] = args[0];
                    return args[0];
                }
            }
            return null;
        }
 
        private string GetPropertyNameFor(string methodName)
        {
            return methodName.Substring(4);
        }
 
        #endregion
    }
}

Btw. in Java schaut das ganze dann so aus:
http://www.tutorials.de/forum/java/2...hlight=Getyped

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 21.12.06, 14:31   #2 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.164
Renommee-Modifikator: 61
Thomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende Zukunft

AW: Getypte Datenmodelle nur mit Interfaces und Dynamic Proxies

Hallo!

kleiner Nachtrag, mit Generics klappts natürlich auch:
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.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
using System;
using System.Collections.Generic;
using System.Text;
using ProxyGenerator = Castle.DynamicProxy.ProxyGenerator;
using IInterceptor = Castle.DynamicProxy.IInterceptor;
 
namespace De.Tutorials.Training
{
    public class DataModelProxyFactoryExample
    {
        public static void Main(string[] args)
        {
            IDataModel dataModel = DataModelProxyFactory.CreateDataModelProxyFor<IDataModel>();
            dataModel.Data = "Hallo";
            dataModel.Value = 10;
            Console.WriteLine(dataModel.Data);
            Console.WriteLine(dataModel.Value);
 
 
            IGenericDataModel<string, IDataModel> genericDataModel = DataModelProxyFactory.CreateDataModelProxyFor<IGenericDataModel<string, IDataModel>>();
            genericDataModel.Data = "Bubu";
            genericDataModel.Value = dataModel;
            Console.WriteLine(genericDataModel.Data);
            Console.WriteLine(genericDataModel.Value.Data + " " + genericDataModel.Value.Value);
        }
    }
 
    /// <summary>
    /// A simple DataModel Interface
    /// </summary>
    public interface IDataModel
    {
        String Data
        {
            get;
            set;
        }
 
        int Value
        {
            get;
            set;
        }
    }
 
    /// <summary>
    /// A simple GenericDataModel Interface
    /// </summary>
    public interface IGenericDataModel<TData,TValue>
    {
        TData Data
        {
            get;
            set;
        }
 
        TValue Value
        {
            get;
            set;
        }
    }
 
    /// <summary>
    /// A Generic DataModel Proxy Factory
    /// </summary>
    public class DataModelProxyFactory
    {
        public static TargetType CreateDataModelProxyFor<TargetType>()
        {
            ProxyGenerator proxyGenerator = new ProxyGenerator();
            return (TargetType)proxyGenerator.CreateProxy(typeof(TargetType), new DataModelWrapper(typeof(TargetType)), new object());
        }
    }
 
    /// <summary>
    /// A simple DataModel wrapper
    /// </summary>
    public class DataModelWrapper : IInterceptor
    {
 
        private IDictionary<string, object> dictionary;
 
        private Type wrappedType;
 
        public DataModelWrapper(Type wrappedType)
        {
            this.wrappedType = wrappedType;
            this.dictionary = new Dictionary<string, object>();
        }
 
        #region IInterceptor Members
 
        public object Intercept(Castle.DynamicProxy.IInvocation invocation, params object[] args)
        {
            if (this.wrappedType.Equals(invocation.Method.DeclaringType))
            {
                if (invocation.Method.Name.StartsWith("get_"))
                {
                    return dictionary[GetPropertyNameFor(invocation.Method.Name)];
 
                }
                else if (invocation.Method.Name.StartsWith("set_"))
                {
                    dictionary[GetPropertyNameFor(invocation.Method.Name)] = args[0];
                    return args[0];
                }
            }
            return null;
        }
 
        private string GetPropertyNameFor(string methodName)
        {
            return methodName.Substring(4);
        }
 
        #endregion
    }
}

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  
 
 
 
Lesezeichen:


Themen-Optionen
Ansicht
Ähnliche Themen
 
Thema Autor Forum Antworten Letzter Beitrag
Java 1.5.0 RMI OHNE Stubs und Skeletons dank Dynamic Proxies ;-) Thomas Darimont Java 11 10.12.07 17:42
Frage zu Interfaces und extends Ozzy Ozborn Java 2 14.05.07 08:51
EJB3, JNDI, lokale Interfaces und Glassfish loopdruid Enterprise Java (JEE, J2EE, Spring & Co.) 0 12.12.06 22:57
Kein Konstruktor und static in Interfaces? yan1 .NET Café 8 05.08.06 10:36
C++ und Interfaces Jens Hibbeler C/C++ 2 11.10.05 10:11
» Tools
 
tutorials.de-Tools tutorial.de-Suchfeld tutorial.de-Widget tutorial.de-RSS-Feed tutorial.de-Banner
» Neue Links
 
Hits: 134
»
JHT's Planetary...
(Cinema 4D-Objekte)
Hits: 261
»
Tageslicht ohne GI
(Cinema 4D-Tutorials)
Hits: 149
»
Puzzle
(Cinema 4D-Tutorials)
Hits: 100
»
Lacreme
(Cinema 4D-Tutorials)
Hits: 190
»
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! - 79,79%
150 Stimmen
Nein, ich denke da muss noch nachgebessert werden... - 20,21%
38 Stimmen
Stimmen gesamt: 188
Du darfst bei dieser Umfrage nicht abstimmen.

 

Alle Zeitangaben in WEZ +1. Es ist jetzt 22:39 Uhr.


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