From 96748a7a596750ad4afabb85479768142bcc99cf Mon Sep 17 00:00:00 2001 From: "DESKTOP-KRJIM7J\\Roman" <23335944+rrrooommmaaa@users.noreply.github.com> Date: Sat, 11 May 2024 16:00:43 +0200 Subject: [PATCH] #3251 coerce output in RuleContext.AddOutValue --- .../ValidationRules/OutputCoercionTest.cs | 69 +++++++++++++++++++ Source/Csla/Rules/RuleContext.cs | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Source/Csla.test/ValidationRules/OutputCoercionTest.cs diff --git a/Source/Csla.test/ValidationRules/OutputCoercionTest.cs b/Source/Csla.test/ValidationRules/OutputCoercionTest.cs new file mode 100644 index 0000000000..98e5885f9a --- /dev/null +++ b/Source/Csla.test/ValidationRules/OutputCoercionTest.cs @@ -0,0 +1,69 @@ +using Csla.Rules; +using Csla.TestHelpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Csla.Test.ValidationRules +{ + public class CalculationObject : BusinessBase + { + public static readonly PropertyInfo PropertyAProperty = RegisterProperty(nameof(PropertyA)); + public int PropertyA + { + get { return GetProperty(PropertyAProperty); } + set { SetProperty(PropertyAProperty, value); } + } + + public static readonly PropertyInfo PropertyBProperty = RegisterProperty(nameof(PropertyB)); + public int PropertyB + { + get { return GetProperty(PropertyBProperty); } + set { SetProperty(PropertyBProperty, value); } + } + + public static readonly PropertyInfo SumPropertyProperty = RegisterProperty(nameof(SumProperty)); + public string SumProperty + { + get { return GetProperty(SumPropertyProperty); } + private set { LoadProperty(SumPropertyProperty, value); } + } + + protected override void AddBusinessRules() + { + base.AddBusinessRules(); + + // Register the sum properties rule + BusinessRules.AddRule(new CalcSumRule(SumPropertyProperty, PropertyAProperty, PropertyBProperty)); + } + + [Create] + private void Create() + { + BusinessRules.CheckRules(); + } + } + + [TestClass] + public class CoerceTests + { + private static TestDIContext _testDIContext; + + [ClassInitialize] + public static void ClassInitialize(TestContext testContext) + { + _testDIContext = TestDIContextFactory.CreateDefaultContext(); + } + + [TestMethod] + public void CalcSumRole_SavesToStringProperty() + { + // Create an instance of a DataPortal that can be used for instantiating objects + var dataPortal = _testDIContext.CreateDataPortal(); + var obj = dataPortal.Create(); + + obj.PropertyA = 10; + obj.PropertyB = 20; + + Assert.AreEqual("30", obj.SumProperty, "string SumProperty should reflect the sum of PropertyA and PropertyB."); + } + } +} diff --git a/Source/Csla/Rules/RuleContext.cs b/Source/Csla/Rules/RuleContext.cs index ae040da6bc..fed49983dd 100644 --- a/Source/Csla/Rules/RuleContext.cs +++ b/Source/Csla/Rules/RuleContext.cs @@ -408,7 +408,7 @@ public void AddOutValue(object value) /// When property is not defined in AffectedProperties list. public void AddOutValue(Csla.Core.IPropertyInfo property, object value) { - _outputPropertyValues.Value[property] = value; + _outputPropertyValues.Value[property] = (value is null) ? property.DefaultValue : Csla.Utilities.CoerceValue(property.Type, value.GetType(), null, value); }