Skip to content

Commit df4deb9

Browse files
committed
Converted Tag to ValueObject
1 parent 1ebdd8d commit df4deb9

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static BlogPost Create(
4343
UpdatedDate = updatedDate ?? DateTime.Now,
4444
PreviewImageUrl = previewImageUrl,
4545
IsPublished = isPublished,
46-
Tags = tags?.Select(t => new Tag { Content = t.Trim() }).ToList(),
46+
Tags = tags?.Select(Tag.Create).ToList(),
4747
};
4848

4949
return blogPost;

LinkDotNet.Blog.Domain/Tag.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
namespace LinkDotNet.Blog.Domain;
1+
using System;
2+
3+
namespace LinkDotNet.Blog.Domain;
24

35
public class Tag
46
{
5-
public string Id { get; set; }
7+
private Tag()
8+
{
9+
}
10+
11+
public string Id { get; private set; }
12+
13+
public string Content { get; private set; }
14+
15+
public static Tag Create(string content)
16+
{
17+
if (string.IsNullOrWhiteSpace(content))
18+
{
19+
throw new ArgumentNullException(nameof(content));
20+
}
621

7-
public string Content { get; set; }
22+
return new Tag
23+
{
24+
Content = content.Trim(),
25+
};
26+
}
827
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using FluentAssertions;
3+
using LinkDotNet.Blog.Domain;
4+
using Xunit;
5+
6+
namespace LinkDotNet.Blog.UnitTests.Domain;
7+
8+
public class TagTests
9+
{
10+
[Fact]
11+
public void ShouldCreateTag()
12+
{
13+
var tag = Tag.Create(" Test ");
14+
15+
tag.Content.Should().Be("Test");
16+
}
17+
18+
[Theory]
19+
[InlineData("")]
20+
[InlineData(null)]
21+
[InlineData(" ")]
22+
public void ShouldThrowExceptionIfInvalid(string content)
23+
{
24+
Action act = () => Tag.Create(content);
25+
26+
act.Should().Throw<ArgumentNullException>();
27+
}
28+
}

0 commit comments

Comments
 (0)