Skip to content

Commit

Permalink
#371 Move example models to EF test library.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstone committed Aug 12, 2020
1 parent b59ca99 commit 908f8df
Show file tree
Hide file tree
Showing 27 changed files with 1,588 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using RapidField.SolidInstruments.Command;
using RapidField.SolidInstruments.Core.Concurrency;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Commands;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Entities;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Repositories;
using RapidField.SolidInstruments.ObjectComposition;
using System;
using System.Linq;

namespace RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.CommandHandlers
{
/// <summary>
/// Processes a command that adds a Fibonacci number to the Simulated database.
/// </summary>
public sealed class AddFibonacciNumberCommandHandler : SimulatedCommandHandler<AddFibonacciNumberCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="AddFibonacciNumberCommandHandler" /> class.
/// </summary>
/// <param name="mediator">
/// A processing intermediary that is used to process sub-commands.
/// </param>
/// <param name="repositoryFactory">
/// The factory that produces data access repositories for the handler.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="mediator" /> is <see langword="null" /> -or- <paramref name="repositoryFactory" /> is
/// <see langword="null" />.
/// </exception>
public AddFibonacciNumberCommandHandler(ICommandMediator mediator, SimulatedRepositoryFactory repositoryFactory)
: base(mediator, repositoryFactory)
{
return;
}

/// <summary>
/// Releases all resources consumed by the current <see cref="AddFibonacciNumberCommandHandler" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing) => base.Dispose(disposing);

/// <summary>
/// Processes the specified command.
/// </summary>
/// <param name="command">
/// The command to process.
/// </param>
/// <param name="mediator">
/// A processing intermediary that is used to process sub-commands. Do not process <paramref name="command" /> using
/// <paramref name="mediator" />, as doing so will generally result in infinite-looping.
/// </param>
/// <param name="repositories">
/// An object that provides access to data access repositories.
/// </param>
/// <param name="controlToken">
/// A token that represents and manages contextual thread safety.
/// </param>
protected override void Process(AddFibonacciNumberCommand command, ICommandMediator mediator, IFactoryProducedInstanceGroup repositories, IConcurrencyControlToken controlToken)
{
var fibonacciNumberSeries = NumberSeries.Named.Fibonacci;
var numberRepository = repositories.Get<NumberRepository>();
var number = numberRepository.FindByValue(command.NumberValue);

if (number is null)
{
number = new Number()
{
Identifier = Guid.NewGuid(),
Value = command.NumberValue
};

numberRepository.Add(number);
}

var numberSeriesNumberRespository = repositories.Get<NumberSeriesNumberRepository>();
var numberSeriesNumber = numberSeriesNumberRespository.FindWhere(entity => entity.Number.Value == number.Value && entity.NumberSeriesIdentifier == fibonacciNumberSeries.Identifier).SingleOrDefault();

if (numberSeriesNumber is null)
{
numberSeriesNumber = new NumberSeriesNumber()
{
Identifier = Guid.NewGuid(),
Number = number,
NumberIdentifier = number.Identifier,
NumberSeriesIdentifier = fibonacciNumberSeries.Identifier
};

numberSeriesNumberRespository.Add(numberSeriesNumber);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using RapidField.SolidInstruments.Command;
using RapidField.SolidInstruments.Core.Concurrency;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Commands;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Entities;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Repositories;
using RapidField.SolidInstruments.ObjectComposition;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.CommandHandlers
{
/// <summary>
/// Processes a command that gets all of the Fibonacci number values from the Simulated database.
/// </summary>
public sealed class GetFibonacciNumberValuesCommandHandler : SimulatedCommandHandler<GetFibonacciNumberValuesCommand, IEnumerable<Int64>>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetFibonacciNumberValuesCommandHandler" /> class.
/// </summary>
/// <param name="mediator">
/// A processing intermediary that is used to process sub-commands.
/// </param>
/// <param name="repositoryFactory">
/// The factory that produces data access repositories for the handler.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="mediator" /> is <see langword="null" /> -or- <paramref name="repositoryFactory" /> is
/// <see langword="null" />.
/// </exception>
public GetFibonacciNumberValuesCommandHandler(ICommandMediator mediator, SimulatedRepositoryFactory repositoryFactory)
: base(mediator, repositoryFactory)
{
return;
}

/// <summary>
/// Releases all resources consumed by the current <see cref="GetFibonacciNumberValuesCommandHandler" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing) => base.Dispose(disposing);

/// <summary>
/// Processes the specified command.
/// </summary>
/// <param name="command">
/// The command to process.
/// </param>
/// <param name="mediator">
/// A processing intermediary that is used to process sub-commands. Do not process <paramref name="command" /> using
/// <paramref name="mediator" />, as doing so will generally result in infinite-looping.
/// </param>
/// <param name="repositories">
/// An object that provides access to data access repositories.
/// </param>
/// <param name="controlToken">
/// A token that represents and manages contextual thread safety.
/// </param>
/// <returns>
/// The result that is emitted when processing the command.
/// </returns>
protected sealed override IEnumerable<Int64> Process(GetFibonacciNumberValuesCommand command, ICommandMediator mediator, IFactoryProducedInstanceGroup repositories, IConcurrencyControlToken controlToken)
{
var numberSeriesNumberRepository = repositories.Get<NumberSeriesNumberRepository>();
return numberSeriesNumberRepository.FindWhere(entity => entity.NumberSeries.Identifier == NumberSeries.Named.Fibonacci.Identifier).Select(entity => entity.Number.Value).ToList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using RapidField.SolidInstruments.DataAccess;
using System;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Commands
{
/// <summary>
/// Represents a data access command that adds a specified numeric value to the Fibonacci series.
/// </summary>
[DataContract]
public sealed class AddFibonacciNumberCommand : DataAccessCommand
{
/// <summary>
/// Initializes a new instance of the <see cref="AddFibonacciNumberCommand" /> class.
/// </summary>
/// <param name="numberValue">
/// The value of the Fibonacci number that is added to the series.
/// </param>
[DebuggerHidden]
internal AddFibonacciNumberCommand(Int64 numberValue)
: base()
{
NumberValue = numberValue;
}

/// <summary>
/// Gets or sets the value of the Fibonacci number that is added to the series.
/// </summary>
[DataMember]
public Int64 NumberValue
{
get;
set;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using RapidField.SolidInstruments.DataAccess;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Commands
{
/// <summary>
/// Represents a data access command that returns the numeric values for the Fibonacci series.
/// </summary>
[DataContract]
public sealed class GetFibonacciNumberValuesCommand : DataAccessCommand<IEnumerable<Int64>>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetFibonacciNumberValuesCommand" /> class.
/// </summary>
[DebuggerHidden]
internal GetFibonacciNumberValuesCommand()
: base()
{
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Models;
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;

namespace RapidField.SolidInstruments.DataAccess.EntityFramework.UnitTests.Entities
{
/// <summary>
/// Represents an integer number.
/// </summary>
public sealed class Number : INumber
{
/// <summary>
/// Initializes a new instance of the <see cref="Number" /> class.
/// </summary>
[DebuggerHidden]
internal Number()
{
return;
}

/// <summary>
/// Initializes a new instance of the <see cref="Number" /> class.
/// </summary>
/// <param name="model">
/// A model that is used to hydrate the new object.
/// </param>
[DebuggerHidden]
internal Number(INumber model)
: this(model.Identifier, model.Value)
{
return;
}

/// <summary>
/// Initializes a new instance of the <see cref="Number" /> class.
/// </summary>
/// <param name="identifier">
/// A unique identifier for the entity.
/// </param>
/// <param name="value">
/// The value of the number.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="identifier" /> is equal to <see cref="Guid.Empty" />.
/// </exception>
[DebuggerHidden]
private Number(Guid identifier, Int64 value)
{
Identifier = identifier.RejectIf().IsEqualToValue(Guid.Empty, nameof(identifier));
Value = value;
}

/// <summary>
/// Gets or sets a unique identifier for the entity.
/// </summary>
[Key]
public Guid Identifier
{
get;
set;
}

/// <summary>
/// Gets or sets the value of the number.
/// </summary>
[Required]
public Int64 Value
{
get;
set;
}
}
}
Loading

0 comments on commit 908f8df

Please sign in to comment.