Browse Source

Common:

23.12.2019 v.1.0.0.5
New
 - Add TypePropertyLocator helper
 - Add Result class (in Value branch)
Dalibor Votruba 6 years ago
parent
commit
10d482908b

+ 4 - 4
Common/Common.csproj

@@ -42,6 +42,8 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Data\Extensions\SqlCommandExt.cs" />
+    <Compile Include="Reflection\TypePropertyLocator.cs" />
+    <Compile Include="Value\Result.cs" />
     <Compile Include="Xml\Extensions\XmlNodeExt.cs" />
     <Compile Include="Xml\Extensions\XmlReaderExt.cs" />
     <Compile Include="Xml\Extensions\XPathNavigatorExt.cs" />
@@ -49,14 +51,12 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Security\Secure.cs" />
   </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Extensions\" />
-    <Folder Include="Helpers\" />
-  </ItemGroup>
+  <ItemGroup />
   <ItemGroup>
     <Content Include="ReleaseNote.txt" />
   </ItemGroup>
   <ItemGroup>
+    <None Include="create_package.ps1" />
     <None Include="QDR.FND.Common.nuspec">
       <SubType>Designer</SubType>
     </None>

+ 2 - 2
Common/Properties/AssemblyInfo.cs

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.1")]
-[assembly: AssemblyFileVersion("1.0.0.1")]
+[assembly: AssemblyVersion("1.0.0.5")]
+[assembly: AssemblyFileVersion("1.0.0.5")]

+ 5 - 6
Common/QDR.FND.Common.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Quadarax.Foundation.Common</id>
-    <version>1.0.0.4</version>
+    <version>1.0.0.5</version>
     <title>Quadarax.Foundation.Common</title>
     <authors>Dalibor Votruba, Quadarax</authors>
     <owners>Dalibor Votruba</owners>
@@ -11,11 +11,10 @@
     <requireLicenseAcceptance>false</requireLicenseAcceptance>
     <description>Implementations of simple patterns and misc. extensions.</description>
     <releaseNotes>
-      3.12.2019 v.1.0.0.4
+      23.12.2019 v.1.0.0.5
       New
-      - Add SqlCommand extension
-      Update
-      - Change classes location to propper namespaces
+      - Add TypePropertyLocator helper
+      - Add Result class (in Value branch)
     </releaseNotes>
     <copyright>Copyright (c) 2019 Quadarax</copyright>
     <tags>quadarax, foundarion, common, qdr, fnd, library, extensions</tags>
@@ -26,6 +25,6 @@
   <files>
     <file src="ReleaseNote.txt" />
     <file src="bin\Release\QDR.FND.Common.dll" target="lib\net472\QDR.FND.Common.dll" />
-	<file src="bin\Release\QDR.FND.Common.pdb" target="lib\net472\QDR.FND.Common.pdb" />
+	  <file src="bin\Release\QDR.FND.Common.pdb" target="lib\net472\QDR.FND.Common.pdb" />
   </files>
 </package>

+ 48 - 0
Common/Reflection/TypePropertyLocator.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+
+namespace Quadarax.Foundation.Common.Reflection
+{
+    public static class TypePropertyLocator
+    {
+        private static readonly IDictionary<string, PropertyInfo> _propertyCache = new Dictionary<string, PropertyInfo>();
+
+
+        public static PropertyInfo GetProperty(this Type type, string propertyName)
+        {
+            var key = GetCacheKey(type, propertyName);
+            if (_propertyCache.ContainsKey(key))
+                return _propertyCache[key];
+            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase);
+            var property = properties.SingleOrDefault(x => x.Name == propertyName);
+            if (property!=null)
+                _propertyCache.Add(key,property);
+            return property;
+        }
+
+        public static object GetPropertyValue(this Type type, string propertyName, object sourceObject)
+        {
+            if (sourceObject==null)
+                throw new ArgumentNullException(nameof(sourceObject));
+            var property = type.GetProperty(propertyName);
+            if (property == null)
+                throw new ArgumentException($"Property '{propertyName}' doesn't exists in type '{type.Name}'.", nameof(propertyName));
+            return property.GetValue(sourceObject);
+        }
+
+        public static string GetPropertyValueAsString(this Type type, string propertyName, object sourceObject)
+        {
+            var value = type.GetPropertyValue(propertyName, sourceObject);
+            return value != null? value.ToString() : string.Empty;
+        }
+
+        private static string GetCacheKey(Type type, string propertyName)
+        {
+            if (string.IsNullOrEmpty(propertyName))
+                throw new ArgumentNullException(nameof(propertyName));
+            return type.FullName + "_" + propertyName;
+        }
+    }
+}

+ 5 - 0
Common/ReleaseNote.txt

@@ -1,3 +1,8 @@
+23.12.2019 v.1.0.0.5
+New
+ - Add TypePropertyLocator helper
+ - Add Result class (in Value branch)
+
 3.12.2019 v.1.0.0.4
 New
  - Add SqlCommand extension

+ 24 - 0
Common/Value/Result.cs

@@ -0,0 +1,24 @@
+using System;
+
+namespace Quadarax.Foundation.Common.Value
+{
+    
+    public class Result
+    {        
+    
+        public bool IsSuccess { get; }
+        public Exception ThrownException { get; }
+
+
+        public Result(Exception e)
+        {
+            ThrownException = e;
+            IsSuccess = false;
+        }
+
+        public Result()
+        {
+            IsSuccess = true;
+        }
+    }
+}