public void AddNotificationsTo_updates_target_list() { var aggregateNotificationList = new NotificationList() .AddNotification("param1", "message1 for param1") .AddNotification("param1", "message2 for param1") .AddNotification("param2", "message1 for param2") .AddNotification("param3", "message1 for param3"); var targetNotificationList = new NotificationList() .AddNotification("param1", "message3 for param1") .AddNotification("param1", "message4 for param1") .AddNotification("param2", "message2 for param2") .AddNotification("param4", "message1 for param4"); var aggregateResult = AggregateResult <TestAggregate> .Fail(aggregateNotificationList); aggregateResult.AddNotificationsTo(targetNotificationList); targetNotificationList.ShouldSatisfyAllConditions( () => targetNotificationList["param1"].Count.ShouldBe(4), () => targetNotificationList["param2"].Count.ShouldBe(2), () => targetNotificationList["param3"].Count.ShouldBe(1), () => targetNotificationList["param4"].Count.ShouldBe(1), () => targetNotificationList["param1"].Any(n => n.Message == "message2 for param1").ShouldBeTrue(), () => targetNotificationList["param1"].Any(n => n.Message == "message4 for param1").ShouldBeTrue()); }
// This is here to compare the above pattern to a more traditional, // less-abstracted pattern. Food for thought... // // Note: Not tested, don't use. // private AggregateResult <ExamplePerson> SetEmailAddress2(string emailAddress) { AggregateResult <ExamplePerson> result; var notifications = new NotificationList(); if (emailAddress == null) { throw new ArgumentNullException(nameof(emailAddress)); } if (!RegexUtilities.IsValidEmail(emailAddress)) { notifications.AddNotification(nameof(EmailAddress), "A valid email address is required."); } if (notifications.HasNotifications) { result = AggregateResult <ExamplePerson> .Fail(notifications); } else { _emailAddress = emailAddress.Trim(); result = AggregateResult <ExamplePerson> .Success(this); } return(result); }
public static async Task <AggregateResult <TAggregate> > AddIfSucceeded <TAggregate>( this AggregateResult <TAggregate> aggregateResult, AppDbContext dbContext) where TAggregate : AggregateRoot { AggregateResult <TAggregate> saveResult = aggregateResult; if (aggregateResult.Succeeded) { try { await dbContext.AddAsync <TAggregate>(aggregateResult.NewAggregate); var count = await dbContext.SaveChangesAsync(); if (count == 0) { saveResult = AggregateResult <TAggregate> .Fail(new NotificationList("Database", "Save to database failed: Return count was zero in SaveIfSucceededAndReturn.")); } } catch (Exception ex) { saveResult = AggregateResult <TAggregate> .Fail( new NotificationList("Database", "An exception occurred when commiting to the database. See the application log for details."), ex); } } return(saveResult); }
public void Fail_given_exception_only_should_set_properties() { var exception = new Exception("ой"); var result = AggregateResult <ConcreteAggregate> .Fail(exception); result.Exception.ShouldBe(exception); }
public void Fail_should_return_correct_properties() { var notifications = new NotificationList("key1", "notification1"); var exception = new Exception("Oopsie!"); var result = AggregateResult <ConcreteAggregate> .Fail(notifications, exception); result.ShouldSatisfyAllConditions( () => result.Succeeded.ShouldBeFalse(), () => result.Notifications.ShouldBe(notifications), () => result.Exception.ShouldBe(exception), () => result.NewAggregate.ShouldBeNull()); }
public void Case_fail_without_exception_should_set_properties() { var aggregate = new ConcreteAggregate(); var notifications = new NotificationList("Anvil", "Must not land on Road Runner"); var aggregateResult = AggregateResult <ConcreteAggregate> .Fail(notifications); var commandResult = aggregateResult.ToCommandResult(); commandResult.ShouldSatisfyAllConditions( () => commandResult.Succeeded.ShouldBeFalse(), () => commandResult.Notifications["Anvil"] .ShouldContain(n => n.Message.Contains("Must not land on Road Runner"), 1), () => commandResult.Exception.ShouldBeNull(), () => commandResult.Status.ShouldBe(CommandResultStatus.ValidationError) ); }
public void Fail_given_null_notifications_should_return_empty_notifcations() { var result = AggregateResult <ConcreteAggregate> .Fail(null); result.Notifications.ShouldBeEmpty(); }