-
11.11.06 14:48 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.327
- Blog-Einträge
- 29
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
Code csharp: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ß TomJava 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
-
21.12.06 14:31 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.327
- Blog-Einträge
- 29
Hallo!
kleiner Nachtrag, mit Generics klappts natürlich auch:
Code csharp: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ß TomJava 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
Ähnliche Themen
-
Java 1.5.0 RMI OHNE Stubs und Skeletons dank Dynamic Proxies ;-)
Von Thomas Darimont im Forum JavaAntworten: 13Letzter Beitrag: 26.05.10, 00:44 -
Frage zu Interfaces und extends
Von Ozzy Ozborn im Forum JavaAntworten: 2Letzter Beitrag: 14.05.07, 08:51 -
EJB3, JNDI, lokale Interfaces und Glassfish
Von loopdruid im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 0Letzter Beitrag: 12.12.06, 22:57 -
Kein Konstruktor und static in Interfaces?
Von yan1 im Forum .NET CaféAntworten: 8Letzter Beitrag: 05.08.06, 10:36 -
C++ und Interfaces
Von Jens Hibbeler im Forum C/C++Antworten: 2Letzter Beitrag: 11.10.05, 10:11




Zitieren
Login





Lesezeichen