├── .editorconfig ├── .gitignore ├── LICENSE ├── NimblePros.ValueObjects.sln ├── README.md ├── src └── NimblePros.ValueObjects │ ├── Address.cs │ ├── DateTimeOffsetRange.cs │ ├── DateTimeRange.cs │ ├── NimblePros.ValueObjects.csproj │ └── ValueObject.cs └── tests └── NimblePros.ValueObjects.UnitTests ├── AddressTests └── AddressConstructor.cs └── NimblePros.ValueObjects.UnitTests.csproj /.editorconfig: -------------------------------------------------------------------------------- 1 | # To learn more about .editorconfig see https://aka.ms/editorconfigdocs 2 | ############################### 3 | # Core EditorConfig Options # 4 | ############################### 5 | # All files 6 | [*] 7 | indent_style = space 8 | # Code files 9 | [*.{cs,csx,vb,vbx}] 10 | indent_size =2 11 | insert_final_newline = true 12 | charset = utf-8-bom 13 | ############################### 14 | # .NET Coding Conventions # 15 | ############################### 16 | [*.{cs,vb}] 17 | # Organize usings 18 | dotnet_sort_system_directives_first = true 19 | # this. preferences 20 | dotnet_style_qualification_for_field = false:silent 21 | dotnet_style_qualification_for_property = false:silent 22 | dotnet_style_qualification_for_method = false:silent 23 | dotnet_style_qualification_for_event = false:silent 24 | # Language keywords vs BCL types preferences 25 | dotnet_style_predefined_type_for_locals_parameters_members = true:silent 26 | dotnet_style_predefined_type_for_member_access = true:silent 27 | # Parentheses preferences 28 | dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent 29 | dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent 30 | dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent 31 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent 32 | # Modifier preferences 33 | dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent 34 | dotnet_style_readonly_field = true:suggestion 35 | # Expression-level preferences 36 | dotnet_style_object_initializer = true:suggestion 37 | dotnet_style_collection_initializer = true:suggestion 38 | dotnet_style_explicit_tuple_names = true:suggestion 39 | dotnet_style_null_propagation = true:suggestion 40 | dotnet_style_coalesce_expression = true:suggestion 41 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent 42 | dotnet_style_prefer_inferred_tuple_names = true:suggestion 43 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion 44 | dotnet_style_prefer_auto_properties = true:silent 45 | dotnet_style_prefer_conditional_expression_over_assignment = true:silent 46 | dotnet_style_prefer_conditional_expression_over_return = true:silent 47 | # Namespace preferences 48 | csharp_style_namespace_declarations = file_scoped:warning 49 | ############################### 50 | # Naming Conventions # 51 | ############################### 52 | # Style Definitions 53 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case 54 | # Use PascalCase for constant fields 55 | dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion 56 | dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields 57 | dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style 58 | dotnet_naming_symbols.constant_fields.applicable_kinds = field 59 | dotnet_naming_symbols.constant_fields.applicable_accessibilities = * 60 | dotnet_naming_symbols.constant_fields.required_modifiers = const 61 | tab_width=2 62 | ############################### 63 | # C# Coding Conventions # 64 | ############################### 65 | [*.cs] 66 | # var preferences 67 | csharp_style_var_for_built_in_types = true:silent 68 | csharp_style_var_when_type_is_apparent = true:silent 69 | csharp_style_var_elsewhere = true:silent 70 | # Expression-bodied members 71 | csharp_style_expression_bodied_methods = false:silent 72 | csharp_style_expression_bodied_constructors = false:silent 73 | csharp_style_expression_bodied_operators = false:silent 74 | csharp_style_expression_bodied_properties = true:silent 75 | csharp_style_expression_bodied_indexers = true:silent 76 | csharp_style_expression_bodied_accessors = true:silent 77 | # Pattern matching preferences 78 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion 79 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion 80 | # Null-checking preferences 81 | csharp_style_throw_expression = true:suggestion 82 | csharp_style_conditional_delegate_call = true:suggestion 83 | # Modifier preferences 84 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion 85 | # Expression-level preferences 86 | csharp_prefer_braces = true:silent 87 | csharp_style_deconstructed_variable_declaration = true:suggestion 88 | csharp_prefer_simple_default_expression = true:suggestion 89 | csharp_style_pattern_local_over_anonymous_function = true:suggestion 90 | csharp_style_inlined_variable_declaration = true:suggestion 91 | 92 | ############################### 93 | # C# Formatting Rules # 94 | ############################### 95 | # New line preferences 96 | csharp_new_line_before_open_brace = all 97 | csharp_new_line_before_else = true 98 | csharp_new_line_before_catch = true 99 | csharp_new_line_before_finally = true 100 | csharp_new_line_before_members_in_object_initializers = true 101 | csharp_new_line_before_members_in_anonymous_types = true 102 | csharp_new_line_between_query_expression_clauses = true 103 | # Indentation preferences 104 | csharp_indent_case_contents = true 105 | csharp_indent_switch_labels = true 106 | csharp_indent_labels = flush_left 107 | # Space preferences 108 | csharp_space_after_cast = false 109 | csharp_space_after_keywords_in_control_flow_statements = true 110 | csharp_space_between_method_call_parameter_list_parentheses = false 111 | csharp_space_between_method_declaration_parameter_list_parentheses = false 112 | csharp_space_between_parentheses = false 113 | csharp_space_before_colon_in_inheritance_clause = true 114 | csharp_space_after_colon_in_inheritance_clause = true 115 | csharp_space_around_binary_operators = before_and_after 116 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false 117 | csharp_space_between_method_call_name_and_opening_parenthesis = false 118 | csharp_space_between_method_call_empty_parameter_list_parentheses = false 119 | # Wrapping preferences 120 | csharp_preserve_single_line_statements = true 121 | csharp_preserve_single_line_blocks = true 122 | ############################### 123 | # VB Coding Conventions # 124 | ############################### 125 | [*.vb] 126 | # Modifier preferences 127 | visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion 128 | -------------------------------------------------------------------------------- /.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 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 NimblePros 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NimblePros.ValueObjects.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{50CABF24-3946-4FCC-9C17-48125210EA69}" 7 | ProjectSection(SolutionItems) = preProject 8 | .editorconfig = .editorconfig 9 | README.md = README.md 10 | EndProjectSection 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{78F7C90D-0DB1-4C3A-A887-8B9034869DD9}" 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8CD07A9F-0787-439C-B444-C281A3967AC5}" 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NimblePros.ValueObjects.UnitTests", "tests\NimblePros.ValueObjects.UnitTests\NimblePros.ValueObjects.UnitTests.csproj", "{A2E11268-1D51-4BD5-972B-F9A45B275452}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NimblePros.ValueObjects", "src\NimblePros.ValueObjects\NimblePros.ValueObjects.csproj", "{FFF1AB2B-FAFB-4C97-B828-E032492C8605}" 19 | EndProject 20 | Global 21 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 22 | Debug|Any CPU = Debug|Any CPU 23 | Release|Any CPU = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 26 | {A2E11268-1D51-4BD5-972B-F9A45B275452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {A2E11268-1D51-4BD5-972B-F9A45B275452}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {A2E11268-1D51-4BD5-972B-F9A45B275452}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {A2E11268-1D51-4BD5-972B-F9A45B275452}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {FFF1AB2B-FAFB-4C97-B828-E032492C8605}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {FFF1AB2B-FAFB-4C97-B828-E032492C8605}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {FFF1AB2B-FAFB-4C97-B828-E032492C8605}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {FFF1AB2B-FAFB-4C97-B828-E032492C8605}.Release|Any CPU.Build.0 = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(NestedProjects) = preSolution 39 | {A2E11268-1D51-4BD5-972B-F9A45B275452} = {8CD07A9F-0787-439C-B444-C281A3967AC5} 40 | {FFF1AB2B-FAFB-4C97-B828-E032492C8605} = {78F7C90D-0DB1-4C3A-A887-8B9034869DD9} 41 | EndGlobalSection 42 | GlobalSection(ExtensibilityGlobals) = postSolution 43 | SolutionGuid = {CBB90C59-4C25-40AF-B6C6-3A5A11B0109C} 44 | EndGlobalSection 45 | EndGlobal 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Value Objects 2 | 3 | A collection of Value Object samples for use in DDD projects. 4 | 5 | [Brought to you by NimblePros](https://nimblepros.com). 6 | 7 | ## Included Objects 8 | 9 | - DateTimeOffsetRange. Describes anything with a start and end DateTimeOffset. 10 | - DateTimeRange. Describes anything with a start and end DateTime. -------------------------------------------------------------------------------- /src/NimblePros.ValueObjects/Address.cs: -------------------------------------------------------------------------------- 1 | using Ardalis.GuardClauses; 2 | 3 | namespace NimblePros.ValueObjects; 4 | 5 | public class Address : ValueObject 6 | { 7 | public String Street { get; private set; } 8 | public String City { get; private set; } 9 | public String State { get; private set; } 10 | public String Country { get; private set; } 11 | public String PostalCode { get; private set; } 12 | 13 | public Address() { } 14 | 15 | public Address(string street, 16 | string city, 17 | string state, 18 | string country, 19 | string postalCode) 20 | { 21 | Street = Guard.Against.NullOrEmpty(street, nameof(street)); 22 | City = Guard.Against.NullOrEmpty(city, nameof(city)); 23 | State = Guard.Against.NullOrEmpty(state, nameof(state)); 24 | Country = Guard.Against.NullOrEmpty(country, nameof(country)); 25 | PostalCode = Guard.Against.NullOrEmpty(postalCode, nameof(postalCode)); 26 | } 27 | 28 | protected override IEnumerable GetEqualityComponents() 29 | { 30 | // Using a yield return statement to return each element one at a time 31 | yield return Street; 32 | yield return City; 33 | yield return State; 34 | yield return Country; 35 | yield return PostalCode; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/NimblePros.ValueObjects/DateTimeOffsetRange.cs: -------------------------------------------------------------------------------- 1 | // Source: https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions/ValueObject/ValueObject.cs 2 | using Ardalis.GuardClauses; 3 | 4 | namespace NimblePros.ValueObjects; 5 | 6 | public class DateTimeOffsetRange : ValueObject 7 | { 8 | public DateTimeOffset Start { get; private set; } 9 | public DateTimeOffset End { get; private set; } 10 | 11 | public DateTimeOffsetRange(DateTimeOffset start, DateTimeOffset end) 12 | { 13 | // Ardalis.GuardClauses supports extensions with custom guards per project 14 | Guard.Against.OutOfRange(start, nameof(start), start, end); 15 | Start = start; 16 | End = end; 17 | } 18 | 19 | public DateTimeOffsetRange(DateTimeOffset start, TimeSpan duration) : this(start, start.Add(duration)) 20 | { 21 | } 22 | 23 | public int DurationInMinutes() 24 | { 25 | return (int)Math.Round((End - Start).TotalMinutes, 0); 26 | } 27 | 28 | public DateTimeOffsetRange NewDuration(TimeSpan newDuration) 29 | { 30 | return new DateTimeOffsetRange(this.Start, newDuration); 31 | } 32 | 33 | public DateTimeOffsetRange NewEnd(DateTimeOffset newEnd) 34 | { 35 | return new DateTimeOffsetRange(this.Start, newEnd); 36 | } 37 | 38 | public DateTimeOffsetRange NewStart(DateTimeOffset newStart) 39 | { 40 | return new DateTimeOffsetRange(newStart, this.End); 41 | } 42 | 43 | public static DateTimeOffsetRange CreateOneDayRange(DateTimeOffset day) 44 | { 45 | return new DateTimeOffsetRange(day, day.AddDays(1)); 46 | } 47 | 48 | public static DateTimeOffsetRange CreateOneWeekRange(DateTimeOffset startDay) 49 | { 50 | return new DateTimeOffsetRange(startDay, startDay.AddDays(7)); 51 | } 52 | 53 | public bool Overlaps(DateTimeOffsetRange dateTimeRange) 54 | { 55 | return this.Start < dateTimeRange.End && 56 | this.End > dateTimeRange.Start; 57 | } 58 | 59 | protected override IEnumerable GetEqualityComponents() 60 | { 61 | yield return Start; 62 | yield return End; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/NimblePros.ValueObjects/DateTimeRange.cs: -------------------------------------------------------------------------------- 1 | using Ardalis.GuardClauses; 2 | 3 | namespace NimblePros.ValueObjects; 4 | public class DateTimeRange : ValueObject 5 | { 6 | public DateTime Start { get; private set; } 7 | public DateTime End { get; private set; } 8 | 9 | public DateTimeRange(DateTime start, DateTime end) 10 | { 11 | // Ardalis.GuardClauses supports extensions with custom guards per project 12 | Guard.Against.OutOfRange(start, nameof(start), start, end); 13 | Start = start; 14 | End = end; 15 | } 16 | 17 | public DateTimeRange(DateTime start, TimeSpan duration) : this(start, start.Add(duration)) 18 | { 19 | } 20 | 21 | public int DurationInMinutes() 22 | { 23 | return (int)Math.Round((End - Start).TotalMinutes, 0); 24 | } 25 | 26 | public DateTimeRange NewDuration(TimeSpan newDuration) 27 | { 28 | return new DateTimeRange(this.Start, newDuration); 29 | } 30 | 31 | public DateTimeRange NewEnd(DateTime newEnd) 32 | { 33 | return new DateTimeRange(this.Start, newEnd); 34 | } 35 | 36 | public DateTimeRange NewStart(DateTime newStart) 37 | { 38 | return new DateTimeRange(newStart, this.End); 39 | } 40 | 41 | public static DateTimeRange CreateOneDayRange(DateTime day) 42 | { 43 | return new DateTimeRange(day, day.AddDays(1)); 44 | } 45 | 46 | public static DateTimeRange CreateOneWeekRange(DateTime startDay) 47 | { 48 | return new DateTimeRange(startDay, startDay.AddDays(7)); 49 | } 50 | 51 | public bool Overlaps(DateTimeRange dateTimeRange) 52 | { 53 | return this.Start < dateTimeRange.End && 54 | this.End > dateTimeRange.Start; 55 | } 56 | 57 | protected override IEnumerable GetEqualityComponents() 58 | { 59 | yield return Start; 60 | yield return End; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/NimblePros.ValueObjects/NimblePros.ValueObjects.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/NimblePros.ValueObjects/ValueObject.cs: -------------------------------------------------------------------------------- 1 | // Source: https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions/ValueObject/ValueObject.cs 2 | namespace NimblePros.ValueObjects; 3 | 4 | [Serializable] 5 | public abstract class ValueObject : IComparable, IComparable 6 | { 7 | private int? _cachedHashCode; 8 | 9 | protected abstract IEnumerable GetEqualityComponents(); 10 | 11 | public override bool Equals(object obj) 12 | { 13 | if (obj == null) 14 | return false; 15 | 16 | if (GetUnproxiedType(this) != GetUnproxiedType(obj)) 17 | return false; 18 | 19 | var valueObject = (ValueObject)obj; 20 | 21 | return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents()); 22 | } 23 | 24 | public override int GetHashCode() 25 | { 26 | if (!_cachedHashCode.HasValue) 27 | { 28 | _cachedHashCode = GetEqualityComponents() 29 | .Aggregate(1, (current, obj) => 30 | { 31 | unchecked 32 | { 33 | return current * 23 + (obj?.GetHashCode() ?? 0); 34 | } 35 | }); 36 | } 37 | 38 | return _cachedHashCode.Value; 39 | } 40 | 41 | public virtual int CompareTo(object obj) 42 | { 43 | Type thisType = GetUnproxiedType(this); 44 | Type otherType = GetUnproxiedType(obj); 45 | 46 | if (thisType != otherType) 47 | return string.Compare(thisType.ToString(), otherType.ToString(), StringComparison.Ordinal); 48 | 49 | var other = (ValueObject)obj; 50 | 51 | object[] components = GetEqualityComponents().ToArray(); 52 | object[] otherComponents = other.GetEqualityComponents().ToArray(); 53 | 54 | for (int i = 0; i < components.Length; i++) 55 | { 56 | int comparison = CompareComponents(components[i], otherComponents[i]); 57 | if (comparison != 0) 58 | return comparison; 59 | } 60 | 61 | return 0; 62 | } 63 | 64 | private int CompareComponents(object object1, object object2) 65 | { 66 | if (object1 is null && object2 is null) 67 | return 0; 68 | 69 | if (object1 is null) 70 | return -1; 71 | 72 | if (object2 is null) 73 | return 1; 74 | 75 | if (object1 is IComparable comparable1 && object2 is IComparable comparable2) 76 | return comparable1.CompareTo(comparable2); 77 | 78 | return object1.Equals(object2) ? 0 : -1; 79 | } 80 | 81 | public virtual int CompareTo(ValueObject other) 82 | { 83 | return CompareTo(other as object); 84 | } 85 | 86 | public static bool operator ==(ValueObject a, ValueObject b) 87 | { 88 | if (a is null && b is null) 89 | return true; 90 | 91 | if (a is null || b is null) 92 | return false; 93 | 94 | return a.Equals(b); 95 | } 96 | 97 | public static bool operator !=(ValueObject a, ValueObject b) 98 | { 99 | return !(a == b); 100 | } 101 | 102 | internal static Type GetUnproxiedType(object obj) 103 | { 104 | const string EFCoreProxyPrefix = "Castle.Proxies."; 105 | const string NHibernateProxyPostfix = "Proxy"; 106 | 107 | Type type = obj.GetType(); 108 | string typeString = type.ToString(); 109 | 110 | if (typeString.Contains(EFCoreProxyPrefix) || typeString.EndsWith(NHibernateProxyPostfix)) 111 | return type.BaseType; 112 | 113 | return type; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /tests/NimblePros.ValueObjects.UnitTests/AddressTests/AddressConstructor.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions.Execution; 2 | using Xunit; 3 | 4 | namespace NimblePros.ValueObjects.UnitTests.AddressTests; 5 | 6 | public class AddressConstructor 7 | { 8 | private string _validCity = "city"; 9 | private string _validState = "state"; 10 | private string _validCountry = "country"; 11 | private string _validStreet = "street"; 12 | private string _validPostalCode = "postalCode"; 13 | 14 | [Fact] 15 | public void SetsProperties() 16 | { 17 | var address = new Address(_validStreet, _validCity, _validState, _validCountry, _validPostalCode); 18 | 19 | using(new AssertionScope()) 20 | { 21 | Assert.Equal(_validStreet, address.Street); 22 | Assert.Equal(_validCity, address.City); 23 | Assert.Equal(_validState, address.State); 24 | Assert.Equal(_validCountry, address.Country); 25 | Assert.Equal(_validPostalCode, address.PostalCode); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/NimblePros.ValueObjects.UnitTests/NimblePros.ValueObjects.UnitTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | all 17 | 18 | 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | all 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | --------------------------------------------------------------------------------