Last active
March 8, 2023 11:12
-
-
Save soraphis/8b41dfa0d7eff43ad894228d65df9c3d to your computer and use it in GitHub Desktop.
a realproxy implementation of IRevertibleChangeTracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright © 2021 github.com/soraphis | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
using System; | |
using System.Diagnostics; | |
using ConsoleApplication1.Utils; | |
namespace ConsoleApplication1 | |
{ | |
public class MySettings : MarshalByRefObject | |
{ | |
public int amountOfThings { get; set; } = 3; | |
public string nameOfStuff { get; set; } = "unnamed"; | |
} | |
/// <summary> | |
/// This shows example usage of the RevertibleChangeProxy | |
/// </summary> | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var settings = new MySettings(){ amountOfThings = 3, nameOfStuff = "unnamed"}; | |
var changeableSettings = RevertibleChangeProxy<MySettings>.Wrap(settings); | |
Debug.Assert(! changeableSettings.IsChanged); | |
changeableSettings.Values.amountOfThings = 7; | |
Debug.Assert(changeableSettings.IsChanged); | |
Debug.Assert(changeableSettings.Values.amountOfThings == 7); | |
Debug.Assert(changeableSettings.Original.amountOfThings == 3); | |
changeableSettings.RejectChanges(); | |
Debug.Assert(! changeableSettings.IsChanged); | |
Debug.Assert(changeableSettings.Values.amountOfThings == 3); | |
Debug.Assert(changeableSettings.Original.amountOfThings == 3); | |
changeableSettings.Values.amountOfThings = 7; | |
changeableSettings.AcceptChanges(); | |
Debug.Assert(! changeableSettings.IsChanged); | |
Debug.Assert(changeableSettings.Values.amountOfThings == 7); | |
Debug.Assert(changeableSettings.Original.amountOfThings == 7); | |
settings.amountOfThings = 4; // dangerous! modifying the underlying object, will result in a 'changed' state on the changeable | |
changeableSettings.Original.amountOfThings = 4; // dangerous! modifying the underlying object, will result in a 'changed' state on the changeable | |
Debug.Assert(changeableSettings.IsChanged); | |
Debug.Assert(changeableSettings.Values.amountOfThings == 7); | |
Debug.Assert(changeableSettings.Original.amountOfThings == 4); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright © 2021 github.com/soraphis | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Messaging; | |
using System.Runtime.Remoting.Proxies; | |
namespace ConsoleApplication1.Utils | |
{ | |
/// <summary> | |
/// Probably not the best name, but its just to create an interface for the IDE to show all useful members | |
/// </summary> | |
public interface IAutoImplementRevertibleChangeTracking<T> : IRevertibleChangeTracking | |
{ | |
T Values { get; } | |
T Original { get; } | |
} | |
public class RevertibleChangeProxy<T> : RealProxy, IRemotingTypeInfo, IRevertibleChangeTracking where T : MarshalByRefObject | |
{ | |
Dictionary<string, object> changeData = new Dictionary<string, object>(); | |
private T _decorated; | |
private PropertyInfo[] props; | |
public RevertibleChangeProxy(T decorated) : base(typeof(T)) | |
{ | |
_decorated = decorated; | |
props = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public); | |
foreach (var pub in props) | |
{ | |
changeData[pub.Name] = pub.GetValue(_decorated); | |
} | |
} | |
private const string IsChangedPropertyName = "get_" + nameof(IsChanged); | |
private const string ValuesPropertyName = "get_Values"; | |
private const string OriginalPropertyName = "get_Original"; | |
public override IMessage Invoke(IMessage msg) | |
{ | |
var methodCall = msg as IMethodCallMessage; | |
if (methodCall is null) return null; | |
try | |
{ | |
switch (methodCall.MethodName) | |
{ | |
case nameof(AcceptChanges): | |
AcceptChanges(); | |
return new ReturnMessage(null, null, 0, methodCall.LogicalCallContext, methodCall); | |
case nameof(RejectChanges): | |
RejectChanges(); | |
return new ReturnMessage(null, null, 0, methodCall.LogicalCallContext, methodCall); | |
case IsChangedPropertyName: | |
return new ReturnMessage(IsChanged, null, 0, methodCall.LogicalCallContext, methodCall); | |
case ValuesPropertyName: | |
return new ReturnMessage((T) this.GetTransparentProxy(), null, 0, methodCall.LogicalCallContext, methodCall); | |
case OriginalPropertyName: | |
return new ReturnMessage(_decorated, null, 0, methodCall.LogicalCallContext, methodCall); | |
default: | |
{ | |
var propName = methodCall.MethodName.Substring(4); | |
if (methodCall.MethodName.StartsWith("set_") && changeData.ContainsKey(propName)) | |
{ | |
changeData[propName] = methodCall.InArgs[0]; | |
return new ReturnMessage(null, null, 0, methodCall.LogicalCallContext, methodCall); | |
}else if(methodCall.MethodName.StartsWith("get_") && changeData.TryGetValue(propName, out var changedPropertyValue)){ | |
return new ReturnMessage(changedPropertyValue, null, 0, methodCall.LogicalCallContext, methodCall); | |
}else{ | |
var result = methodCall.MethodBase.Invoke(_decorated, methodCall.InArgs); | |
return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall); | |
} | |
} | |
} | |
} | |
catch (TargetInvocationException ex) | |
{ | |
var exception = ex.InnerException; | |
return new ReturnMessage(exception, methodCall); | |
} | |
} | |
public void AcceptChanges() | |
{ | |
foreach (var pub in props) | |
{ | |
pub.SetValue(_decorated, changeData[pub.Name]); | |
} | |
} | |
public bool IsChanged => !props.All(x => x.GetValue(_decorated).Equals(changeData[x.Name])); | |
public void RejectChanges() | |
{ | |
if (!IsChanged) return; | |
foreach (var pub in props) | |
{ | |
changeData[pub.Name] = pub.GetValue(_decorated); | |
} | |
} | |
public static IAutoImplementRevertibleChangeTracking<T> Wrap<T>(T target) where T : MarshalByRefObject | |
{ | |
return (IAutoImplementRevertibleChangeTracking<T>) new RevertibleChangeProxy<T>(target).GetTransparentProxy(); | |
} | |
public bool CanCastTo(Type fromType, object o) | |
{ | |
return fromType == typeof(IRevertibleChangeTracking) || fromType == typeof(IAutoImplementRevertibleChangeTracking<T>) || fromType == typeof(T); | |
} | |
public string TypeName | |
{ | |
get => GetProxiedType().FullName; | |
set => throw new NotSupportedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment