├── .gitattributes
├── .gitignore
├── .versionrc.json
├── CHANGELOG.md
├── D365Extensions
├── D365Extensions.Benchmark
│ ├── App.config
│ ├── D365Extensions.Benchmark.csproj
│ ├── EntityLogicalName.cs
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── ProperyExpression.cs
│ ├── TestEntities.cs
│ └── packages.config
├── D365Extensions.Tests
│ ├── AliasedValueExtensionsTests.cs
│ ├── AliasedValueTests.cs
│ ├── ColumnSetExtensionsTests.cs
│ ├── ColumnSetTests.cs
│ ├── ConditionExpressionTests.cs
│ ├── D365Extensions.Tests.csproj
│ ├── DataCollectionExtensionsTests.cs
│ ├── EarlyBoundTypes.cs
│ ├── EntityCollectionExtensionsTests.cs
│ ├── EntityExtensionsTests.cs
│ ├── EntityIdComparer.cs
│ ├── EntityImageCollectionExtensionsTests.cs
│ ├── EntityReferenceExtensionsTests.cs
│ ├── EntityReferenceTests.cs
│ ├── ExecuteMultipleProgressTests.cs
│ ├── FakeExecuteMultipleExecutor.cs
│ ├── FakeExecuteMultipleExecutorTests.cs
│ ├── FilterExpressionExtensionsTests.cs
│ ├── IOrganizationServiceExecuteExtensionsTests.cs
│ ├── IOrganizationServiceRetrieveMultipleExtensionsTests.cs
│ ├── IPluginExecutionContextExtensionsTests.cs
│ ├── IServiceProviderExtensionsTests.cs
│ ├── ITracingServiceExtensionsTests.cs
│ ├── KeyAttributeCollectionExtensionsTests.cs
│ ├── KeyAttributeCollectionTests.cs
│ ├── LinkEntityExtensionsTests.cs
│ ├── LinkEntityTests.cs
│ ├── LogicalNameTests.cs
│ ├── OptionSetValueTests.cs
│ ├── OrderExpressionTests.cs
│ ├── OrganizationRequestCollectionEnumeratorTests.cs
│ ├── OrganizationServiceFaultExtensionsTests.cs
│ ├── PluginExecutionTraceContextTests.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── QueryBaseExtensionsTests.cs
│ ├── QueryByAttributeExtensionsTests.cs
│ ├── QueryExpressionExtensionsTests.cs
│ ├── StringBuilderExtensionsTests.cs
│ ├── TestEntities.cs
│ └── app.config
├── D365Extensions.sln
└── D365Extensions
│ ├── AliasedValue.cs
│ ├── AliasedValueExtensions.cs
│ ├── CodeActivityContextExtensions.cs
│ ├── ColumnSet.cs
│ ├── ColumnSetExtensions.cs
│ ├── ConditionExpression.cs
│ ├── D365Extensions.csproj
│ ├── D365Extensions.nuspec
│ ├── DataCollectionExtensions.cs
│ ├── EntityCollectionExtensions.cs
│ ├── EntityExtensions.cs
│ ├── EntityImageCollectionExtensions.cs
│ ├── EntityReference.cs
│ ├── EntityReferenceExtensions.cs
│ ├── ErrorCodes.cs
│ ├── ExecuteMultipleOperationResponse.cs
│ ├── ExecuteMultipleProgress.cs
│ ├── ExecuteMultipleResponseExtensions.cs
│ ├── ExecuteMultipleResponseItemExtensions.cs
│ ├── FilterExpressionExtensions.cs
│ ├── IOrganizationServiceAssosiateExtensions.cs
│ ├── IOrganizationServiceDeleteExtensions.cs
│ ├── IOrganizationServiceDisassociateExtensions.cs
│ ├── IOrganizationServiceExecuteExtensions.cs
│ ├── IOrganizationServiceRetrieveExtensions.cs
│ ├── IOrganizationServiceRetrieveMultipleExtensions.cs
│ ├── IOrganizationServiceUpdateExtensions.cs
│ ├── IPluginExecutionContextExtensions.cs
│ ├── IServiceProviderExtensions.cs
│ ├── ITracingServiceExtensions.cs
│ ├── KeyAttributeCollection.cs
│ ├── KeyAttributeCollectionExtensions.cs
│ ├── LinkEntity.cs
│ ├── LinkEntityExtentions.cs
│ ├── LogicalName.cs
│ ├── MoneyExtensions.cs
│ ├── OptionSetValue.cs
│ ├── OptionSetValueExtensions.cs
│ ├── OrderExpression.cs
│ ├── OrganizationRequestCollectionEnumerator.cs
│ ├── OrganizationServiceFaultExtensions.cs
│ ├── PluginExecutionTraceContext.cs
│ ├── PluginExecutionTraceContextSettings.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── QueryBaseExtensions.cs
│ ├── QueryByAttributeExtensions.cs
│ ├── QueryExpressionExtensions.cs
│ ├── StringBuilderExtensions.cs
│ ├── scripts
│ └── GenerateErrorCodesEnum.ps1
│ └── СheckParam.cs
├── LICENSE
├── README.md
├── azure-pipelines.yml
├── commitlint.config.js
├── fixrmlogo_64.png
├── package-lock.json
├── package.json
└── scripts
└── azure-pipeline-updater.js
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.snk
202 | *.publishsettings
203 | orleans.codegen.cs
204 |
205 | # Since there are multiple workflows, uncomment next line to ignore bower_components
206 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
207 | #bower_components/
208 |
209 | # RIA/Silverlight projects
210 | Generated_Code/
211 |
212 | # Backup & report files from converting an old project file
213 | # to a newer Visual Studio version. Backup files are not needed,
214 | # because we have git ;-)
215 | _UpgradeReport_Files/
216 | Backup*/
217 | UpgradeLog*.XML
218 | UpgradeLog*.htm
219 |
220 | # SQL Server files
221 | *.mdf
222 | *.ldf
223 | *.ndf
224 |
225 | # Business Intelligence projects
226 | *.rdl.data
227 | *.bim.layout
228 | *.bim_*.settings
229 |
230 | # Microsoft Fakes
231 | FakesAssemblies/
232 |
233 | # GhostDoc plugin setting file
234 | *.GhostDoc.xml
235 |
236 | # Node.js Tools for Visual Studio
237 | .ntvs_analysis.dat
238 | node_modules/
239 |
240 | # Typescript v1 declaration files
241 | typings/
242 |
243 | # Visual Studio 6 build log
244 | *.plg
245 |
246 | # Visual Studio 6 workspace options file
247 | *.opt
248 |
249 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
250 | *.vbw
251 |
252 | # Visual Studio LightSwitch build output
253 | **/*.HTMLClient/GeneratedArtifacts
254 | **/*.DesktopClient/GeneratedArtifacts
255 | **/*.DesktopClient/ModelManifest.xml
256 | **/*.Server/GeneratedArtifacts
257 | **/*.Server/ModelManifest.xml
258 | _Pvt_Extensions
259 |
260 | # Paket dependency manager
261 | .paket/paket.exe
262 | paket-files/
263 |
264 | # FAKE - F# Make
265 | .fake/
266 |
267 | # JetBrains Rider
268 | .idea/
269 | *.sln.iml
270 |
271 | # CodeRush
272 | .cr/
273 |
274 | # Python Tools for Visual Studio (PTVS)
275 | __pycache__/
276 | *.pyc
277 |
278 | # Cake - Uncomment if you are using it
279 | # tools/**
280 | # !tools/packages.config
281 |
282 | # Telerik's JustMock configuration file
283 | *.jmconfig
284 |
285 | # BizTalk build output
286 | *.btp.cs
287 | *.btm.cs
288 | *.odx.cs
289 | *.xsd.cs
290 |
291 | # Visual Studio Life Unit Testing config
292 | *.lutconfig
--------------------------------------------------------------------------------
/.versionrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "header": "# What's new",
3 | "skip": {
4 | "bump": false,
5 | "changelog": false,
6 | "commit": false,
7 | "tag": false
8 | },
9 | "bumpFiles": [
10 | {
11 | "temporary_workaround": "https://github.com/conventional-changelog/standard-version/issues/533",
12 | "filename": "package.json",
13 | "type": "json"
14 | },
15 | {
16 | "filename": "azure-pipelines.yml",
17 | "updater": "./scripts/azure-pipeline-updater.js"
18 | }
19 | ]
20 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/App.config:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/EntityLogicalName.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Xrm.Sdk;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using System.Reflection;
8 | using Microsoft.Xrm.Sdk.Client;
9 | using System.Collections.Concurrent;
10 |
11 | namespace D365Extensions
12 | {
13 | public static class EntityLogicalName
14 | {
15 | static ConcurrentDictionary typeChache = new ConcurrentDictionary();
16 |
17 | static ConcurrentDictionary typeChache2 = new ConcurrentDictionary();
18 |
19 | public static string GetName() where T : Entity
20 | {
21 | return typeof(T).Name.ToLowerInvariant();
22 | }
23 |
24 | public static string GetNameC() where T : Entity
25 | {
26 | var type = typeof(T);
27 |
28 | typeChache.TryGetValue(type, out string logicalName);
29 | if (logicalName == null)
30 | {
31 | logicalName = type.Name.ToLowerInvariant();
32 |
33 | typeChache.TryAdd(type, logicalName);
34 | }
35 |
36 | return logicalName;
37 | }
38 |
39 | public static string GetNameR() where T : Entity
40 | {
41 | return typeof(T).GetCustomAttribute().LogicalName;
42 | }
43 |
44 | public static string GetNameRC() where T : Entity
45 | {
46 | var type = typeof(T);
47 |
48 | typeChache2.TryGetValue(type, out string logicalName);
49 | if (logicalName == null)
50 | {
51 | logicalName = type.GetCustomAttribute().LogicalName;
52 |
53 | typeChache2.TryAdd(type, logicalName);
54 | }
55 |
56 | return logicalName;
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/Program.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using BenchmarkDotNet.Running;
3 | using D365Extensions.Tests;
4 | using System;
5 |
6 | namespace D365Extensions.Benchmark
7 | {
8 | [MemoryDiagnoser]
9 | public class ENBenchmark
10 | {
11 | [Benchmark]
12 | public string GetName() => EntityLogicalName.GetName();
13 |
14 | [Benchmark]
15 | public string GetNameC() => EntityLogicalName.GetNameC();
16 |
17 | [Benchmark]
18 | public string GetNameR() => EntityLogicalName.GetNameR();
19 |
20 | [Benchmark]
21 | public string GetNameRC() => EntityLogicalName.GetNameRC();
22 | }
23 |
24 | [MemoryDiagnoser]
25 | public class PEBenchmark
26 | {
27 | [Benchmark]
28 | public string LambdaRef() => ProperyExpression.GetName(e => e.Property_1);
29 |
30 | [Benchmark]
31 | public string LambdaVal() => ProperyExpression.GetName(e => e.Property_2);
32 |
33 | [Benchmark]
34 | public string ReflectionRef() => ProperyExpression.GetNameR(e => e.Property_1);
35 |
36 | [Benchmark]
37 | public string ReflectionVal() => ProperyExpression.GetNameR(e => e.Property_2);
38 |
39 | [Benchmark]
40 | public string ChacheRef() => ProperyExpression.GetNameC(e => e.Property_1);
41 |
42 | [Benchmark]
43 | public string ChacheVal() => ProperyExpression.GetNameC(e => e.Property_2);
44 |
45 | [Benchmark]
46 | public string Chache2Ref() => ProperyExpression.GetNameC2(e => e.Property_1);
47 |
48 | [Benchmark]
49 | public string Chache2Val() => ProperyExpression.GetNameC2(e => e.Property_2);
50 | }
51 |
52 | public class ToLowerBenchmark
53 | {
54 | [Benchmark]
55 | public string ToLowerInvariant() => "AccountNumber".ToLowerInvariant();
56 |
57 | [Benchmark]
58 | public string ToLower() => "AccountNumber".ToLower();
59 | }
60 |
61 | public class Program
62 | {
63 | static void Main(string[] args)
64 | {
65 | var summary = BenchmarkRunner.Run();
66 | //var summary = BenchmarkRunner.Run();
67 | //var summary = BenchmarkRunner.Run();
68 |
69 | Console.ReadKey();
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("D365Extensions.Benchmark")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("D365Extensions.Benchmark")]
13 | [assembly: AssemblyCopyright("Copyright © 2021")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("1eca11b0-de44-4c41-ac11-85b7c4af0bfa")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/ProperyExpression.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions;
2 | using Microsoft.Xrm.Sdk;
3 | using System;
4 | using System.Collections.Concurrent;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Linq.Expressions;
8 | using System.Reflection;
9 |
10 | namespace D365Extensions
11 | {
12 | ///
13 | /// Helper class for reading property names from lambda expressions
14 | ///
15 | public static class ProperyExpression
16 | {
17 | public static List GetNames(params Expression>[] expressions)
18 | {
19 | return expressions
20 | .Select(e => GetName(e))
21 | .ToList();
22 | }
23 |
24 | public static string GetName(Expression> expression)
25 | {
26 | return GetName(expression?.Body);
27 | }
28 |
29 | public static string GetNameR(Expression> expression)
30 | {
31 | return GetNameR(expression?.Body);
32 | }
33 |
34 | public static string GetNameC(Expression> expression)
35 | {
36 | return GetNameC(expression?.Body);
37 | }
38 |
39 | static ConcurrentDictionary expressionChache = new ConcurrentDictionary();
40 |
41 | public static string GetNameC2(Expression> expression)
42 | {
43 | expressionChache.TryGetValue(expression, out string logicalName);
44 |
45 | if (logicalName == null)
46 | {
47 | logicalName = GetNameC2(expression?.Body);
48 | expressionChache.TryAdd(expression, logicalName);
49 | }
50 |
51 | return logicalName;
52 | }
53 |
54 | static string GetName(Expression expression)
55 | {
56 | if (expression == null) return null;
57 |
58 | // Property, field of method returning value type
59 | if (expression is UnaryExpression unaryExpression)
60 | {
61 | expression = unaryExpression.Operand;
62 | }
63 |
64 | // Reference type property or field
65 | if (expression is MemberExpression memberExpession)
66 | {
67 | MemberInfo member = memberExpession.Member;
68 |
69 | return member.Name.ToLowerInvariant();
70 | }
71 |
72 | throw new ArgumentException(nameof(expression));
73 | }
74 |
75 | static string GetNameR(Expression expression)
76 | {
77 | if (expression == null) return null;
78 |
79 | // Property, field of method returning value type
80 | if (expression is UnaryExpression unaryExpression)
81 | {
82 | expression = unaryExpression.Operand;
83 | }
84 |
85 | // Reference type property or field
86 | if (expression is MemberExpression memberExpession)
87 | {
88 | MemberInfo member = memberExpession.Member;
89 |
90 | return member.GetCustomAttribute(false).LogicalName;
91 | }
92 |
93 | throw new ArgumentException(nameof(expression));
94 | }
95 |
96 | static ConcurrentDictionary memberChache = new ConcurrentDictionary();
97 |
98 | static string GetNameC(Expression expression)
99 | {
100 | if (expression == null) return null;
101 |
102 | // Property, field of method returning value type
103 | if (expression is UnaryExpression unaryExpression)
104 | {
105 | expression = unaryExpression.Operand;
106 | }
107 |
108 | // Reference type property or field
109 | if (expression is MemberExpression memberExpession)
110 | {
111 | MemberInfo member = memberExpession.Member;
112 |
113 | memberChache.TryGetValue(member, out string logicalName);
114 |
115 | if (logicalName == null)
116 | {
117 | logicalName = member.GetCustomAttribute(false).LogicalName;
118 |
119 | memberChache.TryAdd(member, logicalName);
120 | }
121 |
122 | return logicalName;
123 | }
124 |
125 | throw new ArgumentException(nameof(expression));
126 | }
127 |
128 | static string GetNameC2(Expression expression)
129 | {
130 | if (expression == null) return null;
131 |
132 | // Property, field of method returning value type
133 | if (expression is UnaryExpression unaryExpression)
134 | {
135 | expression = unaryExpression.Operand;
136 | }
137 |
138 | // Reference type property or field
139 | if (expression is MemberExpression memberExpession)
140 | {
141 | MemberInfo member = memberExpession.Member;
142 |
143 | return member.GetCustomAttribute(false).LogicalName;
144 | }
145 |
146 | throw new ArgumentException(nameof(expression));
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/TestEntities.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Microsoft.Xrm.Sdk;
7 | using Microsoft.Xrm.Sdk.Client;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | ///
12 | /// Entity for bencmark
13 | ///
14 | [EntityLogicalName("custom_entity")]
15 | public class CustomEntity : Entity
16 | {
17 | public static string EnityLogicalName = "custom_entity";
18 |
19 | public CustomEntity() : base(EnityLogicalName)
20 | {
21 | }
22 |
23 | [AttributeLogicalName("prop_1")]
24 | public string Property_1
25 | {
26 | get => GetAttributeValue("prop_1");
27 | set => SetAttributeValue("prop_1", value);
28 | }
29 |
30 | [AttributeLogicalName("prop_2")]
31 | public int? Property_2
32 | {
33 | get => GetAttributeValue ("prop_2");
34 | set => SetAttributeValue("prop_2", value);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Benchmark/packages.config:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/AliasedValueExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class AliasedValueExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void IsPrimaryKeyShouldReturnTrueTest()
16 | {
17 | // Setup
18 | var av = new AliasedValue(
19 | entityLogicalName: "account",
20 | attributeLogicalName: "accountid",
21 | value: Guid.NewGuid());
22 |
23 | // Act
24 | var idPrimaryKey = av.IsPrimaryKey();
25 |
26 | // Assert
27 | Assert.IsTrue(idPrimaryKey);
28 | }
29 |
30 | [TestMethod()]
31 | public void IsPrimaryKeyShouldReturnTrueForActivityTest()
32 | {
33 | FakeXrmEasy.XrmFakedContext context = new FakeXrmEasy.XrmFakedContext();
34 |
35 |
36 | // Setup
37 | var av = new AliasedValue(
38 | entityLogicalName: "task",
39 | attributeLogicalName: "activityid",
40 | value: Guid.NewGuid());
41 |
42 | // Act
43 | var idPrimaryKey = av.IsPrimaryKey();
44 |
45 | // Assert
46 | Assert.IsTrue(idPrimaryKey);
47 | }
48 |
49 | [TestMethod()]
50 | public void IsPrimaryKeyShouldReturnFalseTest()
51 | {
52 | // Setup
53 | var av = new AliasedValue(
54 | entityLogicalName: "account",
55 | attributeLogicalName: "accountnumber",
56 | value: "42");
57 |
58 | // Act
59 | var idPrimaryKey = av.IsPrimaryKey();
60 |
61 | // Assert
62 | Assert.IsFalse(idPrimaryKey);
63 | }
64 |
65 | [TestMethod()]
66 | public void GetValueTest()
67 | {
68 | // Setup
69 | var av = new AliasedValue(
70 | entityLogicalName: "account",
71 | attributeLogicalName: "accountnumber",
72 | value: "42");
73 |
74 | // Act
75 | var actualValue = av.GetValue();
76 |
77 | // Assert
78 | Assert.AreEqual("42", actualValue);
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/AliasedValueTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class AliasedValueTests
14 | {
15 | [TestMethod()]
16 | public void AliasedValueTest()
17 | {
18 | // Setup
19 | var expectedEntityName = "account";
20 | var expectedAttributeName = "accountnumber";
21 | var expectedValue = "42";
22 |
23 | // Act
24 | AliasedValue aliasedValue = new AliasedValue(a=> a.AccountNumber, expectedValue);
25 |
26 | // Assert
27 | Assert.AreEqual(expectedEntityName, aliasedValue.EntityLogicalName);
28 | Assert.AreEqual(expectedAttributeName, aliasedValue.AttributeLogicalName);
29 | Assert.AreEqual(expectedValue, aliasedValue.Value);
30 | }
31 |
32 | [TestMethod()]
33 | public void NullTest()
34 | {
35 | // Setup
36 | AliasedValue aliasedValueT = null;
37 |
38 | // Act
39 | AliasedValue aliasedValue = aliasedValueT;
40 |
41 | // Assert
42 | Assert.IsNull(aliasedValue);
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/ColumnSetExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class ColumnSetExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void AddColumnTest()
16 | {
17 | // Setup
18 | string expectedColumn = nameof(TestEntity.ReferenceTypeProperty).ToLower();
19 |
20 | // Act
21 | ColumnSet columnSet = new ColumnSet();
22 | columnSet.AddColumn(t => t.ReferenceTypeProperty);
23 |
24 | // Assert
25 | Assert.AreEqual(1, columnSet.Columns.Count);
26 | Assert.AreEqual(expectedColumn, columnSet.Columns[0]);
27 | }
28 |
29 | [TestMethod()]
30 | public void AddColumnsTest()
31 | {
32 | // Setup
33 | string expectedColumn1 = nameof(TestEntity.ReferenceTypeProperty).ToLower();
34 | string expectedColumn2 = nameof(TestEntity.ValueTypeProperty).ToLower();
35 |
36 | // Act
37 | ColumnSet columnSet = new ColumnSet();
38 | columnSet.AddColumns(t => t.ReferenceTypeProperty, t=> t.ValueTypeProperty);
39 |
40 | // Assert
41 | Assert.AreEqual(2, columnSet.Columns.Count);
42 | Assert.AreEqual(expectedColumn1, columnSet.Columns[0]);
43 | Assert.AreEqual(expectedColumn2, columnSet.Columns[1]);
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/ColumnSetTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Linq.Expressions;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class ColumnSetTests
14 | {
15 | [TestMethod()]
16 | public void Defaut_Constructor_Test()
17 | {
18 | // Act
19 | ColumnSet columnSet = new ColumnSet();
20 |
21 | var query = new QueryExpression();
22 |
23 | // Assert
24 | var actual = columnSet.Columns;
25 |
26 | Assert.AreEqual(0, actual.Count);
27 | }
28 |
29 | [TestMethod()]
30 | public void Implicit_Cast_Test()
31 | {
32 | // Setup
33 | string expected1 = nameof(TestEntity.ReferenceTypeProperty).ToLower();
34 | string expected2 = nameof(TestEntity.ValueTypeProperty).ToLower();
35 |
36 | // Act
37 | ColumnSet columnSet = new ColumnSet(
38 | t => t.ReferenceTypeProperty,
39 | t => t.ValueTypeProperty);
40 |
41 | var actual = columnSet.Columns;
42 |
43 | Assert.AreEqual(2, actual.Count);
44 | Assert.AreEqual(expected1, actual[0]);
45 | Assert.AreEqual(expected2, actual[1]);
46 | }
47 |
48 | [TestMethod()]
49 | public void ColumnSet_Test()
50 | {
51 | // Setup
52 | Expression> expected1 = (t) => t.ReferenceTypeProperty;
53 | Expression> expected2 = (t) => t.ValueTypeProperty;
54 |
55 | // Act
56 | ColumnSet columnSet = new ColumnSet(
57 | expected1,
58 | expected2);
59 |
60 | var actual = columnSet.Columns;
61 |
62 | Assert.AreEqual(2, actual.Count);
63 | Assert.AreEqual(expected1, actual[0]);
64 | Assert.AreEqual(expected2, actual[1]);
65 | }
66 |
67 | [TestMethod()]
68 | public void AddColumn_Test()
69 | {
70 | // Setup
71 | ColumnSet columnSet = new ColumnSet();
72 | Expression> expected = (t) => t.ReferenceTypeProperty;
73 |
74 | // Act
75 | columnSet.AddColumn(t => t.ReferenceTypeProperty);
76 | var actual = columnSet.Columns;
77 |
78 | // Assert
79 | Assert.AreEqual(1, actual.Count);
80 | Assert.AreEqual(LogicalName.GetName(expected), LogicalName.GetName(actual[0]));
81 | }
82 |
83 | [TestMethod()]
84 | public void AddColumns_Test()
85 | {
86 | // Setup
87 | ColumnSet columnSet = new ColumnSet();
88 | Expression> expected1 = (t) => t.ReferenceTypeProperty;
89 | Expression> expected2 = (t) => t.ValueTypeProperty;
90 |
91 | // Act
92 | columnSet.AddColumns(t => t.ReferenceTypeProperty, t => t.ValueTypeProperty);
93 | var actual = columnSet.Columns;
94 |
95 | // Assert
96 | Assert.AreEqual(2, actual.Count);
97 | Assert.AreEqual(LogicalName.GetName(expected1), LogicalName.GetName(actual[0]));
98 | Assert.AreEqual(LogicalName.GetName(expected2), LogicalName.GetName(actual[1]));
99 | }
100 |
101 | [TestMethod()]
102 | public void Null_Test()
103 | {
104 | // Setup
105 | ColumnSet columnSetT = null;
106 |
107 | // Act
108 | ColumnSet columnSet = columnSetT;
109 |
110 | // Assert
111 | Assert.IsNull(columnSet);
112 | }
113 | }
114 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/ConditionExpressionTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class ConditionExpressionTests
13 | {
14 | [TestMethod()]
15 | public void Constructor_Full_Test()
16 | {
17 | // Setup
18 | var expectedEntityName = TestEntity.EntityLogicalName;
19 | var expectedAttributeName = nameof(TestEntity.ValueTypeProperty).ToLower();
20 | var expectedOperator = ConditionOperator.Equal;
21 | var expectedValue = new object[] { 42 };
22 |
23 | // Act
24 | ConditionExpression condition = new ConditionExpression(
25 | expectedEntityName,
26 | t => t.ValueTypeProperty,
27 | expectedOperator,
28 | expectedValue);
29 |
30 | // Assert
31 | Assert.AreEqual(expectedEntityName, condition.EntityName);
32 | Assert.AreEqual(expectedAttributeName, condition.AttributeName);
33 | Assert.AreEqual(expectedOperator, condition.Operator);
34 | CollectionAssert.AreEqual(expectedValue, condition.Values.ToArray());
35 | }
36 |
37 | [TestMethod()]
38 | public void Constructor_No_Entity_Name_Test()
39 | {
40 | // Setup
41 | var expectedAttributeName = nameof(TestEntity.ValueTypeProperty).ToLower();
42 | var expectedOperator = ConditionOperator.Equal;
43 | var expectedValue = new object[] { 42 };
44 |
45 | // Act
46 | ConditionExpression condition = new ConditionExpression(
47 | t => t.ValueTypeProperty,
48 | expectedOperator,
49 | expectedValue);
50 |
51 | // Assert
52 | Assert.IsNull(condition.EntityName);
53 | Assert.AreEqual(expectedAttributeName, condition.AttributeName);
54 | Assert.AreEqual(expectedOperator, condition.Operator);
55 | CollectionAssert.AreEqual(expectedValue, condition.Values.ToArray());
56 | }
57 |
58 | [TestMethod()]
59 | public void Constructor_Single_Value_Test()
60 | {
61 | // Setup
62 | var expectedAttributeName = nameof(TestEntity.ValueTypeProperty).ToLower();
63 | var expectedOperator = ConditionOperator.Equal;
64 | int expectedValue = 42;
65 |
66 | // Act
67 | ConditionExpression condition = new ConditionExpression(
68 | t => t.ValueTypeProperty,
69 | expectedOperator,
70 | expectedValue);
71 |
72 | // Assert
73 | Assert.IsNull(condition.EntityName);
74 | Assert.AreEqual(expectedAttributeName, condition.AttributeName);
75 | Assert.AreEqual(expectedOperator, condition.Operator);
76 | Assert.AreEqual(expectedValue, condition.Values[0]);
77 | }
78 |
79 | [TestMethod()]
80 | public void Constructor_Mimimal_Test()
81 | {
82 | // Setup
83 | var expectedAttributeName = nameof(TestEntity.ValueTypeProperty).ToLower();
84 | var expectedOperator = ConditionOperator.Null;
85 |
86 | // Act
87 | ConditionExpression condition = new ConditionExpression(
88 | t => t.ValueTypeProperty,
89 | expectedOperator);
90 |
91 | // Assert
92 | Assert.IsNull(condition.EntityName);
93 | Assert.AreEqual(expectedAttributeName, condition.AttributeName);
94 | Assert.AreEqual(expectedOperator, condition.Operator);
95 | Assert.AreEqual(0, condition.Values.Count);
96 | }
97 |
98 | [TestMethod()]
99 | public void Constructor_Default_Test()
100 | {
101 | //Setup
102 | ConditionExpression conditionDefault = new ConditionExpression();
103 |
104 | // Act
105 | ConditionExpression condition = new ConditionExpression();
106 |
107 | // Assert
108 | Assert.AreEqual(conditionDefault.EntityName, condition.EntityName);
109 | Assert.AreEqual(conditionDefault.AttributeName, condition.AttributeName);
110 | Assert.AreEqual(conditionDefault.Operator, condition.Operator);
111 | Assert.AreEqual(0, condition.Values.Count);
112 | }
113 |
114 | [TestMethod()]
115 | public void Null_Test()
116 | {
117 | // Setup
118 | ConditionExpression conditionT = null;
119 |
120 | // Act
121 | ConditionExpression condition = conditionT;
122 |
123 | // Assert not throw
124 | Assert.IsNull(condition);
125 | }
126 | }
127 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/D365Extensions.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net452
4 | latest
5 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
6 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
7 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages
8 | False
9 | UnitTest
10 | D365Extensions.Tests
11 | D365Extensions.Tests
12 | Copyright © 2018
13 |
14 |
15 | true
16 |
17 |
18 | FixRM.snk
19 |
20 |
21 | 1701;1702;CS0618
22 |
23 |
24 | 1701;1702;CS0618
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 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/DataCollectionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class DataCollectionExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void GetValueTest()
16 | {
17 | // Setup
18 | string expectedSring = "Artem";
19 | int? expectedInt = 36;
20 | DateTime? expectedDateTime = new DateTime(1985, 8, 8);
21 |
22 | ParameterCollection paramCollection = new ParameterCollection();
23 | paramCollection.Add("name", expectedSring);
24 | paramCollection.Add("age", expectedInt);
25 | paramCollection.Add("birthdate", expectedDateTime);
26 |
27 | // Act
28 | string actualString = paramCollection.GetValue("name");
29 | int? actualInt = paramCollection.GetValue("age");
30 | DateTime? actualDateTime = paramCollection.GetValue("birthdate");
31 |
32 | //Assert
33 | Assert.AreEqual(expectedSring, actualString);
34 | Assert.AreEqual(expectedInt, actualInt);
35 | Assert.AreEqual(expectedDateTime, actualDateTime);
36 |
37 | Assert.IsNull(paramCollection.GetValue("notexisting"));
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/EntityCollectionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions;
2 | using D365Extensions.Tests.Entities;
3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
4 | using Microsoft.Xrm.Sdk;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace D365Extensions.Tests
12 | {
13 | [TestClass()]
14 | public class EntityCollectionExtensionsTests
15 | {
16 | [TestMethod()]
17 | public void ContainsAddressTest()
18 | {
19 | // Setup
20 | var expectedEmail1 = "artem@grunin.ru";
21 | var expectedEmail2 = "doesnt@matter.com";
22 |
23 | var party1 = new ActivityParty()
24 | {
25 | AddressUsed = expectedEmail1
26 | };
27 |
28 | var party2 = new ActivityParty()
29 | {
30 | AddressUsed = expectedEmail2.ToUpper()
31 | };
32 |
33 | var collection = new EntityCollection();
34 | collection.EntityName = ActivityParty.EntityLogicalName;
35 | collection.Entities.Add(party1);
36 | collection.Entities.Add(party2);
37 |
38 | // Act + Assert
39 | Assert.IsTrue(collection.ContainsAddress(expectedEmail1));
40 | Assert.IsTrue(collection.ContainsAddress(expectedEmail2));
41 | }
42 |
43 | [TestMethod()]
44 | public void ContainsAddressShouldThrowTest()
45 | {
46 | var collection = new EntityCollection();
47 | collection.EntityName = Account.EntityLogicalName;
48 |
49 | var error = Assert.ThrowsException(()=> collection.ContainsAddress("any"));
50 | Assert.AreEqual(CheckParam.InvalidCollectionMessage, error.Message);
51 | }
52 |
53 | [TestMethod()]
54 | public void InDomainTest()
55 | {
56 | // Setup
57 | var expectedDomain = "grunin.ru";
58 |
59 | var party1 = new ActivityParty()
60 | {
61 | Id = Guid.NewGuid(),
62 | AddressUsed = $"artem@{expectedDomain}"
63 | };
64 |
65 | var party2 = new ActivityParty()
66 | {
67 | Id = Guid.NewGuid(),
68 | AddressUsed = "doesnt@matter.com"
69 | };
70 |
71 | var collection = new EntityCollection();
72 | collection.EntityName = ActivityParty.EntityLogicalName;
73 | collection.Entities.Add(party1);
74 | collection.Entities.Add(party2);
75 |
76 | // Act
77 | var inDomainParties = collection.GetPartiesInDomain(expectedDomain);
78 |
79 | // Assert
80 | Assert.AreEqual(ActivityParty.EntityLogicalName, inDomainParties.EntityName);
81 | Assert.AreEqual(1, inDomainParties.Entities.Count);
82 | Assert.AreEqual(party1.Id, inDomainParties.Entities[0].Id);
83 | }
84 |
85 | [TestMethod()]
86 | public void InDomainShouldThrowTest()
87 | {
88 | var collection = new EntityCollection();
89 | collection.EntityName = Account.EntityLogicalName;
90 |
91 | var error = Assert.ThrowsException(() => collection.GetPartiesInDomain("any"));
92 | Assert.AreEqual(CheckParam.InvalidCollectionMessage, error.Message);
93 | }
94 |
95 | [TestMethod()]
96 | public void NotInDomainTest()
97 | {
98 | // Setup
99 | var expectedDomain = "grunin.ru";
100 |
101 | var party1 = new ActivityParty()
102 | {
103 | Id = Guid.NewGuid(),
104 | AddressUsed = $"artem@{expectedDomain}"
105 | };
106 |
107 | var party2 = new ActivityParty()
108 | {
109 | Id = Guid.NewGuid(),
110 | AddressUsed = "doesnt@matter.com"
111 | };
112 |
113 | var collection = new EntityCollection();
114 | collection.EntityName = ActivityParty.EntityLogicalName;
115 | collection.Entities.Add(party1);
116 | collection.Entities.Add(party2);
117 |
118 | // Act
119 | var notInDomainParties = collection.GetPartiesNotInDomain(expectedDomain);
120 |
121 | // Assert
122 | Assert.AreEqual(ActivityParty.EntityLogicalName, notInDomainParties.EntityName);
123 | Assert.AreEqual(1, notInDomainParties.Entities.Count);
124 | Assert.AreEqual(party2.Id, notInDomainParties.Entities[0].Id);
125 | }
126 |
127 | [TestMethod()]
128 | public void NotInDomainShouldThrowTest()
129 | {
130 | var collection = new EntityCollection();
131 | collection.EntityName = Account.EntityLogicalName;
132 |
133 | var error = Assert.ThrowsException(() => collection.GetPartiesNotInDomain("any"));
134 | Assert.AreEqual(CheckParam.InvalidCollectionMessage, error.Message);
135 | }
136 | }
137 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/EntityIdComparer.cs:
--------------------------------------------------------------------------------
1 | #pragma warning disable CS0618 // Type or member is obsolete
2 |
3 | using Microsoft.Xrm.Sdk;
4 | using System.Collections;
5 | using System.Collections.Generic;
6 |
7 | namespace D365Extensions.Tests
8 | {
9 | class EntityIdComparer : IComparer, IComparer
10 | {
11 | public int Compare(Entity x, Entity y)
12 | {
13 | return x.Id.CompareTo(y.Id);
14 | }
15 |
16 | public int Compare(object x, object y)
17 | {
18 | return this.Compare(x as Entity, y as Entity);
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/EntityImageCollectionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class EntityImageCollectionExtensionsTests
14 | {
15 | [TestMethod()]
16 | public void Should_Return_Null_Test()
17 | {
18 |
19 | // Setup
20 | var images = new EntityImageCollection();
21 |
22 | // Act
23 | var nullImage = images.GetImage("PreImage");
24 |
25 | // Assert
26 | Assert.IsNull(nullImage);
27 | }
28 |
29 | [TestMethod()]
30 | public void Should_Return_Image_Test()
31 | {
32 | // Setup
33 | var image = new Entity()
34 | {
35 | Id = Guid.NewGuid(),
36 | LogicalName = Account.EntityLogicalName,
37 | [nameof(Account.Name).ToLower()] = "Test Account"
38 | };
39 |
40 | var expectedImageName = "PreImage";
41 |
42 | var images = new EntityImageCollection
43 | {
44 | { expectedImageName, image }
45 | };
46 |
47 | // Act
48 | var actualImage = images.GetImage(expectedImageName);
49 |
50 | // Assert
51 | Assert.IsNotNull(actualImage);
52 |
53 | Assert.AreEqual(image.Id, actualImage.Id);
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/EntityReferenceExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class EntityReferenceExtensionsTests
14 | {
15 | [TestMethod()]
16 | public void ToEntityTest()
17 | {
18 | // Setup
19 | var reference = new EntityReference()
20 | {
21 | Id = Guid.NewGuid(),
22 | LogicalName = "account",
23 | Name = "FixRM",
24 | KeyAttributes =
25 | {
26 | { "code", "123" }
27 | },
28 | RowVersion = "456"
29 | };
30 |
31 | // Act
32 | var entity = reference.ToEntity();
33 |
34 | // Assert
35 | Assert.AreEqual(reference.Id, entity.Id);
36 | Assert.AreEqual(reference.LogicalName, entity.LogicalName);
37 | Assert.AreEqual(reference.KeyAttributes, entity.KeyAttributes);
38 | Assert.AreEqual(reference.RowVersion, entity.RowVersion);
39 | }
40 |
41 | [TestMethod()]
42 | public void ToEntityTTest()
43 | {
44 | // Setup
45 | var reference = new EntityReference()
46 | {
47 | Id = Guid.NewGuid(),
48 | LogicalName = "account",
49 | Name = "FixRM",
50 | KeyAttributes =
51 | {
52 | { "code", "123" }
53 | },
54 | RowVersion = "456"
55 | };
56 |
57 | // Act
58 | Account entity = reference.ToEntity();
59 |
60 | // Assert
61 | Assert.AreEqual(reference.Id, entity.Id);
62 | Assert.AreEqual(reference.LogicalName, entity.LogicalName);
63 | Assert.AreEqual(reference.KeyAttributes, entity.KeyAttributes);
64 | Assert.AreEqual(reference.RowVersion, entity.RowVersion);
65 | }
66 | }
67 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/EntityReferenceTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class EntityReferenceTests
14 | {
15 | [TestMethod()]
16 | public void ConstructorTest()
17 | {
18 | // Act
19 | EntityReference reference = new EntityReference();
20 |
21 | // Assert
22 | Assert.AreEqual(Account.EntityLogicalName, reference.LogicalName);
23 | }
24 |
25 | [TestMethod()]
26 | public void Constructor2Test()
27 | {
28 | // Setup
29 | var expectedId = Guid.NewGuid();
30 |
31 | // Act
32 | EntityReference reference = new EntityReference(expectedId);
33 |
34 | // Assert
35 | Assert.AreEqual(Account.EntityLogicalName, reference.LogicalName);
36 | Assert.AreEqual(expectedId, reference.Id);
37 | }
38 |
39 | [TestMethod()]
40 | public void Constructor3Test()
41 | {
42 | // Setup
43 | var expectedKeys = new KeyAttributeCollection();
44 |
45 | // Act
46 | EntityReference reference = new EntityReference(expectedKeys);
47 |
48 | // Assert
49 | Assert.AreEqual(Account.EntityLogicalName, reference.LogicalName);
50 | Assert.AreEqual(expectedKeys, reference.KeyAttributes);
51 | }
52 |
53 | [TestMethod()]
54 | public void Constructor4Test()
55 | {
56 | // Setup
57 | var expectedKey = "accountnumber";
58 | var expectedValue = "123";
59 |
60 | // Act
61 | EntityReference reference = new EntityReference(a=> a.AccountNumber, expectedValue);
62 | var actualKey = reference.KeyAttributes.Single();
63 |
64 | // Assert
65 | Assert.AreEqual(Account.EntityLogicalName, reference.LogicalName);
66 | Assert.AreEqual(expectedKey, actualKey.Key);
67 | Assert.AreEqual(expectedValue, actualKey.Value);
68 | }
69 |
70 | [TestMethod()]
71 | public void EqualsTest()
72 | {
73 | // Setup
74 | var id = Guid.NewGuid();
75 | var rowVersion = "123";
76 | var name = "FixRM";
77 | var key = "accountnumber";
78 | var value = "42";
79 |
80 | var reference1 = new EntityReference()
81 | {
82 | Id = id,
83 | Name = name,
84 | LogicalName = Account.EntityLogicalName,
85 | RowVersion = rowVersion,
86 | KeyAttributes =
87 | {
88 | { key, value }
89 | }
90 | };
91 |
92 | var reference2 = new EntityReference()
93 | {
94 | Id = id,
95 | Name = name,
96 | RowVersion = rowVersion,
97 | KeyAttributes =
98 | {
99 | { key, value }
100 | }
101 | };
102 |
103 | // Act + Assert
104 | Assert.IsTrue(reference1.Equals((EntityReference) reference2));
105 | Assert.IsTrue(reference2.Equals(reference1));
106 | }
107 |
108 | [TestMethod()]
109 | public void GetHashCodeTest()
110 | {
111 | // Setup
112 | var id = Guid.NewGuid();
113 | var rowVersion = "123";
114 | var name = "FixRM";
115 | var key = "accountnumber";
116 | var value = "42";
117 |
118 | var reference1 = new EntityReference()
119 | {
120 | Id = id,
121 | Name = name,
122 | LogicalName = Account.EntityLogicalName,
123 | RowVersion = rowVersion,
124 | KeyAttributes =
125 | {
126 | { key, value }
127 | }
128 | };
129 |
130 | var reference2 = new EntityReference()
131 | {
132 | Id = id,
133 | Name = name,
134 | RowVersion = rowVersion,
135 | KeyAttributes =
136 | {
137 | { key, value }
138 | }
139 | };
140 |
141 | // Act
142 | var code1 = reference1.GetHashCode();
143 | var code2 = reference2.GetHashCode();
144 |
145 | // Assert
146 | Assert.AreEqual(code1, code2);
147 | }
148 | }
149 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/ExecuteMultipleProgressTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Data;
6 | using System.Data.Common;
7 | using System.Text;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class ExecuteMultipleProgressTests
13 | {
14 | [DataTestMethod]
15 | [DataRow(100.0F, 0, 1000, 10000, DisplayName = "Progress should show 100% if 0 is queried test")]
16 | [DataRow(25.0F, 100, 25, 0, DisplayName = "Progress should show 25% if 100 is queried and 25 processed test")]
17 | [DataRow(50.0F, 100, 25, 25, DisplayName = "Progress should show 50% if 100 is queried and 25 processed and 25 skipped test")]
18 | [DataRow(100.0F, 100, 100, 0, DisplayName = "Progress should show 100% if 100 is queried and 100 processed test")]
19 | [DataRow(100.0F, 100, 0, 100, DisplayName = "Progress should show 100% if 100 is queried and 100 skipped test")]
20 | public void ProgressTest(float expectedProgress, int queried, int processed, int skipped)
21 | {
22 | // Setup
23 | const uint errors = 200000000;
24 |
25 | // Act
26 | var emProgress = new ExecuteMultipleProgress((uint)queried, (uint)processed, (uint)skipped, errors);
27 |
28 | // Assert
29 | Assert.AreEqual(expectedProgress, emProgress.Progress);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/FakeExecuteMultipleExecutor.cs:
--------------------------------------------------------------------------------
1 | using FakeXrmEasy;
2 | using FakeXrmEasy.FakeMessageExecutors;
3 | using Microsoft.Xrm.Sdk;
4 | using Microsoft.Xrm.Sdk.Messages;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Runtime;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace D365Extensions.Tests
13 | {
14 | ///
15 | /// To fake OOB behavior
16 | /// https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/execute-multiple-requests
17 | ///
18 | internal class FakeExecuteMultipleExecutor : IFakeMessageExecutor
19 | {
20 | private const string NameOfFaled = "IShouldFail";
21 |
22 | private const string ResponceIdKey = "ResponseId";
23 |
24 | public static OrganizationRequest GoodRequest => new OrganizationRequest() { RequestId = Guid.NewGuid() };
25 |
26 | public static OrganizationRequest FailRequest => new OrganizationRequest() { RequestId = Guid.NewGuid(), RequestName = NameOfFaled };
27 |
28 | public List ActualRequests { get; } = new List();
29 |
30 | public List Responses { get; } = new List();
31 |
32 | public static Guid GetRequestId(ExecuteMultipleResponseItem item)
33 | {
34 | if (item.IsFaulted())
35 | {
36 | return Guid.Parse(item.Fault.Message);
37 | }
38 |
39 | return (Guid)item.Response[ResponceIdKey];
40 | }
41 |
42 | public bool CanExecute(OrganizationRequest request)
43 | {
44 | return request is ExecuteMultipleRequest;
45 | }
46 |
47 | public bool IsFailed(OrganizationRequest orgRequest)
48 | {
49 | return orgRequest.RequestName == NameOfFaled;
50 | }
51 |
52 | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
53 | {
54 | var eMultipleRequest = request as ExecuteMultipleRequest;
55 | ActualRequests.Add(eMultipleRequest);
56 |
57 | var eMultipleResponce = new ExecuteMultipleResponse()
58 | {
59 | [nameof(ExecuteMultipleResponse.Responses)] = new ExecuteMultipleResponseItemCollection()
60 | };
61 | Responses.Add(eMultipleResponce);
62 |
63 | for (var i = 0; i < eMultipleRequest.Requests.Count; i++)
64 | {
65 | var orgRequest = eMultipleRequest.Requests[i];
66 |
67 | if (!IsFailed(orgRequest))
68 | {
69 | //should return good responses
70 | if (eMultipleRequest.Settings.ReturnResponses)
71 | {
72 | //add response to results
73 | eMultipleResponce.Responses.Add(new ExecuteMultipleResponseItem
74 | {
75 | RequestIndex = i,
76 | Response = new OrganizationResponse()
77 | {
78 | //to simplify request-responce mathing in a test
79 | [ResponceIdKey] = orgRequest.RequestId
80 | }
81 | });
82 | }
83 | }
84 | else
85 | {
86 | //should always return errors
87 | eMultipleResponce.Responses.Add(new ExecuteMultipleResponseItem
88 | {
89 | RequestIndex = i,
90 | Fault = new OrganizationServiceFault()
91 | {
92 | //to simplify request-responce mathing in a test
93 | Message = orgRequest.RequestId.ToString()
94 | }
95 | });
96 |
97 | //should mark whole ExecuteMultipleResponse as faulted
98 | eMultipleResponce[nameof(ExecuteMultipleResponse.IsFaulted)] = true;
99 |
100 | // Should stop fake execution
101 | if (!eMultipleRequest.Settings.ContinueOnError)
102 | break;
103 | }
104 | }
105 |
106 | return eMultipleResponce;
107 | }
108 |
109 | public Type GetResponsibleRequestType()
110 | {
111 | return typeof(ExecuteMultipleRequest);
112 | }
113 | }
114 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/FilterExpressionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class FilterExpressionExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void AddCondition1Test()
16 | {
17 | // Setup
18 | FilterExpression filter = new FilterExpression();
19 |
20 | var expectedAttribute = nameof(TestEntity.ReferenceTypeProperty).ToLower();
21 | var expectedOperator = ConditionOperator.Equal;
22 | var expectedValue = "not used";
23 |
24 | // Act
25 | filter.AddCondition(t => t.ReferenceTypeProperty, expectedOperator, expectedValue);
26 |
27 | // Assert
28 | Assert.AreEqual(1, filter.Conditions.Count);
29 | ConditionExpression actualtCondition = filter.Conditions[0];
30 |
31 | Assert.AreEqual(expectedAttribute, actualtCondition.AttributeName);
32 | Assert.AreEqual(expectedOperator, actualtCondition.Operator);
33 | Assert.AreEqual(expectedValue, actualtCondition.Values[0]);
34 | }
35 |
36 | [TestMethod()]
37 | public void AddCondition2Test()
38 | {
39 | // Setup
40 | FilterExpression filter = new FilterExpression();
41 |
42 | var expectedEntityName = TestEntity.EntityLogicalName;
43 | var expectedAttribute = nameof(TestEntity.ReferenceTypeProperty).ToLower();
44 | var expectedOperator = ConditionOperator.Equal;
45 | var expectedValue = "not used";
46 |
47 | // Act
48 | filter.AddCondition(t => t.ReferenceTypeProperty, expectedOperator, expectedValue);
49 |
50 | // Assert
51 | Assert.AreEqual(1, filter.Conditions.Count);
52 | ConditionExpression actualtCondition = filter.Conditions[0];
53 |
54 | Assert.AreEqual(expectedAttribute, actualtCondition.AttributeName);
55 | Assert.AreEqual(expectedOperator, actualtCondition.Operator);
56 | Assert.AreEqual(expectedValue, actualtCondition.Values[0]);
57 | Assert.AreEqual(expectedEntityName, actualtCondition.EntityName);
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/IServiceProviderExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using FakeItEasy;
2 | using FakeXrmEasy;
3 | using FakeXrmEasy.Extensions;
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
5 | using Microsoft.Xrm.Sdk;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace D365Extensions.Tests
13 | {
14 | [TestClass()]
15 | public class IServiceProviderExtensionsTests
16 | {
17 | [TestMethod()]
18 | public void GetPluginExecutionContextTest()
19 | {
20 | // Setup
21 | var expectedPluginExecutionContext = A.Fake();
22 |
23 | var serviceProvider = A.Fake();
24 |
25 | var call = A.CallTo(()=> serviceProvider.GetService(A.That.IsEqualTo(typeof(IPluginExecutionContext))));
26 | call.Returns(expectedPluginExecutionContext);
27 |
28 | // Act
29 | var actualPluginExecutionContext = serviceProvider.GetPluginExecutionContext();
30 |
31 | // Assert
32 | Assert.AreEqual(expectedPluginExecutionContext, actualPluginExecutionContext);
33 |
34 | call.MustHaveHappenedOnceExactly();
35 | }
36 |
37 | [TestMethod()]
38 | public void GetOrganizationServiceFactoryTest()
39 | {
40 | // Setup
41 | var expectedOrgServiceFactory = A.Fake();
42 |
43 | var serviceProvider = A.Fake();
44 |
45 | var call = A.CallTo(() => serviceProvider.GetService(A.That.IsEqualTo(typeof(IOrganizationServiceFactory))));
46 | call.Returns(expectedOrgServiceFactory);
47 |
48 | // Act
49 | var actualOrgServiceFactory = serviceProvider.GetOrganizationServiceFactory();
50 |
51 | // Assert
52 | Assert.AreEqual(expectedOrgServiceFactory, actualOrgServiceFactory);
53 |
54 | call.MustHaveHappenedOnceExactly();
55 | }
56 |
57 | [TestMethod()]
58 | public void GetTracingServiceTest()
59 | {
60 | // Setup
61 | var expectedTracingService = A.Fake();
62 |
63 | var serviceProvider = A.Fake();
64 |
65 | var call = A.CallTo(() => serviceProvider.GetService(A.That.IsEqualTo(typeof(ITracingService))));
66 | call.Returns(expectedTracingService);
67 |
68 | // Act
69 | var actualTracingService = serviceProvider.GetTracingService();
70 |
71 | // Assert
72 | Assert.AreEqual(expectedTracingService, actualTracingService);
73 |
74 | call.MustHaveHappenedOnceExactly();
75 | }
76 |
77 | [TestMethod()]
78 | public void GetPluginExecutionTraceContextTest()
79 | {
80 | // Setup
81 | var expectedTracingService = A.Fake();
82 | var expectedPluginExecutionContext = A.Fake();
83 |
84 | var serviceProvider = A.Fake();
85 |
86 | var callGetTracingService = A.CallTo(() => serviceProvider.GetService(A.That.IsEqualTo(typeof(ITracingService))));
87 | callGetTracingService.Returns(expectedTracingService);
88 |
89 | var callGetPluginContext = A.CallTo(() => serviceProvider.GetService(A.That.IsEqualTo(typeof(IPluginExecutionContext))));
90 | callGetPluginContext.Returns(expectedPluginExecutionContext);
91 |
92 | var expectedSettings = new PluginExecutionTraceContextSettings();
93 |
94 | // Act
95 | var traceContext = serviceProvider.GetPluginExecutionTraceContext(expectedSettings);
96 |
97 | // Assert
98 | Assert.AreEqual(expectedPluginExecutionContext, traceContext.PluginExecutionContext);
99 | Assert.AreEqual(expectedTracingService, traceContext.TracingService);
100 | Assert.AreEqual(expectedSettings, traceContext.Settings);
101 |
102 | callGetTracingService.MustHaveHappenedOnceExactly();
103 | callGetPluginContext.MustHaveHappenedOnceExactly();
104 | }
105 | }
106 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/KeyAttributeCollectionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class KeyAttributeCollectionExtensionsTests
14 | {
15 | [TestMethod()]
16 | public void AddTest()
17 | {
18 | // Setup
19 | const string expectedKey = "accountnumber";
20 | const string expectedValue = "42";
21 |
22 | var keyAttributeCollection = new KeyAttributeCollection();
23 |
24 | // Act
25 | keyAttributeCollection.Add(a => a.AccountNumber, expectedValue);
26 | var actualKeyAttributes = keyAttributeCollection.SingleOrDefault();
27 |
28 | // Assert
29 | Assert.AreEqual(expectedKey, actualKeyAttributes.Key);
30 | Assert.AreEqual(expectedValue, actualKeyAttributes.Value);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/KeyAttributeCollectionTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests.Entities;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class KeyAttributeCollectionTests
14 | {
15 | [TestMethod()]
16 | public void KeyAttributeCollectionTest()
17 | {
18 | // Setup
19 | var expectedKey = "name";
20 | var expectedValue = "FixRM";
21 |
22 | var keys = new KeyAttributeCollection
23 | {
24 | { a => a.Name, expectedValue}
25 | };
26 |
27 | // Act
28 | KeyAttributeCollection actualKeys = keys;
29 | var kv = actualKeys.Single();
30 |
31 | // Assert
32 | Assert.AreEqual(expectedKey, kv.Key);
33 | Assert.AreEqual(expectedValue, kv.Value);
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/LinkEntityExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class LinkEntityExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void AddLink1Test()
16 | {
17 | // Setup
18 | string expectedFromEntityName = EntityFrom.EnityLogicalName;
19 | string expectedToEntityName = EntityTo.EnityLogicalName;
20 | string expectedFromAttributName = nameof(EntityFrom.FromId).ToLower();
21 | string expectedToAttributName = nameof(EntityTo.ToId).ToLower();
22 | JoinOperator expectedOperator = JoinOperator.LeftOuter;
23 |
24 | LinkEntity linkEntity = new LinkEntity()
25 | {
26 | LinkFromEntityName = expectedFromEntityName
27 | };
28 |
29 | // Act
30 | LinkEntity newLink = linkEntity.AddLink(
31 | EntityTo.EnityLogicalName,
32 | f=> f.FromId,
33 | t=> t.ToId,
34 | expectedOperator);
35 |
36 | // Assert
37 | Assert.AreEqual(expectedFromEntityName, newLink.LinkFromEntityName);
38 | Assert.AreEqual(expectedToEntityName, newLink.LinkToEntityName);
39 | Assert.AreEqual(expectedToAttributName, newLink.LinkToAttributeName);
40 | Assert.AreEqual(expectedFromAttributName, newLink.LinkFromAttributeName);
41 | Assert.AreEqual(expectedOperator, newLink.JoinOperator);
42 | }
43 |
44 | [TestMethod()]
45 | public void AddLink2Test()
46 | {
47 | // Setup
48 | string expectedFromEntityName = EntityFrom.EnityLogicalName;
49 | string expectedToEntityName = EntityTo.EnityLogicalName;
50 | string expectedFromAttributName = nameof(EntityFrom.FromId).ToLower();
51 | string expectedToAttributName = nameof(EntityTo.ToId).ToLower();
52 |
53 | LinkEntity linkEntity = new LinkEntity()
54 | {
55 | LinkFromEntityName = expectedFromEntityName
56 | };
57 |
58 | // Act
59 | LinkEntity newLink = linkEntity.AddLink(
60 | EntityTo.EnityLogicalName,
61 | f => f.FromId,
62 | t => t.ToId);
63 |
64 | // Assert
65 | Assert.AreEqual(expectedFromEntityName, newLink.LinkFromEntityName);
66 | Assert.AreEqual(expectedToEntityName, newLink.LinkToEntityName);
67 | Assert.AreEqual(expectedToAttributName, newLink.LinkToAttributeName);
68 | Assert.AreEqual(expectedFromAttributName, newLink.LinkFromAttributeName);
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/LinkEntityTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class LinkEntityTests
13 | {
14 | [TestMethod()]
15 | public void LinkEntityTest()
16 | {
17 | // Setup
18 | string expectedFromEntityName = EntityFrom.EnityLogicalName;
19 | string expectedToEntityName = EntityTo.EnityLogicalName;
20 | string expectedFromAttributName = nameof(EntityFrom.FromId).ToLower();
21 | string expectedToAttributName = nameof(EntityTo.ToId).ToLower();
22 | JoinOperator expectedOperator = JoinOperator.LeftOuter;
23 | string expectedAlias = "Alias";
24 | ColumnSet expectedColumnSet = new ColumnSet();
25 | FilterExpression expectedFilter = new FilterExpression();
26 | OrderExpression expectedOrder = new OrderExpression();
27 | LinkEntity expectedLink = new LinkEntity();
28 |
29 | // Act
30 | LinkEntity linkGen = new LinkEntity(
31 | f => f.FromId,
32 | t => t.ToId,
33 | expectedOperator);
34 | linkGen.EntityAlias = expectedAlias;
35 | linkGen.Columns = expectedColumnSet;
36 | linkGen.LinkCriteria = expectedFilter;
37 | linkGen.Orders.Add(expectedOrder);
38 | linkGen.LinkEntities.Add(expectedLink);
39 |
40 | // implicit cast
41 | LinkEntity linkEntity = linkGen;
42 |
43 | // Assert
44 | Assert.AreEqual(expectedFromEntityName, linkEntity.LinkFromEntityName);
45 | Assert.AreEqual(expectedToEntityName, linkEntity.LinkToEntityName);
46 | Assert.AreEqual(expectedFromAttributName, linkEntity.LinkFromAttributeName);
47 | Assert.AreEqual(expectedToAttributName, linkEntity.LinkToAttributeName);
48 | Assert.AreEqual(expectedOperator, linkEntity.JoinOperator);
49 | Assert.AreEqual(expectedAlias, linkEntity.EntityAlias);
50 | Assert.AreEqual(expectedColumnSet, linkEntity.Columns);
51 | Assert.AreEqual(expectedFilter, linkEntity.LinkCriteria);
52 | Assert.AreEqual(1, linkEntity.Orders.Count);
53 | Assert.AreEqual(expectedOrder, linkEntity.Orders[0]);
54 | Assert.AreEqual(1, linkEntity.LinkEntities.Count);
55 | Assert.AreEqual(expectedLink, linkEntity.LinkEntities[0]);
56 | }
57 |
58 | [TestMethod()]
59 | public void LinkEntity_Default_Test()
60 | {
61 | // Setup
62 | JoinOperator expectedOperator = JoinOperator.Inner;
63 |
64 | // Act
65 | LinkEntity linkEntity = new LinkEntity();
66 |
67 | // Assert
68 | Assert.AreEqual(expectedOperator, linkEntity.JoinOperator);
69 | }
70 |
71 | [TestMethod()]
72 | public void Null_Test()
73 | {
74 | // Setup
75 | LinkEntity linkEntityT = null;
76 |
77 | // Act
78 | LinkEntity linkEntity = linkEntityT;
79 |
80 | // Assert
81 | Assert.IsNull(linkEntity);
82 | }
83 | }
84 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/OptionSetValueTests.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions.Tests;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using Microsoft.Xrm.Sdk;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class OptionSetValueTests
14 | {
15 | [TestMethod()]
16 | public void OptionSetValueTest()
17 | {
18 | // Setup
19 | var expectedValue = StateCode.Active;
20 |
21 | // Act
22 | OptionSetValue optionSet = new OptionSetValue(expectedValue);
23 |
24 | // Assert
25 | Assert.AreEqual((int) expectedValue, optionSet.Value);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/OrderExpressionTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class OrderExpressionTests
13 | {
14 | [TestMethod()]
15 | public void OrderExpressionTest()
16 | {
17 | // Setup
18 | var expectedAttributeName = nameof(TestEntity.ValueTypeProperty).ToLower();
19 | var expectedOrderType = OrderType.Descending;
20 |
21 | // Act
22 | OrderExpression order = new OrderExpression(t => t.ValueTypeProperty, expectedOrderType);
23 |
24 | // Assert
25 | Assert.AreEqual(expectedAttributeName, order.AttributeName);
26 | Assert.AreEqual(expectedOrderType, order.OrderType);
27 | }
28 |
29 | [TestMethod()]
30 | public void OrderExpression_Default_Test()
31 | {
32 | //Setup
33 | var orderDefault = new OrderExpression();
34 | // Act
35 | OrderExpression order = new OrderExpression();
36 |
37 | // Assert
38 | Assert.AreEqual(orderDefault.AttributeName, order.AttributeName);
39 | Assert.AreEqual(orderDefault.OrderType, order.OrderType);
40 | }
41 |
42 | [TestMethod()]
43 | public void Null_Test()
44 | {
45 | //Setup
46 | OrderExpression orderT = null;
47 | // Act
48 | OrderExpression order = orderT;
49 |
50 | // Assert not throw
51 | Assert.IsNull(order);
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/OrganizationRequestCollectionEnumeratorTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Microsoft.Xrm.Sdk;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class OrganizationRequestCollectionEnumeratorTests
13 | {
14 | [TestMethod()]
15 | public void Should_Throw_If_Incorrect_Size_Test()
16 | {
17 | //Setup
18 | var collection = new List();
19 | var size = -10;
20 |
21 | //Act + Assert
22 | Assert.ThrowsException(()=> collection.Chunk(size));
23 | }
24 |
25 | [TestMethod()]
26 | public void Should_Retun_Zero_Length_Test()
27 | {
28 | //Setup
29 | var collection = new List();
30 | var size = 10;
31 |
32 | //Act
33 | var result = collection.Chunk(size).ToList();
34 |
35 | //Assert
36 | Assert.AreEqual(0, result.Count);
37 | }
38 |
39 | [TestMethod()]
40 | public void Should_Return_Whole_Collection_Of_Same_SizeTest()
41 | {
42 | //Setup
43 | var collection = new List()
44 | {
45 | new OrganizationRequest(),
46 | new OrganizationRequest(),
47 | new OrganizationRequest(),
48 | new OrganizationRequest(),
49 | new OrganizationRequest(),
50 | };
51 |
52 | var size = collection.Count;
53 |
54 | //Act
55 | var result = collection.Chunk(size).ToList();
56 |
57 | //Assert
58 | Assert.AreEqual(1, result.Count);
59 | CollectionAssert.AreEqual(collection, result[0]);
60 | }
61 |
62 |
63 | [TestMethod()]
64 | public void Should_Return_Whole_Collection_Test()
65 | {
66 | //Setup
67 | var collection = new List()
68 | {
69 | new OrganizationRequest(),
70 | new OrganizationRequest(),
71 | new OrganizationRequest(),
72 | new OrganizationRequest(),
73 | new OrganizationRequest(),
74 | };
75 |
76 | var size = collection.Count + 1;
77 |
78 | //Act
79 | var result = collection.Chunk(size).ToList();
80 |
81 | //Assert
82 | Assert.AreEqual(1, result.Count);
83 | CollectionAssert.AreEqual(collection, result[0]);
84 | }
85 |
86 | [TestMethod()]
87 | public void Should_Split_Collection_On_Two_Equal_Parts()
88 | {
89 | //Setup
90 | var collection = new List()
91 | {
92 | new OrganizationRequest(),
93 | new OrganizationRequest(),
94 | new OrganizationRequest(),
95 | new OrganizationRequest(),
96 | new OrganizationRequest(),
97 | new OrganizationRequest(),
98 | };
99 |
100 | var size = collection.Count / 2;
101 |
102 | //We dont use Skip+Take in real method as they iterate over collection from the beginning for each attempt
103 | List expectedChunk1 = collection.Take(size).ToList();
104 | List expectedChunk2 = collection.Skip(size).Take(size).ToList();
105 |
106 | //Act
107 | var result = collection.Chunk(size).ToList();
108 | var actualChunk1 = result[0];
109 | var actualChunk2 = result[1];
110 |
111 | //Assert
112 | Assert.AreEqual(2, result.Count);
113 | CollectionAssert.AreEqual(expectedChunk1, actualChunk1);
114 | CollectionAssert.AreEqual(expectedChunk2, actualChunk2);
115 | }
116 |
117 | [TestMethod()]
118 | public void Should_Split_Collection_On_Two_Parts()
119 | {
120 | //Setup
121 | var collection = new List()
122 | {
123 | new OrganizationRequest(),
124 | new OrganizationRequest(),
125 | new OrganizationRequest(),
126 | new OrganizationRequest(),
127 | new OrganizationRequest(),
128 | new OrganizationRequest(),
129 | new OrganizationRequest(),
130 | };
131 |
132 | var size = collection.Count / 2 + 1;
133 |
134 | //We dont use Skip+Take in real method as they iterate over collection from the beginning for each attempt
135 | List expectedChunk1 = collection.Take(size).ToList();
136 | List expectedChunk2 = collection.Skip(size).Take(size).ToList();
137 |
138 | //Act
139 | var result = collection.Chunk(size).ToList();
140 | var actualChunk1 = result[0];
141 | var actualChunk2 = result[1];
142 |
143 | //Assert
144 | Assert.AreEqual(2, result.Count);
145 | CollectionAssert.AreEqual(expectedChunk1, actualChunk1);
146 | CollectionAssert.AreEqual(expectedChunk2, actualChunk2);
147 | }
148 | }
149 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/OrganizationServiceFaultExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.ServiceModel;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace D365Extensions.Tests
11 | {
12 | [TestClass()]
13 | public class OrganizationServiceFaultExtensionsTests
14 | {
15 | [TestMethod()]
16 | public void GetKnownErrorCodeTest()
17 | {
18 | // Setup
19 | const ErrorCodes expectedErrorCode = ErrorCodes.AccessDenied;
20 |
21 | var fault = new OrganizationServiceFault();
22 | fault.ErrorCode = (int)expectedErrorCode;
23 |
24 | var exception = new FaultException(fault);
25 |
26 | // Act
27 | var actualErrorCode = exception.GetErrorCode();
28 |
29 | // Assert
30 | Assert.IsNotNull(actualErrorCode);
31 | Assert.AreEqual(expectedErrorCode, actualErrorCode);
32 | }
33 |
34 | [TestMethod()]
35 | public void GetUnknownErrorCodeTest()
36 | {
37 | // Setup
38 | var fault = new OrganizationServiceFault();
39 | fault.ErrorCode = 42;
40 |
41 | var exception = new FaultException(fault);
42 |
43 | // Act
44 | var error = exception.GetErrorCode();
45 |
46 | // Assert
47 | Assert.IsNull(error);
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using System.Reflection;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using ExecutionScope = Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope;
6 |
7 | // Setting ComVisible to false makes the types in this assembly not visible
8 | // to COM components. If you need to access a type in this assembly from
9 | // COM, set the ComVisible attribute to true on that type.
10 | [assembly: ComVisible(false)]
11 |
12 | // The following GUID is for the ID of the typelib if this project is exposed to COM
13 | [assembly: Guid("57ad9ed0-3237-44b4-bab6-d80870cc46d9")]
14 |
15 | // For running each method in separate thread
16 | // https://devblogs.microsoft.com/devops/mstest-v2-in-assembly-parallel-test-execution/
17 | [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/QueryByAttributeExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class QueryByAttributeExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void AddAttributeTest()
16 | {
17 | // Setup
18 | QueryByAttribute query = new QueryByAttribute();
19 |
20 | var expectedAttributeName = nameof(TestEntity.ReferenceTypeProperty).ToLower();
21 | var expectedAttributeValue = "not used";
22 |
23 | // Act
24 | query.AddAttribute(t => t.ReferenceTypeProperty, expectedAttributeValue);
25 |
26 | // Assert
27 | Assert.AreEqual(1, query.Attributes.Count);
28 |
29 | var actualAttribute = query.Attributes[0];
30 | Assert.AreEqual(expectedAttributeName, actualAttribute);
31 |
32 | var actualValue = query.Values[0];
33 | Assert.AreEqual(expectedAttributeValue, actualValue);
34 | }
35 |
36 | [TestMethod()]
37 | public void AddOrderTest()
38 | {
39 | // Setup
40 | QueryByAttribute query = new QueryByAttribute();
41 |
42 | var expectedAttributeName = nameof(TestEntity.ReferenceTypeProperty).ToLower();
43 | var expectedOrder = OrderType.Ascending;
44 |
45 | // Act
46 | query.AddOrder(t => t.ReferenceTypeProperty, expectedOrder);
47 |
48 | // Assert
49 | Assert.AreEqual(1, query.Orders.Count);
50 |
51 | var actualOrder = query.Orders[0];
52 | Assert.AreEqual(expectedAttributeName, actualOrder.AttributeName);
53 | Assert.AreEqual(expectedOrder, actualOrder.OrderType);
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/QueryExpressionExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Microsoft.Xrm.Sdk.Query;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | [TestClass()]
12 | public class QueryExpressionExtensionsTests
13 | {
14 | [TestMethod()]
15 | public void AddOrderTest()
16 | {
17 | // Setup
18 | QueryExpression query = new QueryExpression();
19 |
20 | var expectedAttributeName = nameof(TestEntity.ReferenceTypeProperty).ToLower();
21 | var expectedOrder = OrderType.Ascending;
22 |
23 | // Act
24 | query.AddOrder(t => t.ReferenceTypeProperty, expectedOrder);
25 |
26 | // Assert
27 | Assert.AreEqual(1, query.Orders.Count);
28 |
29 | var actualOrder = query.Orders[0];
30 | Assert.AreEqual(expectedAttributeName, actualOrder.AttributeName);
31 | Assert.AreEqual(expectedOrder, actualOrder.OrderType);
32 | }
33 |
34 | [TestMethod()]
35 | public void AddLink1Test()
36 | {
37 | // Setup
38 | QueryExpression query = new QueryExpression();
39 |
40 | string expectedToEntityName = EntityTo.EnityLogicalName;
41 | string expectedFromAttributName = nameof(EntityFrom.FromId).ToLower();
42 | string expectedToAttributName = nameof(EntityTo.ToId).ToLower();
43 |
44 | // Act
45 | LinkEntity actualLink = query.AddLink(f => f.FromId, t => t.ToId);
46 |
47 | // Assert
48 | Assert.AreEqual(1, query.LinkEntities.Count);
49 | Assert.AreEqual(query.LinkEntities[0], actualLink);
50 | Assert.AreEqual(expectedToEntityName, actualLink.LinkToEntityName);
51 | Assert.AreEqual(expectedFromAttributName, actualLink.LinkFromAttributeName);
52 | Assert.AreEqual(expectedToAttributName, actualLink.LinkToAttributeName);
53 | }
54 |
55 | [TestMethod()]
56 | public void AddLink2Test()
57 | {
58 | // Setup
59 | QueryExpression query = new QueryExpression();
60 |
61 | string expectedToEntityName = EntityTo.EnityLogicalName;
62 | string expectedFromAttributName = nameof(EntityFrom.FromId).ToLower();
63 | string expectedToAttributName = nameof(EntityTo.ToId).ToLower();
64 | JoinOperator expectedOperator = JoinOperator.LeftOuter;
65 |
66 | // Act
67 | LinkEntity actualLink = query.AddLink(f => f.FromId, t => t.ToId, expectedOperator);
68 |
69 | // Assert
70 | Assert.AreEqual(1, query.LinkEntities.Count);
71 | Assert.AreEqual(query.LinkEntities[0], actualLink);
72 | Assert.AreEqual(expectedToEntityName, actualLink.LinkToEntityName);
73 | Assert.AreEqual(expectedFromAttributName, actualLink.LinkFromAttributeName);
74 | Assert.AreEqual(expectedToAttributName, actualLink.LinkToAttributeName);
75 | Assert.AreEqual(expectedOperator, actualLink.JoinOperator);
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/TestEntities.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Microsoft.Xrm.Sdk;
7 | using Microsoft.Xrm.Sdk.Client;
8 |
9 | namespace D365Extensions.Tests
10 | {
11 | ///
12 | /// Simple Entity inheritor for Test purpose
13 | ///
14 | [EntityLogicalName("testentity")]
15 | public class TestEntity : Entity
16 | {
17 | public const string EntityLogicalName = "testentity";
18 |
19 | public TestEntity() : base(EntityLogicalName)
20 | {
21 | }
22 |
23 | ///
24 | /// Reference type property
25 | ///
26 | [AttributeLogicalName("referencetypeproperty")]
27 | public string ReferenceTypeProperty
28 | {
29 | get
30 | {
31 | return this.GetAttributeValue("referencetypeproperty");
32 | }
33 | set
34 | {
35 | this.SetAttributeValue("referencetypeproperty", value);
36 | }
37 | }
38 |
39 | ///
40 | /// Value type property
41 | ///
42 | [AttributeLogicalName("valuetypeproperty")]
43 | public System.Nullable ValueTypeProperty
44 | {
45 | get
46 | {
47 | return this.GetAttributeValue>("valuetypeproperty");
48 | }
49 | set
50 | {
51 | this.SetAttributeValue("valuetypeproperty", value);
52 | }
53 | }
54 | }
55 |
56 | ///
57 | /// Simple Entity for LinkEntity tests
58 | ///
59 | [EntityLogicalName("entityfrom")]
60 | public class EntityFrom : Entity
61 | {
62 | public static string EnityLogicalName = "entityfrom";
63 |
64 | public EntityFrom() : base(EnityLogicalName)
65 | {
66 | }
67 |
68 | [AttributeLogicalName("fromid")]
69 | public Guid FromId { get; set; }
70 | }
71 |
72 | ///
73 | /// Simple Entity for LinkEntity tests
74 | ///
75 | [EntityLogicalName("entityto")]
76 | public class EntityTo : Entity
77 | {
78 | public static string EnityLogicalName = "entityto";
79 |
80 | public EntityTo() : base(EnityLogicalName)
81 | {
82 | }
83 |
84 | [AttributeLogicalName("toid")]
85 | public Guid ToId { get; set; }
86 | }
87 |
88 | ///
89 | /// Entity for PropertyExpression tests
90 | ///
91 | [EntityLogicalName("custom_entity")]
92 | public class CustomEntity : Entity
93 | {
94 | public static string EnityLogicalName = "custom_entity";
95 |
96 | public CustomEntity() : base(EnityLogicalName)
97 | {
98 | }
99 |
100 | [AttributeLogicalName("string_prop")]
101 | public string StringProp {
102 | get => GetAttributeValue("string_prop");
103 | set => SetAttributeValue("string_prop", value);
104 | }
105 | }
106 |
107 | ///
108 | /// Entity for PropertyExpression tests
109 | ///
110 | [EntityLogicalName("custom_entity2")]
111 | public class CustomEntity2 : Entity
112 | {
113 | public static string EnityLogicalName = "custom_entity2";
114 |
115 | public CustomEntity2() : base(EnityLogicalName)
116 | {
117 | }
118 |
119 | [AttributeLogicalName("string_prop2")]
120 | public string StringProp
121 | {
122 | get => GetAttributeValue("string_prop2");
123 | set => SetAttributeValue("string_prop2", value);
124 | }
125 | }
126 |
127 | ///
128 | /// Entity for PropertyExpression tests
129 | ///
130 | public class NotDecoratedEntity : Entity
131 | {
132 | public static string EnityLogicalName = "notdecoratedentity";
133 |
134 | public NotDecoratedEntity() : base(EnityLogicalName)
135 | {
136 | }
137 |
138 | public string TheProp
139 | {
140 | get => GetAttributeValue("theprop");
141 | set => SetAttributeValue("theprop", value);
142 | }
143 | }
144 |
145 | public enum StateCode
146 | {
147 | Active = 0,
148 | Inactive = 1
149 | }
150 | }
151 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.Tests/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31702.278
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D365Extensions", "D365Extensions\D365Extensions.csproj", "{0FD54081-45D9-41C1-8732-13034BDBB8E6}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D365Extensions.Tests", "D365Extensions.Tests\D365Extensions.Tests.csproj", "{57AD9ED0-3237-44B4-BAB6-D80870CC46D9}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D365Extensions.Benchmark", "D365Extensions.Benchmark\D365Extensions.Benchmark.csproj", "{1ECA11B0-DE44-4C41-AC11-85B7C4AF0BFA}"
11 | EndProject
12 | Global
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
14 | Debug|Any CPU = Debug|Any CPU
15 | Release|Any CPU = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {0FD54081-45D9-41C1-8732-13034BDBB8E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {0FD54081-45D9-41C1-8732-13034BDBB8E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {0FD54081-45D9-41C1-8732-13034BDBB8E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {0FD54081-45D9-41C1-8732-13034BDBB8E6}.Release|Any CPU.Build.0 = Release|Any CPU
22 | {57AD9ED0-3237-44B4-BAB6-D80870CC46D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {57AD9ED0-3237-44B4-BAB6-D80870CC46D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {57AD9ED0-3237-44B4-BAB6-D80870CC46D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {57AD9ED0-3237-44B4-BAB6-D80870CC46D9}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {1ECA11B0-DE44-4C41-AC11-85B7C4AF0BFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {1ECA11B0-DE44-4C41-AC11-85B7C4AF0BFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {1ECA11B0-DE44-4C41-AC11-85B7C4AF0BFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {1ECA11B0-DE44-4C41-AC11-85B7C4AF0BFA}.Release|Any CPU.Build.0 = Release|Any CPU
30 | EndGlobalSection
31 | GlobalSection(SolutionProperties) = preSolution
32 | HideSolutionNode = FALSE
33 | EndGlobalSection
34 | GlobalSection(ExtensibilityGlobals) = postSolution
35 | SolutionGuid = {CB4BDB0A-199A-4423-923D-9E9219CFA7C3}
36 | EndGlobalSection
37 | EndGlobal
38 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/AliasedValue.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions;
2 | using System;
3 | using System.Linq.Expressions;
4 |
5 | namespace Microsoft.Xrm.Sdk
6 | {
7 | ///
8 | /// Generic version if AliasedValue class
9 | ///
10 | ///
11 | public class AliasedValue where T : Entity
12 | {
13 | public Expression> AttributeName { get; }
14 |
15 | public object Value { get; }
16 |
17 | public AliasedValue(Expression> attributeName, object value)
18 | {
19 | AttributeName = attributeName;
20 | Value = value;
21 | }
22 |
23 | public static implicit operator AliasedValue(AliasedValue t)
24 | {
25 | if (t is null) return null;
26 |
27 | return new AliasedValue(
28 | entityLogicalName: LogicalName.GetName(),
29 | attributeLogicalName: LogicalName.GetName(t.AttributeName),
30 | value: t.Value);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/AliasedValueExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Text;
4 | using System.Threading.Tasks;
5 |
6 | namespace Microsoft.Xrm.Sdk
7 | {
8 | public static class AliasedValueExtensions
9 | {
10 | ///
11 | /// Checks if AliasedValue contains primary key attribute of linked entity
12 | ///
13 | /// Id of linked entity
14 | public static bool IsPrimaryKey(this AliasedValue aliasedValue)
15 | {
16 | return aliasedValue.AttributeLogicalName == "activityid" ||
17 | aliasedValue.AttributeLogicalName.ConsistOf(aliasedValue.EntityLogicalName, "id");
18 | }
19 |
20 | ///
21 | /// Gets the value of AliasedValue
22 | ///
23 | public static T GetValue(this AliasedValue aliasedValue)
24 | {
25 | return (T) aliasedValue.Value;
26 | }
27 |
28 | // to avoid string allocations
29 | private static bool ConsistOf(this string value, string part1, string part2)
30 | {
31 | if (value.Length != part1.Length + part2.Length)
32 | return false;
33 |
34 | return value.StartsWith(part1) && value.EndsWith(part2);
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/CodeActivityContextExtensions.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Xrm.Sdk;
2 | using Microsoft.Xrm.Sdk.Workflow;
3 | using System;
4 | using System.Activities;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace System.Activities
11 | {
12 | public static class CodeActivityContextExtensions
13 | {
14 | ///
15 | /// Gets IWorkflowContext extension from CodeActivityContext
16 | ///
17 | ///
18 | public static IWorkflowContext GetWorkflowContext(this CodeActivityContext codeActivityContext)
19 | {
20 | return codeActivityContext.GetExtension();
21 | }
22 |
23 | ///
24 | /// Gets IOrganizationServiceFactory extension from CodeActivityContext
25 | ///
26 | ///
27 | ///
28 | public static IOrganizationServiceFactory GetOrganizationServiceFactory(this CodeActivityContext codeActivityContext)
29 | {
30 |
31 | return codeActivityContext.GetExtension();
32 | }
33 |
34 | ///
35 | /// Gets ITracingService extension from CodeActivityContext
36 | ///
37 | ///
38 | ///
39 | public static ITracingService GetTracingService(this CodeActivityContext codeActivityContext)
40 | {
41 | return codeActivityContext.GetExtension();
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/ColumnSet.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq.Expressions;
4 | using D365Extensions;
5 |
6 | namespace Microsoft.Xrm.Sdk.Query
7 | {
8 | ///
9 | /// Strongly typed version of the ColumnSet class
10 | ///
11 | public sealed class ColumnSet where T : Entity
12 | {
13 | ///
14 | /// Gets the collection of lambdas containing the names of the attributes to be retrieved
15 | /// from a query.
16 | ///
17 | public List>> Columns { get; } = new List>>();
18 |
19 | ///
20 | /// Initializes a new instance of the ColumnSet class.
21 | ///
22 | public ColumnSet()
23 | {
24 | }
25 |
26 | ///
27 | /// Initializes a new instance of the ColumnSet class setting the Columns property.
28 | ///
29 | /// Specifies an array of property expressions containing the names of the attributes.
30 | ///
31 | public ColumnSet(params Expression>[] columns)
32 | {
33 | Columns.AddRange(columns);
34 | }
35 |
36 | ///
37 | /// Adds the specified attribute to the column set
38 | ///
39 | /// Specifies a property expressions containing the name of the attribute.
40 | public void AddColumn(Expression> column)
41 | {
42 | Columns.Add(column);
43 | }
44 |
45 | ///
46 | /// Adds the specified attributes to the column set.
47 | ///
48 | /// Specifies an array of property expressions containing the names of the attributes.
49 | public void AddColumns(params Expression>[] columns)
50 | {
51 | Columns.AddRange(columns);
52 | }
53 |
54 | ///
55 | /// Converts ColumnSet to ColumnSet
56 | ///
57 | public static implicit operator ColumnSet(ColumnSet t)
58 | {
59 | if (t == null) return null;
60 |
61 | return new ColumnSet(LogicalName.GetNames(t.Columns.ToArray()));
62 | }
63 | }
64 | }
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/ColumnSetExtensions.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions;
2 | using System;
3 | using System.Linq.Expressions;
4 |
5 | namespace Microsoft.Xrm.Sdk.Query
6 | {
7 | public static class ColumnSetExtensions
8 | {
9 | ///
10 | /// Adds the specified attribute to the column set.
11 | ///
12 | /// Type of the entity to add column from
13 | /// The property expression containing the name of the attribute to add
14 | public static void AddColumn(this ColumnSet columnSet, Expression> column) where T: Entity
15 | {
16 | columnSet.AddColumn(LogicalName.GetName(column));
17 | }
18 |
19 | ///
20 | /// Adds the specified attributes to the column set.
21 | ///
22 | /// Type of the entity to add column from
23 | /// The property expressions containing the name of the attribute to add
24 | public static void AddColumns(this ColumnSet columnSet, params Expression>[] columns) where T : Entity
25 | {
26 | columnSet.AddColumns(LogicalName.GetNames(columns));
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/D365Extensions/D365Extensions/ConditionExpression.cs:
--------------------------------------------------------------------------------
1 | using D365Extensions;
2 | using System;
3 | using System.Collections;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Linq.Expressions;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Microsoft.Xrm.Sdk.Query
11 | {
12 | ///
13 | /// Strongly typed version of the ConditionExpression class
14 | ///
15 | public class ConditionExpression where T : Entity
16 | {
17 | ///
18 | /// Initializes a new instance of ConditionExpression class.
19 | ///
20 | public ConditionExpression()
21 | {
22 | }
23 |
24 | ///
25 | /// Initializes a new instance of the ConditionExpression class.
26 | ///
27 | /// The logical name of a table, or a LinkEntity.EntityAlias value for a table, that contains the column to evaluate for the condition
28 | /// The logical name of the attribute in the condition expression.
29 | /// The condition operator.
30 | /// The array of attribute values.
31 | public ConditionExpression(string entityName, Expression> attributeName, ConditionOperator conditionOperator, params object[] values)
32 | {
33 | EntityName = entityName;
34 | AttributeName = attributeName;
35 | Operator = conditionOperator;
36 | if (values != null)
37 | {
38 | Values = new List