Skip to content

Commit

Permalink
+ name value object
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhorikov committed Feb 8, 2020
1 parent bbb98bc commit 6716d0e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
17 changes: 17 additions & 0 deletions 3_Name_value_object.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
USE [EFCoreDDD]
GO
BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.Student.Name', N'Tmp_FirstName', 'COLUMN'
GO
EXECUTE sp_rename N'dbo.Student.Tmp_FirstName', N'FirstName', 'COLUMN'
GO
ALTER TABLE dbo.Student ADD
LastName nvarchar(50) NOT NULL CONSTRAINT DF_Student_LastName DEFAULT ''
GO
ALTER TABLE dbo.Student
DROP CONSTRAINT DF_Student_LastName
GO
ALTER TABLE dbo.Student SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
1 change: 1 addition & 0 deletions DddAndEFCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
1_Database_initial.sql = 1_Database_initial.sql
2_Enrollments.sql = 2_Enrollments.sql
3_Name_value_object.sql = 3_Name_value_object.sql
README.md = README.md
EndProjectSection
EndProject
Expand Down
46 changes: 46 additions & 0 deletions src/App/Name.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using CSharpFunctionalExtensions;

namespace App
{
public class Name : ValueObject
{
public string First { get; }
public string Last { get; }

protected Name()
{
}

private Name(string first, string last)
: this()
{
First = first;
Last = last;
}

public static Result<Name> Create(string firstName, string lastName)
{
if (string.IsNullOrWhiteSpace(firstName))
return Result.Failure<Name>("First name should not be empty");
if (string.IsNullOrWhiteSpace(lastName))
return Result.Failure<Name>("Last name should not be empty");

firstName = firstName.Trim();
lastName = lastName.Trim();

if (firstName.Length > 200)
return Result.Failure<Name>("First name is too long");
if (lastName.Length > 200)
return Result.Failure<Name>("Last name is too long");

return Result.Success(new Name(firstName, lastName));
}

protected override IEnumerable<object> GetEqualityComponents()
{
yield return First;
yield return Last;
}
}
}
6 changes: 5 additions & 1 deletion src/App/SchoolContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
x.Property(p => p.Id).HasColumnName("StudentID");
x.Property(p => p.Email)
.HasConversion(p => p.Value, p => Email.Create(p).Value);
x.Property(p => p.Name);
x.OwnsOne(p => p.Name, p =>
{
p.Property(pp => pp.First).HasColumnName("FirstName");
p.Property(pp => pp.Last).HasColumnName("LastName");
});
x.HasOne(p => p.FavoriteCourse).WithMany();
x.HasMany(p => p.Enrollments).WithOne(p => p.Student)
.OnDelete(DeleteBehavior.Cascade)
Expand Down
4 changes: 2 additions & 2 deletions src/App/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace App
{
public class Student : Entity
{
public string Name { get; set; }
public virtual Name Name { get; set; }
public Email Email { get; set; }
public virtual Course FavoriteCourse { get; set; }

Expand All @@ -17,7 +17,7 @@ protected Student()
}

public Student(
string name, Email email, Course favoriteCourse, Grade favoriteCourseGrade)
Name name, Email email, Course favoriteCourse, Grade favoriteCourseGrade)
: this()
{
Name = name;
Expand Down
6 changes: 3 additions & 3 deletions src/App/StudentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public string RegisterStudent(
if (result.IsFailure)
return result.Error;

var student = new Student(name, result.Value, favoriteCourse, favoriteCourseGrade);
_repository.Save(student);
//var student = new Student(name, result.Value, favoriteCourse, favoriteCourseGrade);
//_repository.Save(student);

_context.SaveChanges();

Expand All @@ -97,7 +97,7 @@ public string EditPersonalInfo(
if (result.IsFailure)
return result.Error;

student.Name = name;
//student.Name = name;
student.Email = result.Value;
student.FavoriteCourse = favoriteCourse;

Expand Down

0 comments on commit 6716d0e

Please sign in to comment.