├── .gitattributes ├── .gitignore ├── .vscode ├── cSpell.json ├── launch.json └── tasks.json ├── GitVersion.yml ├── LICENSE.txt ├── README.md ├── build.ps1 ├── build.sh ├── build ├── dotnet-install.ps1 ├── dotnet-install.sh └── tools.csproj ├── global.json └── src ├── All.sln ├── Projac.Recipes ├── DataDefinition │ ├── CreateSchema.cs │ ├── DeleteData.cs │ ├── DropSchema.cs │ ├── SetCheckpoint.cs │ └── Usage.cs ├── ElasticsearchIntegration │ └── Usage.cs ├── Enveloping │ └── Usage.cs ├── EventStoreIntegration │ └── Usage.cs ├── MemoryCacheIntegration │ ├── ProjectionUsage.cs │ └── TestingUsage.cs ├── Projac.Recipes.csproj ├── RavenDBIntegration │ ├── ProjectionUsage.cs │ └── TestingUsage.cs ├── RedisIntegration │ └── Usage.cs ├── SQLiteIntegration │ └── Usage.cs ├── Shared │ ├── PortfolioAdded.cs │ ├── PortfolioRemoved.cs │ └── PortfolioRenamed.cs ├── SqlStreamStoreIntegration │ └── Usage.cs ├── Syntax │ └── Usage.cs ├── WindowsAzureStorageIntegration │ ├── PortfolioModel.cs │ ├── RebuildProjection.cs │ └── Usage.cs └── app.config ├── Projac.SQLite.Tests ├── Projac.SQLite.Tests.csproj ├── SQLiteParameterAssertions.cs ├── SQLiteParameterEqualityComparer.cs ├── SQLiteParameterValueStub.cs ├── SQLiteSyntaxTestCases.cs ├── SQLiteSyntaxTests.Compose.cs ├── SQLiteSyntaxTests.DataTypes.cs ├── SQLiteSyntaxTests.NonQueryStatement.cs ├── SQLiteSyntaxTests.QueryStatement.cs └── SQLiteSyntaxTests.cs ├── Projac.SQLite ├── Projac.SQLite.csproj ├── SQLiteDbParameterValue.cs ├── SQLiteSyntax.Compose.cs ├── SQLiteSyntax.NonQueryStatement.cs ├── SQLiteSyntax.QueryStatement.cs ├── SQLiteSyntax.cs └── SqLiteSyntax.DataTypes.cs ├── Projac.Sql.Tests ├── AnonymousSqlProjectionBuilderTests.cs ├── AnonymousSqlProjectionTests.cs ├── AsyncSqlProjectorTests.cs ├── ConcurrentResolveTests.cs ├── Executors │ ├── ConnectedSqlCommandExecutorTests.cs │ ├── ConnectedTransactionalSqlCommandExecutorTests.cs │ ├── SqlCommandExecutorTests.cs │ ├── TestDbConnection.cs │ ├── TestDbTransaction.cs │ └── TransactionalSqlCommandExecutorTests.cs ├── Framework │ └── SqlNonQueryCommandStub.cs ├── HandlerResolutionCases.cs ├── Projac.Sql.Tests.csproj ├── ProjectorProjectCases.cs ├── ResolveTests.cs ├── SqlNonQueryCommandComposerTests.cs ├── SqlNonQueryCommandTests.cs ├── SqlProjectionHandlerEnumeratorTests.cs ├── SqlProjectionHandlerTests.cs ├── SqlProjectionTests.cs ├── SqlProjectorTests.cs └── SqlQueryStatementTests.cs ├── Projac.Sql ├── AnonymousSqlProjection.cs ├── AnonymousSqlProjectionBuilder.cs ├── AsyncSqlProjector.cs ├── ConcurrentResolve.cs ├── Executors │ ├── ConnectedSqlCommandExecutor.cs │ ├── ConnectedTransactionalSqlCommandExecutor.cs │ ├── IAsyncSqlNonQueryCommandExecutor.cs │ ├── IAsyncSqlQueryCommandExecutor.cs │ ├── ISqlNonQueryCommandExecutor.cs │ ├── ISqlQueryCommandExecutor.cs │ ├── SqlCommandExecutor.cs │ └── TransactionalSqlCommandExecutor.cs ├── IDbParameterValue.cs ├── Projac.Sql.csproj ├── Resolve.cs ├── SqlNonQueryCommand.cs ├── SqlNonQueryCommandComposer.cs ├── SqlProjection.cs ├── SqlProjectionHandler.cs ├── SqlProjectionHandlerEnumerator.cs ├── SqlProjectionHandlerResolver.cs ├── SqlProjector.cs └── SqlQueryCommand.cs ├── Projac.SqlClient.Tests ├── Legacy │ ├── TSqlTests.Compose.cs │ ├── TSqlTests.DataTypes.cs │ ├── TSqlTests.NonQueryProcedure.cs │ ├── TSqlTests.NonQueryStatement.cs │ ├── TSqlTests.ParameterCountLimitExceeded.cs │ ├── TSqlTests.QueryProcedure.cs │ └── TSqlTests.QueryStatement.cs ├── ParameterCountLimitedExceeded.cs ├── Projac.SqlClient.Tests.csproj ├── SqlClientSyntaxTestCases.cs ├── SqlClientSyntaxTests.Compose.cs ├── SqlClientSyntaxTests.DataTypes.cs ├── SqlClientSyntaxTests.NonQueryProcedure.cs ├── SqlClientSyntaxTests.NonQueryStatement.cs ├── SqlClientSyntaxTests.ParameterCountLimitExceeded.cs ├── SqlClientSyntaxTests.QueryProcedure.cs ├── SqlClientSyntaxTests.QueryStatement.cs ├── SqlClientSyntaxTests.cs ├── SqlParameterAssertions.cs ├── SqlParameterEqualityComparer.cs ├── SqlParameterValueStub.cs ├── TSqlBigIntNullValueTests.cs ├── TSqlBigIntValueTests.cs ├── TSqlBinaryNullValueTests.cs ├── TSqlBinarySizeTest.cs ├── TSqlBinaryValueTests.cs ├── TSqlBitNullValueTests.cs ├── TSqlBitValueTests.cs ├── TSqlCharNullValueTests.cs ├── TSqlCharSizeTest.cs ├── TSqlCharValueTests.cs ├── TSqlDateNullValueTests.cs ├── TSqlDateTime2NullValueTests.cs ├── TSqlDateTime2PrecisionTest.cs ├── TSqlDateTime2ValueTests.cs ├── TSqlDateTimeNullValueTests.cs ├── TSqlDateTimeOffsetNullValueTests.cs ├── TSqlDateTimeOffsetValueTests.cs ├── TSqlDateTimeValueTests.cs ├── TSqlDateValueTests.cs ├── TSqlDecimalNullValueTests.cs ├── TSqlDecimalPrecisionTest.cs ├── TSqlDecimalScaleTest.cs ├── TSqlDecimalValueTests.cs ├── TSqlIntNullValueTests.cs ├── TSqlIntValueTests.cs ├── TSqlMoneyNullValueTests.cs ├── TSqlMoneyValueTests.cs ├── TSqlNCharNullValueTests.cs ├── TSqlNCharSizeTest.cs ├── TSqlNCharValueTests.cs ├── TSqlNVarCharNullValueTests.cs ├── TSqlNVarCharSizeTest.cs ├── TSqlNVarCharValueTests.cs ├── TSqlUniqueIdentifierNullValueTests.cs ├── TSqlUniqueIdentifierValueTests.cs ├── TSqlVarBinaryNullValueTests.cs ├── TSqlVarBinarySizeTest.cs ├── TSqlVarBinaryValueTests.cs ├── TSqlVarCharNullValueTests.cs ├── TSqlVarCharSizeTest.cs └── TSqlVarCharValueTests.cs ├── Projac.SqlClient ├── Legacy │ ├── TSql.Compose.cs │ ├── TSql.DataTypes.cs │ ├── TSql.NonQueryProcedure.cs │ ├── TSql.NonQueryStatement.cs │ ├── TSql.QueryProcedure.cs │ ├── TSql.QueryStatement.cs │ └── TSql.cs ├── Limits.cs ├── Projac.SqlClient.csproj ├── SqlClientSyntax.Compose.cs ├── SqlClientSyntax.DataTypes.cs ├── SqlClientSyntax.NonQueryProcedure.cs ├── SqlClientSyntax.NonQueryStatement.cs ├── SqlClientSyntax.QueryProcedure.cs ├── SqlClientSyntax.QueryStatement.cs ├── SqlClientSyntax.cs ├── TSqlBigIntNullValue.cs ├── TSqlBigIntValue.cs ├── TSqlBinaryNullValue.cs ├── TSqlBinarySize.cs ├── TSqlBinaryValue.cs ├── TSqlBitNullValue.cs ├── TSqlBitValue.cs ├── TSqlCharNullValue.cs ├── TSqlCharSize.cs ├── TSqlCharValue.cs ├── TSqlDateNullValue.cs ├── TSqlDateTime2NullValue.cs ├── TSqlDateTime2Precision.cs ├── TSqlDateTime2Value.cs ├── TSqlDateTimeNullValue.cs ├── TSqlDateTimeOffsetNullValue.cs ├── TSqlDateTimeOffsetValue.cs ├── TSqlDateTimeValue.cs ├── TSqlDateValue.cs ├── TSqlDecimalNullValue.cs ├── TSqlDecimalPrecision.cs ├── TSqlDecimalScale.cs ├── TSqlDecimalValue.cs ├── TSqlIntNullValue.cs ├── TSqlIntValue.cs ├── TSqlMoneyNullValue.cs ├── TSqlMoneyValue.cs ├── TSqlNCharNullValue.cs ├── TSqlNCharSize.cs ├── TSqlNCharValue.cs ├── TSqlNVarCharNullValue.cs ├── TSqlNVarCharSize.cs ├── TSqlNVarCharValue.cs ├── TSqlUniqueIdentifierNullValue.cs ├── TSqlUniqueIdentifierValue.cs ├── TSqlVarBinaryNullValue.cs ├── TSqlVarBinarySize.cs ├── TSqlVarBinaryValue.cs ├── TSqlVarCharNullValue.cs ├── TSqlVarCharSize.cs └── TSqlVarCharValue.cs ├── Projac.Tests ├── AnonymousProjectionBuilderTests.cs ├── AnonymousProjectionTests.cs ├── CallRecordingConnection.cs ├── ConcurrentResolveTests.cs ├── HandlerResolutionCases.cs ├── Projac.Tests.csproj ├── ProjectionHandlerEnumeratorTests.cs ├── ProjectionHandlerTests.cs ├── ProjectionTests.cs ├── ProjectorProjectCases.cs ├── ProjectorTests.cs ├── ResolveTests.cs ├── Signal.cs └── Testing │ ├── ProjectionScenarioTests.cs │ ├── ProjectionTestSpecificationTests.cs │ └── VerificationResultTests.cs ├── Projac ├── AnonymousProjection.cs ├── AnonymousProjectionBuilder.cs ├── ConcurrentResolve.cs ├── Projac.csproj ├── Projection.cs ├── ProjectionHandler.cs ├── ProjectionHandlerEnumerator.cs ├── ProjectionHandlerResolver.cs ├── Projector.cs ├── Resolve.cs ├── TaskExtensions.cs └── Testing │ ├── ProjectionScenario.cs │ ├── ProjectionTestSpecification.cs │ ├── VerificationResult.cs │ └── VerificationResultState.cs └── Recipes ├── Enveloping └── Usage.cs ├── Recipes.csproj ├── SqlStreamStoreIntegration └── Usage.cs └── packages.config /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | obj 3 | build/tools -------------------------------------------------------------------------------- /.vscode/cSpell.json: -------------------------------------------------------------------------------- 1 | // cSpell Settings 2 | { 3 | // Version of the setting file. Always 0.1 4 | "version": "0.1", 5 | // language - current active spelling language 6 | "language": "en", 7 | // words - list of words to be always considered correct 8 | "words": [ 9 | "Projac" 10 | ], 11 | // flagWords - list of words to be always considered incorrect 12 | // This is useful for offensive words and common spelling errors. 13 | // For example "hte" should be "the" 14 | "flagWords": [ 15 | "hte" 16 | ] 17 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceRoot}/src/Projac.Tests/bin/Debug/netcoreapp2.0/Projac.Tests.dll", 14 | "args": [], 15 | "cwd": "${workspaceRoot}/src/Projac.Tests", 16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window 17 | "console": "internalConsole", 18 | "stopAtEntry": false, 19 | "internalConsoleOptions": "openOnSessionStart" 20 | }, 21 | { 22 | "name": ".NET Core Attach", 23 | "type": "coreclr", 24 | "request": "attach", 25 | "processId": "${command:pickProcess}" 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.1.0", 3 | "command": "dotnet", 4 | "isShellCommand": true, 5 | "args": [], 6 | "tasks": [ 7 | { 8 | "taskName": "build", 9 | "args": [ 10 | "${workspaceRoot}/src/Projac.Tests/Projac.Tests.csproj" 11 | ], 12 | "isBuildCommand": true, 13 | "problemMatcher": "$msCompile" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /GitVersion.yml: -------------------------------------------------------------------------------- 1 | assembly-versioning-scheme: MajorMinorPatch 2 | mode: ContinuousDelivery 3 | next-version: 0.1.0 4 | branches: {} 5 | ignore: 6 | sha: [] -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | ==================== 3 | 4 | Copyright (c) 2013, Yves Reynhout 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 8 | 9 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 10 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 11 | Neither the name of the BitTacklr nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Projac 2 | 3 | Projac is a set of .NET libraries that allow you to author projections targeting various backing stores and is easy to integrate with existing event stores such as [EventStore](http://www.eventstore.org) and [SQLStreamStore](https://github.com/SQLStreamStore). [![Join the chat at https://gitter.im/yreynhout/Projac](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/yreynhout/Projac?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | - [Projac](https://github.com/BitTacklr/Projac/wiki/projac) allows you to author projections that target any store for which you can bring your own connection (e.g [Redis](http://redis.io), [RavenDB](http://ravendb.net/), [Elasticsearch](http://http://www.elasticsearch.org/), [Microsoft Windows Azure Table Storage](http://azure.microsoft.com/en-us/documentation/services/storage/)). 6 | 7 | - [Projac.Sql, Projac.SqlClient and Projac.SQLite](https://github.com/BitTacklr/Projac/wiki/projac.sql) allow you to author projections that target relational databases. Projac.Sql contains common abstractions across all database providers that use the ADO.NET model. Projac.SqlClient targets [Microsoft SQL Server](http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/overview.aspx). Projac.SQLite targets [SQLite](http://sqlite.org). We welcome contributions for other database providers that follow a similar recipe. 8 | 9 | It's available on both NuGet & MyGet: 10 | 11 | - Projac: [NuGet](https://www.nuget.org/packages/Projac/) - [MyGet](https://www.myget.org/feed/projac/package/nuget/Projac) 12 | - Projac.Sql: [NuGet](https://www.nuget.org/packages/Projac,Sql/) - [MyGet](https://www.myget.org/feed/projac/package/nuget/Projac.Sql) 13 | - Projac.SqlClient: [NuGet](https://www.nuget.org/packages/Projac.SqlClient/) - [MyGet](https://www.myget.org/feed/projac/package/nuget/Projac.SqlClient) 14 | - [WIP] Projac.SQLite: [NuGet](https://www.nuget.org/packages/Projac,SQLite/) - [MyGet](https://www.myget.org/feed/projac/package/nuget/Projac.SQLite) 15 | 16 | The custom MyGet feed can be found [here](https://www.myget.org/F/projac/api/v3/index.json). 17 | 18 | --- 19 | 20 | **Important Changes** 21 | 22 | If you're using a version prior to 0.1.0, not only has your cheese been moved, it probably has been broken in unexpected places. Please check out the [changes made in 0.1.0](https://github.com/BitTacklr/Projac/wiki/Changes0.1.0) as well as the [how do I upgrade to 0.1.0 guide](https://github.com/BitTacklr/Projac/wiki/UpgradeTo0.1.0). If you want to keep your cheese as is, you can always fork this code base and use the `legacy` branch. 23 | 24 | --- 25 | 26 | # Contributions 27 | 28 | * Date, DateTime, DateTime2, Money data types in TSql by [@xt0rted](https://github.com/xt0rted) 29 | * The ``positional syntax`` suggestion by [@tojans](https://github.com/tojans). 30 | * Decimal data type in SqlClientSyntax by [@ritasker](https://github.com/ritasker) 31 | -------------------------------------------------------------------------------- /build/tools.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | netstandard2.0;net46;net452 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "2.0.2" 4 | } 5 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/DataDefinition/CreateSchema.cs: -------------------------------------------------------------------------------- 1 | namespace Recipes.DataDefinition 2 | { 3 | /// 4 | /// Instructs a projection to create its schema objects. 5 | /// 6 | public class CreateSchema 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/DataDefinition/DeleteData.cs: -------------------------------------------------------------------------------- 1 | namespace Recipes.DataDefinition 2 | { 3 | /// 4 | /// Instructs a projection to delete its data. 5 | /// 6 | public class DeleteData 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/DataDefinition/DropSchema.cs: -------------------------------------------------------------------------------- 1 | namespace Recipes.DataDefinition 2 | { 3 | /// 4 | /// Instructs a projection to drop its schema objects. 5 | /// 6 | public class DropSchema 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/DataDefinition/SetCheckpoint.cs: -------------------------------------------------------------------------------- 1 | namespace Recipes.DataDefinition 2 | { 3 | /// 4 | /// Instructs a projection to record its checkpoint. 5 | /// 6 | public class SetCheckpoint 7 | { 8 | /// 9 | /// The checkpoint the projection is at. 10 | /// 11 | public readonly long Checkpoint; 12 | 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The checkpoint the projection is at. 17 | public SetCheckpoint(long checkpoint) 18 | { 19 | Checkpoint = checkpoint; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/DataDefinition/Usage.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using Projac.Sql; 4 | using Projac.SqlClient; 5 | 6 | namespace Recipes.DataDefinition 7 | { 8 | public static class Usage 9 | { 10 | private static readonly byte[] Id = "Sample".HashId(); 11 | 12 | public class SampleUsingProjection : SqlClientProjection 13 | { 14 | public SampleUsingProjection() 15 | { 16 | When(_ => 17 | Sql.NonQueryStatement( 18 | "CREATE TABLE [Sample] ([Id] INT NOT NULL CONSTRAINT PK_Sample PRIMARY KEY, [Value] INT NOT NULL)")); 19 | 20 | When(_ => 21 | Sql.NonQueryStatement( 22 | "DROP TABLE [Sample]")); 23 | 24 | When(_ => 25 | Sql.NonQueryStatement( 26 | "DELETE FROM [Sample]")); 27 | 28 | When(_ => 29 | Sql.NonQueryStatement( 30 | "UPDATE [CheckpointGate] SET Checkpoint = @Checkpoint WHERE [Id] = @Id", 31 | new 32 | { 33 | Checkpoint = Sql.BigInt(_.Checkpoint), 34 | Id = Sql.Binary(Id, 16) 35 | })); 36 | } 37 | } 38 | 39 | public class SqlClientProjection : SqlProjection 40 | { 41 | private static readonly SqlClientSyntax Syntax = new SqlClientSyntax(); 42 | 43 | protected SqlClientSyntax Sql 44 | { 45 | get { return Syntax; } 46 | } 47 | } 48 | 49 | public static class SampleUsingBuilder 50 | { 51 | private static readonly SqlClientSyntax Sql = new SqlClientSyntax(); 52 | 53 | public static readonly AnonymousSqlProjection Instance = new AnonymousSqlProjectionBuilder(). 54 | When(_ => 55 | Sql.NonQueryStatement( 56 | "CREATE TABLE [Sample] ([Id] INT NOT NULL CONSTRAINT PK_Sample PRIMARY KEY, [Value] INT NOT NULL)")). 57 | When(_ => 58 | Sql.NonQueryStatement( 59 | "DROP TABLE [Sample]")). 60 | When(_ => 61 | Sql.NonQueryStatement( 62 | "DELETE FROM [Sample]")). 63 | When(_ => 64 | Sql.NonQueryStatement( 65 | "UPDATE [CheckpointGate] SET Checkpoint = @Checkpoint WHERE [Id] = @Id", 66 | new 67 | { 68 | Checkpoint = Sql.BigInt(_.Checkpoint), 69 | Id = Sql.Binary(Id, 16) 70 | })). 71 | Build(); 72 | } 73 | 74 | private static byte[] HashId(this string value) 75 | { 76 | using (var hash = MD5.Create()) 77 | return hash.ComputeHash(Encoding.UTF8.GetBytes(value)); 78 | } 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /src/Projac.Recipes/ElasticsearchIntegration/Usage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Elasticsearch.Net; 4 | using Newtonsoft.Json; 5 | using NUnit.Framework; 6 | using Projac; 7 | using Recipes.Shared; 8 | 9 | namespace Recipes.ElasticsearchIntegration 10 | { 11 | [TestFixture, Ignore("Because 'Explicit' is not respected by R#")] 12 | public class Usage 13 | { 14 | [Test] 15 | public async Task Show() 16 | { 17 | //Spin up a docker image of elastic search and/or change the endpoint below 18 | var config = new ConnectionConfiguration(new Uri("http://192.168.99.100:32769/")); 19 | var client = new ElasticLowLevelClient(config); 20 | var portfolioId = Guid.NewGuid(); 21 | await new Projector(Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)). 22 | ProjectAsync(client, new object[] 23 | { 24 | new PortfolioAdded {Id = portfolioId, Name = "My portfolio"}, 25 | new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"}, 26 | new PortfolioRemoved {Id = portfolioId} 27 | }); 28 | } 29 | 30 | public static AnonymousProjection Projection = new AnonymousProjectionBuilder(). 31 | When((client, message) => 32 | client.IndexAsync( 33 | "index", 34 | "portfolio", 35 | message.Id.ToString("N"), 36 | new PostData(JsonConvert.SerializeObject(new 37 | { 38 | name = message.Name 39 | })))). 40 | When((client, message) => 41 | client.DeleteAsync( 42 | "index", 43 | "portfolio", 44 | message.Id.ToString("N"))). 45 | When((client, message) => 46 | client.UpdateAsync( 47 | "index", 48 | "portfolio", 49 | message.Id.ToString("N"), 50 | new PostData(JsonConvert.SerializeObject( 51 | new 52 | { 53 | Script = "ctx._source.name=name;", 54 | Params = new 55 | { 56 | name = message.Name 57 | } 58 | })))). 59 | Build(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Projac.Recipes/MemoryCacheIntegration/ProjectionUsage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Caching; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | using Projac; 6 | using Recipes.Shared; 7 | 8 | namespace Recipes.MemoryCacheIntegration 9 | { 10 | [TestFixture, Ignore("Because 'Explicit' is not respected by R#")] 11 | public class ProjectionUsage 12 | { 13 | [Test] 14 | public async Task Show() 15 | { 16 | using (var cache = new MemoryCache(new Random().Next().ToString())) 17 | { 18 | var portfolioId = Guid.NewGuid(); 19 | await new Projector( 20 | Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)). 21 | ProjectAsync(cache, new object[] 22 | { 23 | new PortfolioAdded {Id = portfolioId, Name = "My portfolio"}, 24 | new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"}, 25 | new PortfolioRemoved {Id = portfolioId } 26 | }); 27 | } 28 | } 29 | 30 | public static AnonymousProjection Projection = 31 | new AnonymousProjectionBuilder(). 32 | When((cache, message) => 33 | { 34 | cache.Add( 35 | new CacheItem( 36 | message.Id.ToString(), 37 | new PortfolioModel 38 | { 39 | Id = message.Id, 40 | Name = message.Name 41 | }), new CacheItemPolicy 42 | { 43 | AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration 44 | }); 45 | }). 46 | When((cache, message) => 47 | { 48 | cache.Remove(message.Id.ToString()); 49 | }). 50 | When((cache, message) => 51 | { 52 | var item = cache.GetCacheItem(message.Id.ToString()); 53 | if (item != null) 54 | { 55 | var model = (PortfolioModel) item.Value; 56 | model.Name = message.Name; 57 | } 58 | }). 59 | Build(); 60 | 61 | class PortfolioModel 62 | { 63 | public Guid Id { get; set; } 64 | public string Name { get; set; } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Projac.Recipes/RavenDBIntegration/ProjectionUsage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | using Projac; 6 | using Raven.Client; 7 | using Raven.Client.Embedded; 8 | using Recipes.Shared; 9 | 10 | namespace Recipes.RavenDBIntegration 11 | { 12 | [TestFixture, Ignore("Because 'Explicit' is not respected by R#")] 13 | public class ProjectionUsage 14 | { 15 | [Test] 16 | public async Task Show() 17 | { 18 | using (var store = new EmbeddableDocumentStore 19 | { 20 | RunInMemory = true, 21 | DataDirectory = Path.GetTempPath() 22 | }) 23 | { 24 | store.Initialize(); 25 | using (var session = store.OpenAsyncSession()) 26 | { 27 | var portfolioId = Guid.NewGuid(); 28 | await new Projector(Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)). 29 | ProjectAsync(session, new object[] 30 | { 31 | new PortfolioAdded {Id = portfolioId, Name = "My portfolio"}, 32 | new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"}, 33 | new PortfolioRemoved {Id = portfolioId} 34 | }); 35 | } 36 | } 37 | } 38 | 39 | public static AnonymousProjection Projection = new AnonymousProjectionBuilder(). 40 | When((session, message) => session.StoreAsync( 41 | new PortfolioDocument 42 | { 43 | Id = message.Id, 44 | Name = message.Name 45 | }, message.Id.ToString("N"))). 46 | When(async (session, message) => 47 | { 48 | var document = await session.LoadAsync(message.Id.ToString("N")); 49 | session.Delete(document); 50 | }). 51 | When(async (session, message) => 52 | { 53 | var document = await session.LoadAsync(message.Id.ToString("N")); 54 | document.Name = message.Name; 55 | }). 56 | Build(); 57 | 58 | class PortfolioDocument 59 | { 60 | public Guid Id { get; set; } 61 | public string Name { get; set; } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Projac.Recipes/RedisIntegration/Usage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | using Projac; 6 | using Recipes.Shared; 7 | using StackExchange.Redis; 8 | 9 | namespace Recipes.RedisIntegration 10 | { 11 | [TestFixture, Ignore("Because 'Explicit' is not respected by R#")] 12 | public class Usage 13 | { 14 | [Test] 15 | public async Task Show() 16 | { 17 | var connection = await ConnectionMultiplexer.ConnectAsync(new ConfigurationOptions 18 | { 19 | EndPoints = 20 | { 21 | {IPAddress.Parse("192.168.99.100"), 32770} 22 | } 23 | }); 24 | var portfolioId = Guid.NewGuid(); 25 | await new Projector(Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)). 26 | ProjectAsync(connection, new object[] 27 | { 28 | new PortfolioAdded {Id = portfolioId, Name = "My portfolio"}, 29 | new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"}, 30 | new PortfolioRemoved {Id = portfolioId} 31 | }); 32 | } 33 | 34 | public static AnonymousProjection Projection = new AnonymousProjectionBuilder(). 35 | When((connection, message) => 36 | { 37 | var db = connection.GetDatabase(); 38 | return db.HashSetAsync(message.Id.ToString("N"), "Name", message.Name); 39 | }). 40 | When((connection, message) => 41 | { 42 | var db = connection.GetDatabase(); 43 | return db.HashDeleteAsync(message.Id.ToString("N"), "Name"); 44 | }). 45 | When((connection, message) => 46 | { 47 | var db = connection.GetDatabase(); 48 | return db.HashSetAsync(message.Id.ToString("N"), "Name", message.Name); 49 | }). 50 | Build(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Projac.Recipes/Shared/PortfolioAdded.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Recipes.Shared 4 | { 5 | public class PortfolioAdded 6 | { 7 | public Guid Id { get; set; } 8 | public string Name { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/Shared/PortfolioRemoved.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Recipes.Shared 4 | { 5 | public class PortfolioRemoved 6 | { 7 | public Guid Id { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/Shared/PortfolioRenamed.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Recipes.Shared 4 | { 5 | public class PortfolioRenamed 6 | { 7 | public Guid Id { get; set; } 8 | public string Name { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/Syntax/Usage.cs: -------------------------------------------------------------------------------- 1 | using Projac.Sql; 2 | using Projac.SqlClient; 3 | using Recipes.Shared; 4 | 5 | namespace Recipes.Syntax 6 | { 7 | public class Usage 8 | { 9 | public class SampleUsingProjection : SqlProjection 10 | { 11 | public SampleUsingProjection() 12 | { 13 | When(_ => 14 | Sql.NonQueryStatement( 15 | "INSERT INTO [Portfolio] () VALUES ()")); 16 | 17 | When(_ => 18 | Sql.NonQueryStatement( 19 | "UPDATE [Portfolio] SET WHERE")); 20 | 21 | When(_ => 22 | Sql.NonQueryStatement( 23 | "DELETE FROM [Portfolio] WHERE [Id]=@Id", Sql.UniqueIdentifier(_.Id))); 24 | } 25 | } 26 | 27 | /// 28 | /// Represent a SQL projection with syntax. 29 | /// 30 | public abstract class SqlProjection : SqlProjection 31 | where TSyntax : new() 32 | { 33 | private static readonly TSyntax Syntax = new TSyntax(); 34 | 35 | /// 36 | /// Gets the sql syntax associated with this projection. 37 | /// 38 | /// 39 | /// The sql syntax associated with this projection. 40 | /// 41 | protected TSyntax Sql 42 | { 43 | get { return Syntax; } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Projac.Recipes/WindowsAzureStorageIntegration/PortfolioModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.WindowsAzure.Storage.Table; 3 | 4 | namespace Recipes.WindowsAzureStorageIntegration 5 | { 6 | public class PortfolioModel : TableEntity 7 | { 8 | public PortfolioModel(Guid id) 9 | { 10 | PartitionKey = "Portfolio"; 11 | RowKey = id.ToString("N"); 12 | } 13 | 14 | public string Name { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/WindowsAzureStorageIntegration/RebuildProjection.cs: -------------------------------------------------------------------------------- 1 | namespace Recipes.WindowsAzureStorageIntegration 2 | { 3 | public class RebuildProjection {} 4 | } -------------------------------------------------------------------------------- /src/Projac.Recipes/WindowsAzureStorageIntegration/Usage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Microsoft.WindowsAzure.Storage; 4 | using Microsoft.WindowsAzure.Storage.Table; 5 | using NUnit.Framework; 6 | using Projac; 7 | using Recipes.Shared; 8 | 9 | namespace Recipes.WindowsAzureStorageIntegration 10 | { 11 | [TestFixture, Ignore("Because 'Explicit' is not respected by R#")] 12 | public class Usage 13 | { 14 | [Test] 15 | public async Task ShowUsingDevStorage() 16 | { 17 | var account = CloudStorageAccount.DevelopmentStorageAccount; 18 | var client = account.CreateCloudTableClient(); 19 | var portfolioId = Guid.NewGuid(); 20 | await new Projector(Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)). 21 | ProjectAsync(client, new object[] 22 | { 23 | new RebuildProjection(), 24 | new PortfolioAdded {Id = portfolioId, Name = "My portfolio"}, 25 | new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"}, 26 | new PortfolioRemoved {Id = portfolioId} 27 | }); 28 | } 29 | 30 | public static AnonymousProjection Projection = new AnonymousProjectionBuilder(). 31 | When((client, message) => 32 | { 33 | var table = client.GetTableReference("Portfolio"); 34 | return table.CreateIfNotExistsAsync(); 35 | }). 36 | When((client, message) => 37 | { 38 | var table = client.GetTableReference("Portfolio"); 39 | return table.ExecuteAsync( 40 | TableOperation.Insert( 41 | new PortfolioModel(message.Id) 42 | { 43 | Name = message.Name 44 | })); 45 | }). 46 | When((client, message) => 47 | { 48 | var table = client.GetTableReference("Portfolio"); 49 | return table.ExecuteAsync( 50 | TableOperation.Delete(new PortfolioModel(message.Id) 51 | { 52 | ETag = "*" 53 | })); 54 | }). 55 | When((client, message) => 56 | { 57 | var table = client.GetTableReference("Portfolio"); 58 | return table.ExecuteAsync( 59 | TableOperation.Merge(new PortfolioModel(message.Id) 60 | { 61 | Name = message.Name, 62 | ETag = "*" 63 | })); 64 | }). 65 | Build(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Projac.Recipes/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Projac.SQLite.Tests/Projac.SQLite.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.SQLite.Tests 5 | Projac.SQLite.Tests 6 | netcoreapp2.0 7 | net46;net452 8 | AnyCPU 9 | Projac.SQLite.Tests 10 | true 11 | false 12 | 13 | 14 | 0.0.0 15 | 0.0.0 16 | 0.0.0 17 | 18 | 19 | False 20 | False 21 | Off 22 | 23 | 24 | False 25 | True 26 | Off 27 | 28 | 29 | false 30 | Projac.SQLite.Tests 31 | Yves Reynhout 32 | BitTacklr 33 | Projac 34 | Projac.SQLite.Tests unit tests Projac.SQLite. 35 | Copyright © BitTacklr 2017. All rights reserved. 36 | en 37 | 38 | 39 | 40 | 41 | 42 | 43 | portable 44 | 45 | 46 | full 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/Projac.SQLite.Tests/SQLiteParameterAssertions.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using System.Data.Common; 3 | using System.Data.SQLite; 4 | using NUnit.Framework; 5 | 6 | namespace Projac.SQLite.Tests 7 | { 8 | internal static class SQLiteParameterAssertions 9 | { 10 | public static void ExpectSQLiteParameter(this DbParameter parameter, 11 | string name, 12 | DbType sqlDbType, 13 | object value, 14 | bool nullable = false, 15 | int size = 0) 16 | { 17 | Assert.That(parameter, Is.Not.Null); 18 | Assert.That(parameter, Is.InstanceOf()); 19 | 20 | ((SQLiteParameter)parameter).ExpectSQLiteParameter(name, sqlDbType, value, nullable, size); 21 | } 22 | 23 | public static void ExpectSQLiteParameter(this SQLiteParameter parameter, 24 | string name, 25 | DbType dbType, 26 | object value, 27 | bool nullable = false, 28 | int size = 0) 29 | { 30 | Assert.That(parameter, Is.Not.Null); 31 | 32 | Assert.That(parameter.ParameterName, Is.EqualTo(name)); 33 | Assert.That(parameter.Direction, Is.EqualTo(ParameterDirection.Input)); 34 | Assert.That(parameter.SourceColumn, Is.EqualTo("")); 35 | Assert.That(parameter.SourceColumnNullMapping, Is.False); 36 | Assert.That(parameter.SourceVersion, Is.EqualTo(DataRowVersion.Default)); 37 | Assert.That(parameter.Value, Is.EqualTo(value)); 38 | Assert.That(parameter.Size, Is.EqualTo(size)); 39 | Assert.That(parameter.IsNullable, Is.EqualTo(nullable)); 40 | Assert.That(parameter.DbType, Is.EqualTo(dbType)); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /src/Projac.SQLite.Tests/SQLiteParameterEqualityComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.SQLite; 3 | 4 | namespace Projac.SQLite.Tests 5 | { 6 | internal class SQLiteParameterEqualityComparer : IEqualityComparer 7 | { 8 | public bool Equals(SQLiteParameter x, SQLiteParameter y) 9 | { 10 | if (x == null && y == null) return true; 11 | if (x != null && y != null) 12 | { 13 | return Equals(x.ParameterName, y.ParameterName) && 14 | Equals(x.Direction, y.Direction) && 15 | Equals(x.Value, y.Value) && 16 | Equals(x.IsNullable, y.IsNullable) && 17 | Equals(x.DbType, y.DbType) && 18 | Equals(x.Size, y.Size) && 19 | Equals(x.SourceColumn, y.SourceColumn) && 20 | Equals(x.SourceColumnNullMapping, y.SourceColumnNullMapping) && 21 | Equals(x.SourceVersion, y.SourceVersion); 22 | } 23 | return false; 24 | } 25 | 26 | public int GetHashCode(SQLiteParameter obj) 27 | { 28 | if (obj == null) 29 | return 0; 30 | return obj.ParameterName.GetHashCode() ^ 31 | obj.Direction.GetHashCode() ^ 32 | (obj.Value == null ? 0 : obj.Value.GetHashCode()) ^ 33 | obj.IsNullable.GetHashCode() ^ 34 | obj.DbType.GetHashCode() ^ 35 | obj.Size.GetHashCode() ^ 36 | obj.SourceColumn.GetHashCode() ^ 37 | obj.SourceColumnNullMapping.GetHashCode() ^ 38 | obj.SourceVersion.GetHashCode(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/Projac.SQLite.Tests/SQLiteParameterValueStub.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using System.Data.SQLite; 4 | using Projac.Sql; 5 | 6 | namespace Projac.SQLite.Tests 7 | { 8 | internal class SQLiteParameterValueStub : IDbParameterValue 9 | { 10 | public DbParameter ToDbParameter(string parameterName) 11 | { 12 | return new SQLiteParameter { ParameterName = parameterName, Value = DBNull.Value }; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /src/Projac.SQLite.Tests/SQLiteSyntaxTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Projac.SQLite; 3 | 4 | namespace Projac.SQLite.Tests 5 | { 6 | [TestFixture] 7 | public partial class SQLiteSyntaxTests 8 | { 9 | private SQLiteSyntax _syntax; 10 | 11 | [SetUp] 12 | public void SetUp() 13 | { 14 | _syntax = new SQLiteSyntax(); 15 | } 16 | 17 | private SQLiteSyntax Sql { get { return _syntax; } } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Projac.SQLite/Projac.SQLite.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.SQLite 5 | Projac.SQLite 6 | netstandard2.0 7 | net46;net452 8 | AnyCPU 9 | Projac.SQLite 10 | true 11 | 12 | 13 | 0.0.0 14 | 0.0.0 15 | 0.0.0 16 | 17 | 18 | False 19 | False 20 | true 21 | Off 22 | 23 | 24 | False 25 | True 26 | true 27 | Off 28 | 29 | 30 | true 31 | 0.0.0 32 | Projac.SQLite 33 | Yves Reynhout 34 | Projac.SQLite 35 | Projac.SQLite 36 | Yves Reynhout 37 | BitTacklr 38 | Projac 39 | Projac.SQLite provides lightweight infrastructure for authoring SQL projections targeting SQLite. 40 | Copyright © BitTacklr 2017. All rights reserved. 41 | en 42 | false 43 | https://github.com/yreynhout/Projac/blob/master/LICENSE 44 | https://github.com/yreynhout/Projac 45 | Please visit https://github.com/BitTacklr/Projac/wiki/Changes$(PackageVersion) . 46 | command and query responsibility segregation;cqrs;event sourcing;es;domain driven design;ddd 47 | true 48 | true 49 | false 50 | https://github.com/yreynhout/Projac 51 | git 52 | true 53 | false 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/Projac.SQLite/SQLiteSyntax.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Common; 2 | using System.Linq; 3 | using System.Reflection; 4 | using Projac.Sql; 5 | 6 | namespace Projac.SQLite 7 | { 8 | /// 9 | /// Fluent SQLite syntax. 10 | /// 11 | public partial class SQLiteSyntax 12 | { 13 | private static DbParameter[] CollectFromAnonymousType(object parameters) 14 | { 15 | if (parameters == null) 16 | return new DbParameter[0]; 17 | return parameters. 18 | GetType(). 19 | GetProperties(BindingFlags.Instance | BindingFlags.Public). 20 | Where(property => typeof(IDbParameterValue).IsAssignableFrom(property.PropertyType)). 21 | Select(property => 22 | ((IDbParameterValue)property.GetGetMethod().Invoke(parameters, null)). 23 | ToDbParameter(FormatDbParameterName(property.Name))). 24 | ToArray(); 25 | } 26 | 27 | private static string FormatDbParameterName(string name) 28 | { 29 | return "@" + name; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/ConcurrentResolveTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace Projac.Sql.Tests 5 | { 6 | [TestFixture] 7 | public class ConcurrentResolveTests 8 | { 9 | [Test] 10 | public void WhenEqualToHandlerMessageTypeHandlersCanNotBeNull() 11 | { 12 | Assert.Throws(() => 13 | ConcurrentResolve.WhenEqualToHandlerMessageType(null)); 14 | } 15 | 16 | [Test] 17 | public void WhenEqualToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 18 | { 19 | var sut = ConcurrentResolve.WhenEqualToHandlerMessageType(new SqlProjectionHandler[0]); 20 | Assert.Throws(() => sut(null)); 21 | } 22 | 23 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenEqualToHandlerMessageTypeCases")] 24 | public void WhenEqualToHandlerMessageTypeResolverReturnsExpectedResult( 25 | SqlProjectionHandler[] resolvable, 26 | object message, 27 | SqlProjectionHandler[] resolved) 28 | { 29 | var sut = ConcurrentResolve.WhenEqualToHandlerMessageType(resolvable); 30 | var result = sut(message); 31 | Assert.That(result, Is.EquivalentTo(resolved)); 32 | } 33 | 34 | [Test] 35 | public void WhenAssignableToHandlerMessageTypeHandlersCanNotBeNull() 36 | { 37 | Assert.Throws(() => 38 | ConcurrentResolve.WhenAssignableToHandlerMessageType(null)); 39 | } 40 | 41 | [Test] 42 | public void WhenAssignableToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 43 | { 44 | var sut = ConcurrentResolve.WhenAssignableToHandlerMessageType(new SqlProjectionHandler[0]); 45 | Assert.Throws(() => sut(null)); 46 | } 47 | 48 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenAssignableToHandlerMessageTypeCases")] 49 | public void WhenAssignableToHandlerMessageTypeResolverReturnsExpectedResult( 50 | SqlProjectionHandler[] resolvable, 51 | object message, 52 | SqlProjectionHandler[] resolved) 53 | { 54 | var sut = ConcurrentResolve.WhenAssignableToHandlerMessageType(resolvable); 55 | var result = sut(message); 56 | Assert.That(result, Is.EquivalentTo(resolved)); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/Executors/ConnectedTransactionalSqlCommandExecutorTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data.Common; 4 | using System.Threading; 5 | using NUnit.Framework; 6 | using Projac.Sql.Executors; 7 | 8 | namespace Projac.Sql.Tests.Executors 9 | { 10 | [TestFixture] 11 | public class ConnectedTransactionalSqlCommandExecutorTests 12 | { 13 | [Test] 14 | public void IsSynchronousTransactionalSqlNonQueryCommandExecutor() 15 | { 16 | Assert.IsInstanceOf(SutFactory()); 17 | } 18 | 19 | [Test] 20 | public void IsAsynchronousSqlNonQueryCommandExecutor() 21 | { 22 | Assert.IsInstanceOf(SutFactory()); 23 | } 24 | 25 | [Test] 26 | public void TransactionCanNotBeNull() 27 | { 28 | Assert.Throws( 29 | () => SutFactory(null)); 30 | } 31 | 32 | [Test] 33 | public void ExecuteNonQueryCommandCanNotBeNull() 34 | { 35 | var sut = SutFactory(); 36 | Assert.Throws( 37 | () => sut.ExecuteNonQuery((SqlNonQueryCommand)null)); 38 | } 39 | 40 | [Test] 41 | public void ExecuteNonQueryCommandsCanNotBeNull() 42 | { 43 | var sut = SutFactory(); 44 | Assert.Throws( 45 | () => sut.ExecuteNonQuery((IEnumerable) null)); 46 | } 47 | 48 | [Test] 49 | public void ExecuteNonQueryAsyncCommandCanNotBeNull() 50 | { 51 | var sut = SutFactory(); 52 | Assert.That(async () => 53 | await sut.ExecuteNonQueryAsync((SqlNonQueryCommand)null), 54 | Throws.ArgumentNullException); 55 | } 56 | 57 | [Test] 58 | public void ExecuteNonQueryAsyncTokenCommandCanNotBeNull() 59 | { 60 | var sut = SutFactory(); 61 | Assert.That(async () => 62 | await sut.ExecuteNonQueryAsync((SqlNonQueryCommand)null, CancellationToken.None), 63 | Throws.ArgumentNullException); 64 | } 65 | 66 | [Test] 67 | public void ExecuteNonQueryAsyncCommandsCanNotBeNull() 68 | { 69 | var sut = SutFactory(); 70 | Assert.That(async () => 71 | await sut.ExecuteNonQueryAsync((IEnumerable)null), 72 | Throws.ArgumentNullException); 73 | } 74 | 75 | [Test] 76 | public void ExecuteNonQueryAsyncTokenCommandsCanNotBeNull() 77 | { 78 | var sut = SutFactory(); 79 | Assert.That(async () => 80 | await sut.ExecuteNonQueryAsync((IEnumerable)null, CancellationToken.None), 81 | Throws.ArgumentNullException); 82 | } 83 | 84 | private static ConnectedTransactionalSqlCommandExecutor SutFactory() 85 | { 86 | return SutFactory(new TestDbTransaction(new TestDbConnection())); 87 | } 88 | 89 | private static ConnectedTransactionalSqlCommandExecutor SutFactory(DbTransaction transaction) 90 | { 91 | return new ConnectedTransactionalSqlCommandExecutor(transaction); 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/Executors/TestDbConnection.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using System.Data.Common; 3 | 4 | namespace Projac.Sql.Tests.Executors 5 | { 6 | public class TestDbConnection : DbConnection 7 | { 8 | private ConnectionState _connectionState; 9 | 10 | public TestDbConnection() 11 | { 12 | _connectionState = ConnectionState.Closed; 13 | } 14 | 15 | protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) 16 | { 17 | return null; 18 | } 19 | 20 | public override void Close() 21 | { 22 | } 23 | 24 | public override void ChangeDatabase(string databaseName) 25 | { 26 | } 27 | 28 | public override void Open() 29 | { 30 | _connectionState = ConnectionState.Open; 31 | } 32 | 33 | public override string ConnectionString { get; set; } 34 | 35 | public override string Database 36 | { 37 | get { return ""; } 38 | } 39 | 40 | public override ConnectionState State 41 | { 42 | get { return _connectionState; } 43 | } 44 | 45 | public override string DataSource 46 | { 47 | get { return ""; } 48 | } 49 | 50 | public override string ServerVersion 51 | { 52 | get { return ""; } 53 | } 54 | 55 | protected override DbCommand CreateDbCommand() 56 | { 57 | return null; 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/Executors/TestDbTransaction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | 5 | namespace Projac.Sql.Tests.Executors 6 | { 7 | public class TestDbTransaction : DbTransaction 8 | { 9 | private readonly TestDbConnection _connection; 10 | 11 | public TestDbTransaction(TestDbConnection connection) 12 | { 13 | if (connection == null) throw new ArgumentNullException("connection"); 14 | _connection = connection; 15 | } 16 | 17 | public override void Commit() 18 | { 19 | } 20 | 21 | public override void Rollback() 22 | { 23 | } 24 | 25 | protected override DbConnection DbConnection 26 | { 27 | get { return _connection; } 28 | } 29 | 30 | public override IsolationLevel IsolationLevel 31 | { 32 | get { return IsolationLevel.Unspecified; } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/Framework/SqlNonQueryCommandStub.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using System.Data.Common; 3 | 4 | namespace Projac.Sql.Tests.Framework 5 | { 6 | public class SqlNonQueryCommandStub : SqlNonQueryCommand 7 | { 8 | public SqlNonQueryCommandStub(string text, DbParameter[] parameters, CommandType type) : base(text, parameters, type) 9 | { 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/Projac.Sql.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.Sql.Tests 5 | Projac.Sql.Tests 6 | netcoreapp2.0 7 | netcoreapp2.0;net46;net452 8 | AnyCPU 9 | Projac.Sql.Tests 10 | true 11 | false 12 | 13 | 14 | 0.0.0 15 | 0.0.0 16 | 0.0.0 17 | 18 | 19 | False 20 | False 21 | Off 22 | 23 | 24 | False 25 | True 26 | Off 27 | 28 | 29 | false 30 | Projac.Sql.Tests 31 | Yves Reynhout 32 | BitTacklr 33 | Projac 34 | Projac.Sql.Tests unit tests Projac.Sql. 35 | Copyright © BitTacklr 2017. All rights reserved. 36 | en 37 | 38 | 39 | 40 | 41 | 42 | portable 43 | 44 | 45 | full 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/ResolveTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace Projac.Sql.Tests 5 | { 6 | [TestFixture] 7 | public class ResolveTests 8 | { 9 | [Test] 10 | public void WhenEqualToHandlerMessageTypeHandlersCanNotBeNull() 11 | { 12 | Assert.Throws(() => 13 | Resolve.WhenEqualToHandlerMessageType(null)); 14 | } 15 | 16 | [Test] 17 | public void WhenEqualToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 18 | { 19 | var sut = Resolve.WhenEqualToHandlerMessageType(new SqlProjectionHandler[0]); 20 | Assert.Throws(() => sut(null)); 21 | } 22 | 23 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenEqualToHandlerMessageTypeCases")] 24 | public void WhenEqualToHandlerMessageTypeResolverReturnsExpectedResult( 25 | SqlProjectionHandler[] resolvable, 26 | object message, 27 | SqlProjectionHandler[] resolved) 28 | { 29 | var sut = Resolve.WhenEqualToHandlerMessageType(resolvable); 30 | var result = sut(message); 31 | Assert.That(result, Is.EquivalentTo(resolved)); 32 | } 33 | 34 | [Test] 35 | public void WhenAssignableToHandlerMessageTypeHandlersCanNotBeNull() 36 | { 37 | Assert.Throws(() => 38 | Resolve.WhenAssignableToHandlerMessageType(null)); 39 | } 40 | 41 | [Test] 42 | public void WhenAssignableToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 43 | { 44 | var sut = Resolve.WhenAssignableToHandlerMessageType(new SqlProjectionHandler[0]); 45 | Assert.Throws(() => sut(null)); 46 | } 47 | 48 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenAssignableToHandlerMessageTypeCases")] 49 | public void WhenAssignableToHandlerMessageTypeResolverReturnsExpectedResult( 50 | SqlProjectionHandler[] resolvable, 51 | object message, 52 | SqlProjectionHandler[] resolved) 53 | { 54 | var sut = Resolve.WhenAssignableToHandlerMessageType(resolvable); 55 | var result = sut(message); 56 | Assert.That(result, Is.EquivalentTo(resolved)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/SqlNonQueryCommandTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using NUnit.Framework; 5 | 6 | namespace Projac.Sql.Tests 7 | { 8 | [TestFixture] 9 | public class SqlNonQueryCommandTests 10 | { 11 | [Test] 12 | public void TextCanNotBeNull() 13 | { 14 | Assert.Throws(() => SutFactory((string)null)); 15 | } 16 | 17 | [Test] 18 | public void TextCanBeEmpty() 19 | { 20 | Assert.DoesNotThrow(() => SutFactory("")); 21 | } 22 | 23 | [Test] 24 | public void ParametersCanNotBeNull() 25 | { 26 | Assert.Throws(() => SutFactory((DbParameter[])null)); 27 | } 28 | 29 | [Test] 30 | public void TypeCanNotBeUnsupportedEnumValue() 31 | { 32 | Assert.Throws(() => SutFactory((CommandType) Int32.MinValue)); 33 | } 34 | 35 | [TestCase(CommandType.StoredProcedure)] 36 | [TestCase(CommandType.TableDirect)] 37 | [TestCase(CommandType.Text)] 38 | public void TypeCanBeUnsupportedEnumValue(CommandType type) 39 | { 40 | Assert.DoesNotThrow(() => SutFactory(type)); 41 | } 42 | 43 | [Test] 44 | public void ParametersCanBeEmpty() 45 | { 46 | Assert.DoesNotThrow(() => SutFactory(new DbParameter[0])); 47 | } 48 | 49 | [Test] 50 | public void PropertiesReturnExpectedValues() 51 | { 52 | var sut = SutFactory("text", new DbParameter[0], CommandType.TableDirect); 53 | 54 | Assert.That(sut.Text, Is.EqualTo("text")); 55 | Assert.That(sut.Parameters, Is.EqualTo(new DbParameter[0])); 56 | Assert.That(sut.Type, Is.EqualTo(CommandType.TableDirect)); 57 | } 58 | 59 | private static SqlNonQueryCommand SutFactory(string text) 60 | { 61 | return SutFactory(text, new DbParameter[0], CommandType.Text); 62 | } 63 | 64 | private static SqlNonQueryCommand SutFactory(DbParameter[] parameters) 65 | { 66 | return SutFactory("text", parameters, CommandType.Text); 67 | } 68 | 69 | private static SqlNonQueryCommand SutFactory(CommandType type) 70 | { 71 | return SutFactory("text", new DbParameter[0], type); 72 | } 73 | 74 | private static SqlNonQueryCommand SutFactory(string text, DbParameter[] parameters, CommandType type) 75 | { 76 | return new SqlNonQueryCommand(text, parameters, type); 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/SqlProjectionHandlerTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using NUnit.Framework; 4 | 5 | namespace Projac.Sql.Tests 6 | { 7 | [TestFixture] 8 | public class SqlProjectionHandlerTests 9 | { 10 | [Test] 11 | public void MessageCanNotBeNull() 12 | { 13 | Assert.Throws( 14 | () => new SqlProjectionHandler(null, _ => new SqlNonQueryCommand[0]) 15 | ); 16 | } 17 | 18 | [Test] 19 | public void HandlerCanNotBeNull() 20 | { 21 | Assert.Throws( 22 | () => new SqlProjectionHandler(typeof(object), null) 23 | ); 24 | } 25 | 26 | [Test] 27 | public void ParametersArePreservedAsProperties() 28 | { 29 | var message = typeof(object); 30 | Func> handler = _ => new SqlNonQueryCommand[0]; 31 | 32 | var sut = new SqlProjectionHandler(message, handler); 33 | 34 | Assert.That(sut.Message, Is.EqualTo(message)); 35 | Assert.That(sut.Handler, Is.EqualTo(handler)); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Projac.Sql.Tests/SqlQueryStatementTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using NUnit.Framework; 5 | 6 | namespace Projac.Sql.Tests 7 | { 8 | [TestFixture] 9 | public class SqlQueryCommandTests 10 | { 11 | [Test] 12 | public void TextCanNotBeNull() 13 | { 14 | Assert.Throws(() => SutFactory((string)null)); 15 | } 16 | 17 | [Test] 18 | public void TextCanBeEmpty() 19 | { 20 | Assert.DoesNotThrow(() => SutFactory("")); 21 | } 22 | 23 | [Test] 24 | public void ParametersCanNotBeNull() 25 | { 26 | Assert.Throws(() => SutFactory((DbParameter[])null)); 27 | } 28 | 29 | [Test] 30 | public void TypeCanNotBeUnsupportedEnumValue() 31 | { 32 | Assert.Throws(() => SutFactory((CommandType)Int32.MinValue)); 33 | } 34 | 35 | [TestCase(CommandType.StoredProcedure)] 36 | [TestCase(CommandType.TableDirect)] 37 | [TestCase(CommandType.Text)] 38 | public void TypeCanBeUnsupportedEnumValue(CommandType type) 39 | { 40 | Assert.DoesNotThrow(() => SutFactory(type)); 41 | } 42 | 43 | [Test] 44 | public void ParametersCanBeEmpty() 45 | { 46 | Assert.DoesNotThrow(() => SutFactory(new DbParameter[0])); 47 | } 48 | 49 | [Test] 50 | public void PropertiesReturnExpectedValues() 51 | { 52 | var sut = SutFactory("text", new DbParameter[0], CommandType.TableDirect); 53 | 54 | Assert.That(sut.Text, Is.EqualTo("text")); 55 | Assert.That(sut.Parameters, Is.EqualTo(new DbParameter[0])); 56 | Assert.That(sut.Type, Is.EqualTo(CommandType.TableDirect)); 57 | } 58 | 59 | private static SqlQueryCommand SutFactory(string text) 60 | { 61 | return SutFactory(text, new DbParameter[0], CommandType.Text); 62 | } 63 | 64 | private static SqlQueryCommand SutFactory(DbParameter[] parameters) 65 | { 66 | return SutFactory("text", parameters, CommandType.Text); 67 | } 68 | 69 | private static SqlQueryCommand SutFactory(CommandType type) 70 | { 71 | return SutFactory("text", new DbParameter[0], type); 72 | } 73 | 74 | private static SqlQueryCommand SutFactory(string text, DbParameter[] parameters, CommandType type) 75 | { 76 | return new SqlQueryCommand(text, parameters, type); 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /src/Projac.Sql/AnonymousSqlProjection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Projac.Sql 6 | { 7 | /// 8 | /// Represent an anonymous SQL projection. 9 | /// 10 | public class AnonymousSqlProjection : IEnumerable 11 | { 12 | private readonly SqlProjectionHandler[] _handlers; 13 | 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// The handlers associated with this projection. 18 | /// Thrown when are null. 19 | public AnonymousSqlProjection(SqlProjectionHandler[] handlers) 20 | { 21 | if (handlers == null) throw new ArgumentNullException("handlers"); 22 | _handlers = handlers; 23 | } 24 | 25 | /// 26 | /// Gets a read only collection of projection handlers. 27 | /// 28 | /// 29 | /// The projection handlers associated with this projection. 30 | /// 31 | public SqlProjectionHandler[] Handlers 32 | { 33 | get { return _handlers; } 34 | } 35 | 36 | /// 37 | /// Performs an implicit conversion from to SqlProjectionHandler[]. 38 | /// 39 | /// The instance. 40 | /// 41 | /// The result of the conversion. 42 | /// 43 | public static implicit operator SqlProjectionHandler[](AnonymousSqlProjection instance) 44 | { 45 | return instance.Handlers; 46 | } 47 | 48 | /// 49 | /// Returns an enumerator that iterates through a copy of the handlers. 50 | /// 51 | /// 52 | /// An that can be used to iterate through a copy of the handlers. 53 | /// 54 | public SqlProjectionHandlerEnumerator GetEnumerator() 55 | { 56 | return new SqlProjectionHandlerEnumerator(Handlers); 57 | } 58 | 59 | /// 60 | /// Returns an enumerator that iterates through the collection. 61 | /// 62 | /// 63 | /// An that can be used to iterate through the collection. 64 | /// 65 | IEnumerator IEnumerable.GetEnumerator() 66 | { 67 | return GetEnumerator(); 68 | } 69 | 70 | /// 71 | /// Returns an enumerator that iterates through a collection. 72 | /// 73 | /// 74 | /// An object that can be used to iterate through the collection. 75 | /// 76 | IEnumerator IEnumerable.GetEnumerator() 77 | { 78 | return GetEnumerator(); 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /src/Projac.Sql/ConcurrentResolve.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | 4 | namespace Projac.Sql 5 | { 6 | /// 7 | /// Represents the available concurrent resolvers. 8 | /// 9 | public static class ConcurrentResolve 10 | { 11 | /// 12 | /// Resolves the handlers that match the type of the message exactly. 13 | /// 14 | /// The set of resolvable handlers. 15 | /// A resolver. 16 | public static SqlProjectionHandlerResolver WhenEqualToHandlerMessageType(SqlProjectionHandler[] handlers) 17 | { 18 | return Resolve.WhenEqualToHandlerMessageType(handlers); 19 | } 20 | 21 | /// 22 | /// Resolves the handlers to which the message instance is assignable. 23 | /// 24 | /// The set of resolvable handlers. 25 | /// A resolver. 26 | public static SqlProjectionHandlerResolver WhenAssignableToHandlerMessageType(SqlProjectionHandler[] handlers) 27 | { 28 | if (handlers == null) 29 | throw new ArgumentNullException("handlers"); 30 | var cache = new ConcurrentDictionary(); 31 | return message => 32 | { 33 | if (message == null) 34 | throw new ArgumentNullException("message"); 35 | SqlProjectionHandler[] result; 36 | if (!cache.TryGetValue(message.GetType(), out result)) 37 | { 38 | result = cache.GetOrAdd(message.GetType(), 39 | Array.FindAll(handlers, 40 | handler => handler.Message.IsInstanceOfType(message))); 41 | } 42 | return result; 43 | }; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/Projac.Sql/Executors/IAsyncSqlNonQueryCommandExecutor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Projac.Sql.Executors 6 | { 7 | /// 8 | /// Represents the asynchronous execution of commands against a data 9 | /// source. 10 | /// 11 | public interface IAsyncSqlNonQueryCommandExecutor 12 | { 13 | /// 14 | /// Executes the specified command asynchronously. 15 | /// 16 | /// The command. 17 | /// 18 | /// A that will return when the command 19 | /// was executed. 20 | /// 21 | /// Thrown when is null. 22 | Task ExecuteNonQueryAsync(SqlNonQueryCommand command); 23 | 24 | /// 25 | /// Executes the specified command. 26 | /// 27 | /// The command. 28 | /// The cancellation token. 29 | /// 30 | /// A that will return when the command 31 | /// was executed. 32 | /// 33 | /// Thrown when is null. 34 | Task ExecuteNonQueryAsync(SqlNonQueryCommand command, CancellationToken cancellationToken); 35 | 36 | /// 37 | /// Executes the specified commands asynchronously. 38 | /// 39 | /// The commands to execute. 40 | /// 41 | /// A that will return the number of commands 42 | /// executed. 43 | /// 44 | /// Thrown when are null. 45 | Task ExecuteNonQueryAsync(IEnumerable commands); 46 | 47 | /// 48 | /// Executes the specified commands asynchronously. 49 | /// 50 | /// The commands. 51 | /// The cancellation token. 52 | /// 53 | /// A that will return the number of commands 54 | /// executed. 55 | /// 56 | /// Thrown when are null. 57 | Task ExecuteNonQueryAsync(IEnumerable commands, CancellationToken cancellationToken); 58 | } 59 | } -------------------------------------------------------------------------------- /src/Projac.Sql/Executors/IAsyncSqlQueryCommandExecutor.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Common; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Projac.Sql.Executors 6 | { 7 | /// 8 | /// Represents the synchronous execution of a command against a data 9 | /// source. 10 | /// 11 | public interface IAsyncSqlQueryCommandExecutor 12 | { 13 | /// 14 | /// Executes the specified command asynchronously. 15 | /// 16 | /// The command. 17 | /// 18 | /// A that will return a data reader. 19 | /// 20 | /// Thrown when is null. 21 | Task ExecuteReaderAsync(SqlQueryCommand command); 22 | 23 | /// 24 | /// Executes the specified command asynchronously. 25 | /// 26 | /// The command. 27 | /// The cancellation token. 28 | /// 29 | /// A that will return a data reader. 30 | /// 31 | /// Thrown when is null. 32 | Task ExecuteReaderAsync(SqlQueryCommand command, CancellationToken cancellationToken); 33 | 34 | /// 35 | /// Executes the specified command asynchronously. 36 | /// 37 | /// The command. 38 | /// 39 | /// A that will return a scalar value. 40 | /// 41 | /// Thrown when is null. 42 | Task ExecuteScalarAsync(SqlQueryCommand command); 43 | 44 | /// 45 | /// Executes the specified command asynchronously. 46 | /// 47 | /// The command. 48 | /// The cancellation token. 49 | /// 50 | /// A that will return a scalar value. 51 | /// 52 | /// Thrown when is null. 53 | Task ExecuteScalarAsync(SqlQueryCommand command, CancellationToken cancellationToken); 54 | } 55 | } -------------------------------------------------------------------------------- /src/Projac.Sql/Executors/ISqlNonQueryCommandExecutor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Projac.Sql.Executors 4 | { 5 | /// 6 | /// Represents the synchronous execution of commands against a data 7 | /// source. 8 | /// 9 | public interface ISqlNonQueryCommandExecutor 10 | { 11 | /// 12 | /// Executes the specified command. 13 | /// 14 | /// The command. 15 | /// The number of commands executed. 16 | /// Thrown when is null. 17 | void ExecuteNonQuery(SqlNonQueryCommand command); 18 | 19 | /// 20 | /// Executes the specified commands. 21 | /// 22 | /// The commands. 23 | /// The number of commands executed. 24 | /// Thrown when are null. 25 | int ExecuteNonQuery(IEnumerable commands); 26 | } 27 | } -------------------------------------------------------------------------------- /src/Projac.Sql/Executors/ISqlQueryCommandExecutor.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Common; 2 | 3 | namespace Projac.Sql.Executors 4 | { 5 | /// 6 | /// Represents the synchronous execution of a command against a data 7 | /// source. 8 | /// 9 | public interface ISqlQueryCommandExecutor 10 | { 11 | /// 12 | /// Executes the specified command. 13 | /// 14 | /// The command. 15 | /// A . 16 | /// Thrown when is null. 17 | DbDataReader ExecuteReader(SqlQueryCommand command); 18 | 19 | /// 20 | /// Executes the specified command. 21 | /// 22 | /// The command. 23 | /// A scalar value. 24 | /// Thrown when is null. 25 | object ExecuteScalar(SqlQueryCommand command); 26 | } 27 | } -------------------------------------------------------------------------------- /src/Projac.Sql/IDbParameterValue.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Common; 2 | 3 | namespace Projac.Sql 4 | { 5 | /// 6 | /// Represents a database parameter value that can be converted to a . 7 | /// 8 | public interface IDbParameterValue 9 | { 10 | /// 11 | /// Creates a instance based on this instance. 12 | /// 13 | /// The name of the parameter. 14 | /// A . 15 | DbParameter ToDbParameter(string parameterName); 16 | } 17 | } -------------------------------------------------------------------------------- /src/Projac.Sql/Projac.Sql.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.Sql 5 | Projac.Sql 6 | netstandard2.0 7 | netstandard2.0;net46;net452 8 | AnyCPU 9 | Projac.Sql 10 | true 11 | 12 | 13 | 0.0.0 14 | 0.0.0 15 | 0.0.0 16 | 17 | 18 | False 19 | False 20 | true 21 | Off 22 | 23 | 24 | False 25 | True 26 | true 27 | Off 28 | 29 | 30 | true 31 | 0.0.0 32 | Projac.Sql 33 | Yves Reynhout 34 | Projac.Sql 35 | Projac.Sql 36 | Yves Reynhout 37 | BitTacklr 38 | Projac 39 | Projac.Sql provides lightweight infrastructure for authoring SQL projections. 40 | Copyright © BitTacklr 2017. All rights reserved. 41 | en 42 | false 43 | https://github.com/yreynhout/Projac/blob/master/LICENSE 44 | https://github.com/yreynhout/Projac 45 | Please visit https://github.com/BitTacklr/Projac/wiki/Changes$(PackageVersion) . 46 | command and query responsibility segregation;cqrs;event sourcing;es;domain driven design;ddd 47 | true 48 | true 49 | false 50 | https://github.com/yreynhout/Projac 51 | git 52 | true 53 | false 54 | 55 | 56 | portable 57 | 58 | 59 | full 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/Projac.Sql/Resolve.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Projac.Sql 6 | { 7 | /// 8 | /// Represents the available resolvers. 9 | /// 10 | public static class Resolve 11 | { 12 | /// 13 | /// Resolves the handlers that match the type of the message exactly. 14 | /// 15 | /// The set of resolvable handlers. 16 | /// A resolver. 17 | public static SqlProjectionHandlerResolver WhenEqualToHandlerMessageType(SqlProjectionHandler[] handlers) 18 | { 19 | if (handlers == null) 20 | throw new ArgumentNullException("handlers"); 21 | var cache = handlers. 22 | GroupBy(handler => handler.Message). 23 | ToDictionary(@group => @group.Key, @group => @group.ToArray()); 24 | return message => 25 | { 26 | if(message == null) 27 | throw new ArgumentNullException("message"); 28 | SqlProjectionHandler[] result; 29 | return cache.TryGetValue(message.GetType(), out result) ? 30 | result : 31 | new SqlProjectionHandler[0]; 32 | }; 33 | } 34 | 35 | /// 36 | /// Resolves the handlers to which the message instance is assignable. 37 | /// 38 | /// The set of resolvable handlers. 39 | /// A resolver. 40 | public static SqlProjectionHandlerResolver WhenAssignableToHandlerMessageType(SqlProjectionHandler[] handlers) 41 | { 42 | if (handlers == null) 43 | throw new ArgumentNullException("handlers"); 44 | var cache = new Dictionary(); 45 | return message => 46 | { 47 | if (message == null) 48 | throw new ArgumentNullException("message"); 49 | SqlProjectionHandler[] result; 50 | if (!cache.TryGetValue(message.GetType(), out result)) 51 | { 52 | result = Array.FindAll(handlers, 53 | handler => handler.Message.IsInstanceOfType(message)); 54 | cache.Add(message.GetType(), result); 55 | } 56 | return result; 57 | }; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Projac.Sql/SqlNonQueryCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | 5 | namespace Projac.Sql 6 | { 7 | /// 8 | /// Represent a SQL non query command. 9 | /// 10 | public class SqlNonQueryCommand 11 | { 12 | private readonly DbParameter[] _parameters; 13 | private readonly string _text; 14 | private readonly CommandType _type; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// The text. 20 | /// The parameters. 21 | /// The type of command. 22 | /// 23 | /// Thrown when or 24 | /// is null. 25 | /// 26 | public SqlNonQueryCommand(string text, DbParameter[] parameters, CommandType type) 27 | { 28 | if (text == null) 29 | throw new ArgumentNullException("text"); 30 | if (parameters == null) 31 | throw new ArgumentNullException("parameters"); 32 | if (!Enum.IsDefined(typeof (CommandType), type)) 33 | throw new ArgumentException(string.Format("The command type value {0} is not supported.", type), "type"); 34 | _text = text; 35 | _parameters = parameters; 36 | _type = type; 37 | } 38 | 39 | /// 40 | /// Gets the text. 41 | /// 42 | /// 43 | /// The text. 44 | /// 45 | public string Text 46 | { 47 | get { return _text; } 48 | } 49 | 50 | /// 51 | /// Gets the parameters. 52 | /// 53 | /// 54 | /// The parameters. 55 | /// 56 | public DbParameter[] Parameters 57 | { 58 | get { return _parameters; } 59 | } 60 | 61 | /// 62 | /// Gets the type of command. 63 | /// 64 | /// 65 | /// The type of command. 66 | /// 67 | public CommandType Type 68 | { 69 | get { return _type; } 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /src/Projac.Sql/SqlProjectionHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Projac.Sql 5 | { 6 | /// 7 | /// Represents a handler of a particular type of message. 8 | /// 9 | public class SqlProjectionHandler 10 | { 11 | private readonly Type _message; 12 | private readonly Func> _handler; 13 | 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// The message. 18 | /// The handler. 19 | /// 20 | /// Throw when or is 21 | /// null. 22 | /// 23 | public SqlProjectionHandler(Type message, Func> handler) 24 | { 25 | if (message == null) throw new ArgumentNullException("message"); 26 | if (handler == null) throw new ArgumentNullException("handler"); 27 | _message = message; 28 | _handler = handler; 29 | } 30 | 31 | /// 32 | /// The type of message to handle. 33 | /// 34 | public Type Message 35 | { 36 | get { return _message; } 37 | } 38 | 39 | /// 40 | /// The function that handles the message. 41 | /// 42 | public Func> Handler 43 | { 44 | get { return _handler; } 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /src/Projac.Sql/SqlProjectionHandlerEnumerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Projac.Sql 6 | { 7 | /// 8 | /// Represents a array enumerator. 9 | /// 10 | public class SqlProjectionHandlerEnumerator : IEnumerator 11 | { 12 | private readonly SqlProjectionHandler[] _handlers; 13 | private int _index; 14 | 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// The handlers to enumerate. 19 | /// Thrown when are null. 20 | public SqlProjectionHandlerEnumerator(SqlProjectionHandler[] handlers) 21 | { 22 | if (handlers == null) throw new ArgumentNullException("handlers"); 23 | _handlers = handlers; 24 | _index = -1; 25 | } 26 | 27 | /// 28 | /// Advances the enumerator to the next element of the collection. 29 | /// 30 | /// 31 | /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. 32 | /// 33 | public bool MoveNext() 34 | { 35 | return _index < _handlers.Length && 36 | ++_index < _handlers.Length; 37 | } 38 | 39 | /// 40 | /// Sets the enumerator to its initial position, which is before the first element in the collection. 41 | /// 42 | public void Reset() 43 | { 44 | _index = -1; 45 | } 46 | 47 | /// 48 | /// Gets the element in the collection at the current position of the enumerator. 49 | /// 50 | /// 51 | /// Enumeration has not started. Call MoveNext. 52 | /// or 53 | /// Enumeration has already ended. Call Reset. 54 | /// 55 | public SqlProjectionHandler Current 56 | { 57 | get 58 | { 59 | if (_index == -1) 60 | throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); 61 | if (_index == _handlers.Length) 62 | throw new InvalidOperationException("Enumeration has already ended. Call Reset."); 63 | 64 | return _handlers[_index]; 65 | } 66 | } 67 | 68 | /// 69 | /// Gets the element in the collection at the current position of the enumerator. 70 | /// 71 | object IEnumerator.Current 72 | { 73 | get { return Current; } 74 | } 75 | 76 | /// 77 | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 78 | /// 79 | public void Dispose() 80 | { 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /src/Projac.Sql/SqlProjectionHandlerResolver.cs: -------------------------------------------------------------------------------- 1 | namespace Projac.Sql 2 | { 3 | /// 4 | /// Resolves the handlers that match the specified . 5 | /// 6 | /// The message to resolve handlers for. 7 | /// The set of matching handlers. 8 | public delegate SqlProjectionHandler[] SqlProjectionHandlerResolver(object message); 9 | } -------------------------------------------------------------------------------- /src/Projac.Sql/SqlProjector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Projac.Sql.Executors; 5 | 6 | namespace Projac.Sql 7 | { 8 | /// 9 | /// Projects a single message or a set of messages in a synchronous manner to the matching handlers. 10 | /// 11 | public class SqlProjector 12 | { 13 | private readonly SqlProjectionHandlerResolver _resolver; 14 | private readonly ISqlNonQueryCommandExecutor _executor; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// The handler resolver. 20 | /// The command executor. 21 | /// Thrown when or is null. 22 | public SqlProjector(SqlProjectionHandlerResolver resolver, ISqlNonQueryCommandExecutor executor) 23 | { 24 | if (resolver == null) throw new ArgumentNullException("resolver"); 25 | if (executor == null) throw new ArgumentNullException("executor"); 26 | 27 | _resolver = resolver; 28 | _executor = executor; 29 | } 30 | 31 | /// 32 | /// Projects the specified message. 33 | /// 34 | /// The message to project. 35 | /// The number of commands executed. 36 | /// Thrown when is null. 37 | public int Project(object message) 38 | { 39 | if (message == null) throw new ArgumentNullException("message"); 40 | 41 | return _executor. 42 | ExecuteNonQuery( 43 | from handler in _resolver(message) 44 | from statement in handler.Handler(message) 45 | select statement); 46 | } 47 | 48 | /// 49 | /// Projects the specified messages. 50 | /// 51 | /// The messages to project. 52 | /// The number of commands executed. 53 | /// Thrown when are null. 54 | public int Project(IEnumerable messages) 55 | { 56 | if (messages == null) 57 | throw new ArgumentNullException("messages"); 58 | 59 | return _executor. 60 | ExecuteNonQuery( 61 | from message in messages 62 | from handler in _resolver(message) 63 | from statement in handler.Handler(message) 64 | select statement); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Projac.Sql/SqlQueryCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | 5 | namespace Projac.Sql 6 | { 7 | /// 8 | /// Represent a SQL query command. 9 | /// 10 | public class SqlQueryCommand 11 | { 12 | private readonly DbParameter[] _parameters; 13 | private readonly string _text; 14 | private readonly CommandType _type; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// The text. 20 | /// The parameters. 21 | /// The type of command. 22 | /// 23 | /// Thrown when or 24 | /// is null. 25 | /// 26 | public SqlQueryCommand(string text, DbParameter[] parameters, CommandType type) 27 | { 28 | if (text == null) 29 | throw new ArgumentNullException("text"); 30 | if (parameters == null) 31 | throw new ArgumentNullException("parameters"); 32 | if (!Enum.IsDefined(typeof(CommandType), type)) 33 | throw new ArgumentException(string.Format("The command type value {0} is not supported.", type), "type"); 34 | _text = text; 35 | _parameters = parameters; 36 | _type = type; 37 | } 38 | 39 | /// 40 | /// Gets the text. 41 | /// 42 | /// 43 | /// The text. 44 | /// 45 | public string Text 46 | { 47 | get { return _text; } 48 | } 49 | 50 | /// 51 | /// Gets the parameters. 52 | /// 53 | /// 54 | /// The parameters. 55 | /// 56 | public DbParameter[] Parameters 57 | { 58 | get { return _parameters; } 59 | } 60 | 61 | /// 62 | /// Gets the type of command. 63 | /// 64 | /// 65 | /// The type of command. 66 | /// 67 | public CommandType Type 68 | { 69 | get { return _type; } 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/Projac.SqlClient.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.SqlClient.Tests 5 | Projac.SqlClient.Tests 6 | netcoreapp2.0 7 | netcoreapp2.0;net46;net452 8 | AnyCPU 9 | Projac.SqlClient.Tests 10 | true 11 | false 12 | 13 | 14 | 0.0.0 15 | 0.0.0 16 | 0.0.0 17 | 18 | 19 | False 20 | False 21 | Off 22 | 23 | 24 | False 25 | True 26 | Off 27 | 28 | 29 | false 30 | Projac.SqlClient.Tests 31 | Yves Reynhout 32 | BitTacklr 33 | Projac 34 | Projac.SqlClient.Tests unit tests Projac.SqlClient. 35 | Copyright © BitTacklr 2017. All rights reserved. 36 | en 37 | 38 | CS0618 39 | 40 | 41 | 42 | 43 | 44 | 45 | portable 46 | 47 | 48 | full 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/SqlClientSyntaxTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Projac.SqlClient; 3 | 4 | namespace Projac.Sql.Tests.SqlClient 5 | { 6 | [TestFixture] 7 | public partial class SqlClientSyntaxTests 8 | { 9 | private SqlClientSyntax _syntax; 10 | 11 | [SetUp] 12 | public void SetUp() 13 | { 14 | _syntax = new SqlClientSyntax(); 15 | } 16 | 17 | private SqlClientSyntax Sql { get { return _syntax; } } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/SqlParameterAssertions.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using System.Data.Common; 3 | using System.Data.SqlClient; 4 | using NUnit.Framework; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | internal static class SqlParameterAssertions 9 | { 10 | public static void ExpectSqlParameter(this DbParameter parameter, 11 | string name, 12 | SqlDbType sqlDbType, 13 | object value, 14 | bool nullable = false, 15 | int size = 0, 16 | int precision = 0, 17 | int scale = 0) 18 | { 19 | Assert.That(parameter, Is.Not.Null); 20 | Assert.That(parameter, Is.InstanceOf()); 21 | 22 | ((SqlParameter)parameter).ExpectSqlParameter(name, sqlDbType, value, nullable, size, precision, scale); 23 | } 24 | 25 | public static void ExpectSqlParameter(this SqlParameter parameter, 26 | string name, 27 | SqlDbType sqlDbType, 28 | object value, 29 | bool nullable = false, 30 | int size = 0, 31 | int precision = 0, 32 | int scale = 0) 33 | { 34 | Assert.That(parameter, Is.Not.Null); 35 | 36 | Assert.That(parameter.ParameterName, Is.EqualTo(name)); 37 | Assert.That(parameter.Direction, Is.EqualTo(ParameterDirection.Input)); 38 | Assert.That(parameter.SourceColumn, Is.EqualTo("")); 39 | Assert.That(parameter.SourceColumnNullMapping, Is.False); 40 | Assert.That(parameter.SourceVersion, Is.EqualTo(DataRowVersion.Default)); 41 | Assert.That(parameter.Value, Is.EqualTo(value)); 42 | Assert.That(parameter.Size, Is.EqualTo(size)); 43 | Assert.That(parameter.IsNullable, Is.EqualTo(nullable)); 44 | Assert.That(parameter.LocaleId, Is.EqualTo(0)); 45 | Assert.That(parameter.SqlDbType, Is.EqualTo(sqlDbType)); 46 | Assert.That(parameter.Precision, Is.EqualTo(precision)); 47 | Assert.That(parameter.Scale, Is.EqualTo(scale)); 48 | Assert.That(parameter.Offset, Is.EqualTo(0)); 49 | Assert.That(parameter.XmlSchemaCollectionDatabase, Is.EqualTo("")); 50 | Assert.That(parameter.XmlSchemaCollectionName, Is.EqualTo("")); 51 | Assert.That(parameter.XmlSchemaCollectionOwningSchema, Is.EqualTo("")); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/SqlParameterEqualityComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.SqlClient; 3 | 4 | namespace Projac.Sql.Tests.SqlClient 5 | { 6 | internal class SqlParameterEqualityComparer : IEqualityComparer 7 | { 8 | public bool Equals(SqlParameter x, SqlParameter y) 9 | { 10 | if (x == null && y == null) return true; 11 | if (x != null && y != null) 12 | { 13 | return Equals(x.ParameterName, y.ParameterName) && 14 | Equals(x.Direction, y.Direction) && 15 | Equals(x.LocaleId, y.LocaleId) && 16 | Equals(x.Value, y.Value) && 17 | Equals(x.IsNullable, y.IsNullable) && 18 | Equals(x.SqlDbType, y.SqlDbType) && 19 | Equals(x.Size, y.Size) && 20 | Equals(x.Precision, y.Precision) && 21 | Equals(x.Scale, y.Scale) && 22 | Equals(x.Offset, y.Offset) && 23 | Equals(x.SourceColumn, y.SourceColumn) && 24 | Equals(x.SourceColumnNullMapping, y.SourceColumnNullMapping) && 25 | Equals(x.SourceVersion, y.SourceVersion) && 26 | Equals(x.XmlSchemaCollectionDatabase, y.XmlSchemaCollectionDatabase) && 27 | Equals(x.XmlSchemaCollectionName, y.XmlSchemaCollectionName) && 28 | Equals(x.XmlSchemaCollectionOwningSchema, y.XmlSchemaCollectionOwningSchema); 29 | } 30 | return false; 31 | } 32 | 33 | public int GetHashCode(SqlParameter obj) 34 | { 35 | if (obj == null) 36 | return 0; 37 | return obj.ParameterName.GetHashCode() ^ 38 | obj.Direction.GetHashCode() ^ 39 | obj.LocaleId.GetHashCode() ^ 40 | (obj.Value == null ? 0 : obj.Value.GetHashCode()) ^ 41 | obj.IsNullable.GetHashCode() ^ 42 | obj.SqlDbType.GetHashCode() ^ 43 | obj.Size.GetHashCode() ^ 44 | obj.Precision.GetHashCode() ^ 45 | obj.Scale.GetHashCode() ^ 46 | obj.Offset.GetHashCode() ^ 47 | obj.SourceColumn.GetHashCode() ^ 48 | obj.SourceColumnNullMapping.GetHashCode() ^ 49 | obj.SourceVersion.GetHashCode() ^ 50 | obj.XmlSchemaCollectionDatabase.GetHashCode() ^ 51 | obj.XmlSchemaCollectionName.GetHashCode() ^ 52 | obj.XmlSchemaCollectionOwningSchema.GetHashCode(); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/SqlParameterValueStub.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using System.Data.SqlClient; 4 | 5 | namespace Projac.Sql.Tests.SqlClient 6 | { 7 | internal class SqlParameterValueStub : IDbParameterValue 8 | { 9 | public DbParameter ToDbParameter(string parameterName) 10 | { 11 | return new SqlParameter { ParameterName = parameterName, Value = DBNull.Value }; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlBigIntNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlBigIntNullValueTests 10 | { 11 | private TSqlBigIntNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlBigIntNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlBigIntNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.BigInt, DBNull.Value, true, 8); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.BigInt, DBNull.Value, true, 8); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlBigIntNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlBigIntValueTests.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using NUnit.Framework; 3 | using Projac.SqlClient; 4 | 5 | namespace Projac.Sql.Tests.SqlClient 6 | { 7 | [TestFixture] 8 | public class TSqlBigIntValueTests 9 | { 10 | [Test] 11 | public void IsSqlParameterValue() 12 | { 13 | var sut = SutFactory(); 14 | 15 | Assert.That(sut, Is.InstanceOf()); 16 | } 17 | 18 | [Test] 19 | public void ToDbParameterReturnsExpectedInstance() 20 | { 21 | const string parameterName = "name"; 22 | 23 | var sut = SutFactory(123); 24 | 25 | var result = sut.ToDbParameter(parameterName); 26 | 27 | result.ExpectSqlParameter(parameterName, SqlDbType.BigInt, 123, false, 8); 28 | } 29 | 30 | [Test] 31 | public void ToSqlParameterReturnsExpectedInstance() 32 | { 33 | const string parameterName = "name"; 34 | 35 | var sut = SutFactory(123); 36 | 37 | var result = sut.ToSqlParameter(parameterName); 38 | 39 | result.ExpectSqlParameter(parameterName, SqlDbType.BigInt, 123, false, 8); 40 | } 41 | 42 | [Test] 43 | public void DoesEqualItself() 44 | { 45 | var sut = SutFactory(); 46 | var instance = sut; 47 | Assert.That(sut.Equals(instance), Is.True); 48 | } 49 | 50 | [Test] 51 | public void DoesNotEqualOtherObjectType() 52 | { 53 | var sut = SutFactory(); 54 | Assert.That(sut.Equals(new object()), Is.False); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualNull() 59 | { 60 | var sut = SutFactory(); 61 | Assert.That(sut.Equals(null), Is.False); 62 | } 63 | 64 | [Test] 65 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 66 | { 67 | var sut = SutFactory(123); 68 | var other = SutFactory(123); 69 | Assert.That(sut.Equals(other), Is.True); 70 | } 71 | 72 | [Test] 73 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 74 | { 75 | var sut = SutFactory(123); 76 | var other = SutFactory(456); 77 | Assert.That(sut.Equals(other), Is.False); 78 | } 79 | 80 | [Test] 81 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 82 | { 83 | var sut = SutFactory(123); 84 | var other = SutFactory(123); 85 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 86 | } 87 | 88 | [Test] 89 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 90 | { 91 | var sut = SutFactory(123); 92 | var other = SutFactory(456); 93 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 94 | } 95 | 96 | private static TSqlBigIntValue SutFactory() 97 | { 98 | return SutFactory(123); 99 | } 100 | 101 | private static TSqlBigIntValue SutFactory(long value) 102 | { 103 | return new TSqlBigIntValue(value); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlBinaryNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlBinaryNullValueTests 10 | { 11 | private TSqlBinaryNullValue _sut; 12 | private TSqlBinarySize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlBinarySize(100); 18 | _sut = new TSqlBinaryNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.Binary, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.Binary, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlBitNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlBitNullValueTests 10 | { 11 | private TSqlBitNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlBitNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlBitNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.Bit, DBNull.Value, true, 1); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.Bit, DBNull.Value, true, 1); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlBitNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlBitValueTests.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using NUnit.Framework; 3 | using Projac.SqlClient; 4 | 5 | namespace Projac.Sql.Tests.SqlClient 6 | { 7 | [TestFixture] 8 | public class TSqlBitValueTests 9 | { 10 | [Test] 11 | public void IsSqlParameterValue() 12 | { 13 | var sut = SutFactory(); 14 | 15 | Assert.That(sut, Is.InstanceOf()); 16 | } 17 | 18 | [Test] 19 | public void ToDbParameterReturnsExpectedInstance() 20 | { 21 | const string parameterName = "name"; 22 | 23 | var sut = SutFactory(true); 24 | 25 | var result = sut.ToDbParameter(parameterName); 26 | 27 | result.ExpectSqlParameter(parameterName, SqlDbType.Bit, true, false, 1); 28 | } 29 | 30 | [Test] 31 | public void ToSqlParameterReturnsExpectedInstance() 32 | { 33 | const string parameterName = "name"; 34 | 35 | var sut = SutFactory(true); 36 | 37 | var result = sut.ToSqlParameter(parameterName); 38 | 39 | result.ExpectSqlParameter(parameterName, SqlDbType.Bit, true, false, 1); 40 | } 41 | 42 | [Test] 43 | public void DoesEqualItself() 44 | { 45 | var sut = SutFactory(); 46 | var instance = sut; 47 | Assert.That(sut.Equals(instance), Is.True); 48 | } 49 | 50 | [Test] 51 | public void DoesNotEqualOtherObjectType() 52 | { 53 | var sut = SutFactory(); 54 | Assert.That(sut.Equals(new object()), Is.False); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualNull() 59 | { 60 | var sut = SutFactory(); 61 | Assert.That(sut.Equals(null), Is.False); 62 | } 63 | 64 | [Test] 65 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 66 | { 67 | var sut = SutFactory(true); 68 | var other = SutFactory(true); 69 | Assert.That(sut.Equals(other), Is.True); 70 | } 71 | 72 | [Test] 73 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 74 | { 75 | var sut = SutFactory(true); 76 | var other = SutFactory(false); 77 | Assert.That(sut.Equals(other), Is.False); 78 | } 79 | 80 | [Test] 81 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 82 | { 83 | var sut = SutFactory(true); 84 | var other = SutFactory(true); 85 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 86 | } 87 | 88 | [Test] 89 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 90 | { 91 | var sut = SutFactory(true); 92 | var other = SutFactory(false); 93 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 94 | } 95 | 96 | private static TSqlBitValue SutFactory() 97 | { 98 | return SutFactory(true); 99 | } 100 | 101 | private static TSqlBitValue SutFactory(bool value) 102 | { 103 | return new TSqlBitValue(value); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlCharNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlCharNullValueTests 10 | { 11 | private TSqlCharNullValue _sut; 12 | private TSqlCharSize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlCharSize(100); 18 | _sut = new TSqlCharNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.Char, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.Char, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateNullValueTests 10 | { 11 | private TSqlDateNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlDateNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void IsSqlParameterValue() 21 | { 22 | Assert.That(_sut, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void ToDbParameterReturnsExpectedInstance() 27 | { 28 | const string parameterName = "name"; 29 | 30 | var result = _sut.ToDbParameter(parameterName); 31 | 32 | result.ExpectSqlParameter(parameterName, SqlDbType.Date, DBNull.Value, true); 33 | } 34 | 35 | [Test] 36 | public void ToSqlParameterReturnsExpectedInstance() 37 | { 38 | const string parameterName = "name"; 39 | 40 | var result = _sut.ToSqlParameter(parameterName); 41 | 42 | result.ExpectSqlParameter(parameterName, SqlDbType.Date, DBNull.Value, true); 43 | } 44 | 45 | [Test] 46 | public void DoesEqualItself() 47 | { 48 | var self = _sut; 49 | Assert.That(_sut.Equals(self), Is.True); 50 | } 51 | 52 | [Test] 53 | public void DoesNotEqualOtherObjectType() 54 | { 55 | Assert.That(_sut.Equals(new object()), Is.False); 56 | } 57 | 58 | [Test] 59 | public void DoesNotEqualNull() 60 | { 61 | Assert.That(_sut.Equals(null), Is.False); 62 | } 63 | 64 | [Test] 65 | public void HasExpectedHashCode() 66 | { 67 | var result = _sut.GetHashCode(); 68 | 69 | Assert.That(result, Is.EqualTo(0)); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateTime2NullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateTime2NullValueTests 10 | { 11 | private TSqlDateTime2NullValue _sut; 12 | private TSqlDateTime2Precision _precision; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _precision = new TSqlDateTime2Precision(4); 18 | _sut = new TSqlDateTime2NullValue(_precision); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime2, DBNull.Value, true, 8, 4); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime2, DBNull.Value, true, 8, 4); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(4)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateTimeNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateTimeNullValueTests 10 | { 11 | private TSqlDateTimeNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlDateTimeNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlDateTimeNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime, DBNull.Value, true, 8); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime, DBNull.Value, true, 8); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlDateTimeNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateTimeOffsetNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateTimeOffsetNullValueTests 10 | { 11 | private TSqlDateTimeOffsetNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlDateTimeOffsetNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlDateTimeOffsetNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTimeOffset, DBNull.Value, true, 7); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTimeOffset, DBNull.Value, true, 7); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlDateTimeOffsetNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateTimeValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateTimeValueTests 10 | { 11 | [Test] 12 | public void IsSqlParameterValue() 13 | { 14 | var sut = SutFactory(); 15 | 16 | Assert.That(sut, Is.InstanceOf()); 17 | } 18 | 19 | [Test] 20 | public void ToDbParameterReturnsExpectedInstance() 21 | { 22 | const string parameterName = "name"; 23 | 24 | var value = DateTime.UtcNow; 25 | var sut = SutFactory(value); 26 | 27 | var result = sut.ToDbParameter(parameterName); 28 | 29 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime, value, false, 8); 30 | } 31 | 32 | [Test] 33 | public void ToSqlParameterReturnsExpectedInstance() 34 | { 35 | const string parameterName = "name"; 36 | 37 | var value = DateTime.UtcNow; 38 | var sut = SutFactory(value); 39 | 40 | var result = sut.ToSqlParameter(parameterName); 41 | 42 | result.ExpectSqlParameter(parameterName, SqlDbType.DateTime, value, false, 8); 43 | } 44 | 45 | [Test] 46 | public void DoesEqualItself() 47 | { 48 | var sut = SutFactory(); 49 | var instance = sut; 50 | Assert.That(sut.Equals(instance), Is.True); 51 | } 52 | 53 | [Test] 54 | public void DoesNotEqualOtherObjectType() 55 | { 56 | var sut = SutFactory(); 57 | Assert.That(sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | var sut = SutFactory(); 64 | Assert.That(sut.Equals(null), Is.False); 65 | } 66 | 67 | [Test] 68 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 69 | { 70 | var value = DateTime.UtcNow; 71 | var sut = SutFactory(value); 72 | var other = SutFactory(value); 73 | Assert.That(sut.Equals(other), Is.True); 74 | } 75 | 76 | [Test] 77 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 78 | { 79 | var value1 = new DateTime(0); 80 | var value2 = new DateTime(1); 81 | var sut = SutFactory(value1); 82 | var other = SutFactory(value2); 83 | Assert.That(sut.Equals(other), Is.False); 84 | } 85 | 86 | [Test] 87 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 88 | { 89 | var value = DateTime.UtcNow; 90 | var sut = SutFactory(value); 91 | var other = SutFactory(value); 92 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 93 | } 94 | 95 | [Test] 96 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 97 | { 98 | var value1 = new DateTime(0); 99 | var value2 = new DateTime(1); 100 | var sut = SutFactory(value1); 101 | var other = SutFactory(value2); 102 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 103 | } 104 | 105 | private static TSqlDateTimeValue SutFactory() 106 | { 107 | return SutFactory(DateTime.UtcNow); 108 | } 109 | 110 | private static TSqlDateTimeValue SutFactory(DateTime value) 111 | { 112 | return new TSqlDateTimeValue(value); 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlDateValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlDateValueTests 10 | { 11 | [Test] 12 | public void IsSqlParameterValue() 13 | { 14 | var sut = SutFactory(); 15 | 16 | Assert.That(sut, Is.InstanceOf()); 17 | } 18 | 19 | [Test] 20 | public void ToDbParameterReturnsExpectedInstance() 21 | { 22 | const string parameterName = "name"; 23 | 24 | var value = DateTime.UtcNow; 25 | var sut = SutFactory(value); 26 | 27 | var result = sut.ToDbParameter(parameterName); 28 | 29 | result.ExpectSqlParameter(parameterName, SqlDbType.Date, value); 30 | } 31 | 32 | [Test] 33 | public void ToSqlParameterReturnsExpectedInstance() 34 | { 35 | const string parameterName = "name"; 36 | 37 | var value = DateTime.UtcNow; 38 | var sut = SutFactory(value); 39 | 40 | var result = sut.ToSqlParameter(parameterName); 41 | 42 | result.ExpectSqlParameter(parameterName, SqlDbType.Date, value); 43 | } 44 | 45 | [Test] 46 | public void DoesEqualItself() 47 | { 48 | var sut = SutFactory(); 49 | var instance = sut; 50 | Assert.That(sut.Equals(instance), Is.True); 51 | } 52 | 53 | [Test] 54 | public void DoesNotEqualOtherObjectType() 55 | { 56 | var sut = SutFactory(); 57 | Assert.That(sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | var sut = SutFactory(); 64 | Assert.That(sut.Equals(null), Is.False); 65 | } 66 | 67 | [Test] 68 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 69 | { 70 | var value = DateTime.UtcNow; 71 | var sut = SutFactory(value); 72 | var other = SutFactory(value); 73 | Assert.That(sut.Equals(other), Is.True); 74 | } 75 | 76 | [Test] 77 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 78 | { 79 | var value1 = new DateTime(0); 80 | var value2 = new DateTime(1); 81 | var sut = SutFactory(value1); 82 | var other = SutFactory(value2); 83 | Assert.That(sut.Equals(other), Is.False); 84 | } 85 | 86 | [Test] 87 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 88 | { 89 | var value = new DateTime(0); 90 | var sut = SutFactory(value); 91 | var other = SutFactory(value); 92 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 93 | } 94 | 95 | [Test] 96 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 97 | { 98 | var value1 = new DateTime(0); 99 | var value2 = new DateTime(1); 100 | var sut = SutFactory(value1); 101 | var other = SutFactory(value2); 102 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 103 | } 104 | 105 | private static TSqlDateValue SutFactory() 106 | { 107 | return SutFactory(DateTime.UtcNow); 108 | } 109 | 110 | private static TSqlDateValue SutFactory(DateTime value) 111 | { 112 | return new TSqlDateValue(value); 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlIntNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlIntNullValueTests 10 | { 11 | private TSqlIntNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlIntNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlIntNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.Int, DBNull.Value, true, 4); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.Int, DBNull.Value, true, 4); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlIntNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlIntValueTests.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using NUnit.Framework; 3 | using Projac.SqlClient; 4 | 5 | namespace Projac.Sql.Tests.SqlClient 6 | { 7 | [TestFixture] 8 | public class TSqlIntValueTests 9 | { 10 | [Test] 11 | public void IsSqlParameterValue() 12 | { 13 | var sut = SutFactory(); 14 | 15 | Assert.That(sut, Is.InstanceOf()); 16 | } 17 | 18 | [Test] 19 | public void ToDbParameterReturnsExpectedInstance() 20 | { 21 | const string parameterName = "name"; 22 | 23 | var sut = SutFactory(123); 24 | 25 | var result = sut.ToDbParameter(parameterName); 26 | 27 | result.ExpectSqlParameter(parameterName, SqlDbType.Int, 123, false, 4); 28 | } 29 | 30 | [Test] 31 | public void ToSqlParameterReturnsExpectedInstance() 32 | { 33 | const string parameterName = "name"; 34 | 35 | var sut = SutFactory(123); 36 | 37 | var result = sut.ToSqlParameter(parameterName); 38 | 39 | result.ExpectSqlParameter(parameterName, SqlDbType.Int, 123, false, 4); 40 | } 41 | 42 | [Test] 43 | public void DoesEqualItself() 44 | { 45 | var sut = SutFactory(); 46 | var instance = sut; 47 | Assert.That(sut.Equals(instance), Is.True); 48 | } 49 | 50 | [Test] 51 | public void DoesNotEqualOtherObjectType() 52 | { 53 | var sut = SutFactory(); 54 | Assert.That(sut.Equals(new object()), Is.False); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualNull() 59 | { 60 | var sut = SutFactory(); 61 | Assert.That(sut.Equals(null), Is.False); 62 | } 63 | 64 | [Test] 65 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 66 | { 67 | var sut = SutFactory(123); 68 | var other = SutFactory(123); 69 | Assert.That(sut.Equals(other), Is.True); 70 | } 71 | 72 | [Test] 73 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 74 | { 75 | var sut = SutFactory(123); 76 | var other = SutFactory(456); 77 | Assert.That(sut.Equals(other), Is.False); 78 | } 79 | 80 | [Test] 81 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 82 | { 83 | var sut = SutFactory(123); 84 | var other = SutFactory(123); 85 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 86 | } 87 | 88 | [Test] 89 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 90 | { 91 | var sut = SutFactory(123); 92 | var other = SutFactory(456); 93 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 94 | } 95 | 96 | private static TSqlIntValue SutFactory() 97 | { 98 | return SutFactory(123); 99 | } 100 | 101 | private static TSqlIntValue SutFactory(int value) 102 | { 103 | return new TSqlIntValue(value); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlMoneyNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlMoneyNullValueTests 10 | { 11 | private TSqlMoneyNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlMoneyNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlMoneyNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.Money, DBNull.Value, true, 8, 0, 4); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.Money, DBNull.Value, true, 8, 0, 4); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlMoneyNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlMoneyValueTests.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using NUnit.Framework; 3 | using Projac.SqlClient; 4 | 5 | namespace Projac.Sql.Tests.SqlClient 6 | { 7 | [TestFixture] 8 | public class TSqlMoneyValueTests 9 | { 10 | [Test] 11 | public void IsSqlParameterValue() 12 | { 13 | var sut = SutFactory(); 14 | 15 | Assert.That(sut, Is.InstanceOf()); 16 | } 17 | 18 | [Test] 19 | public void ToDbParameterReturnsExpectedInstance() 20 | { 21 | const string parameterName = "name"; 22 | 23 | var sut = SutFactory(123.45M); 24 | 25 | var result = sut.ToDbParameter(parameterName); 26 | 27 | result.ExpectSqlParameter(parameterName, SqlDbType.Money, 123.45M, false, 8, 0, 4); 28 | } 29 | 30 | [Test] 31 | public void ToSqlParameterReturnsExpectedInstance() 32 | { 33 | const string parameterName = "name"; 34 | 35 | var sut = SutFactory(123.45M); 36 | 37 | var result = sut.ToSqlParameter(parameterName); 38 | 39 | result.ExpectSqlParameter(parameterName, SqlDbType.Money, 123.45M, false, 8, 0, 4); 40 | } 41 | 42 | [Test] 43 | public void DoesEqualItself() 44 | { 45 | var sut = SutFactory(); 46 | var instance = sut; 47 | Assert.That(sut.Equals(instance), Is.True); 48 | } 49 | 50 | [Test] 51 | public void DoesNotEqualOtherObjectType() 52 | { 53 | var sut = SutFactory(); 54 | Assert.That(sut.Equals(new object()), Is.False); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualNull() 59 | { 60 | var sut = SutFactory(); 61 | Assert.That(sut.Equals(null), Is.False); 62 | } 63 | 64 | [Test] 65 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 66 | { 67 | var sut = SutFactory(123.45M); 68 | var other = SutFactory(123.45M); 69 | Assert.That(sut.Equals(other), Is.True); 70 | } 71 | 72 | [Test] 73 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 74 | { 75 | var sut = SutFactory(123.45M); 76 | var other = SutFactory(456.78M); 77 | Assert.That(sut.Equals(other), Is.False); 78 | } 79 | 80 | [Test] 81 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 82 | { 83 | var sut = SutFactory(123.45M); 84 | var other = SutFactory(123.45M); 85 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 86 | } 87 | 88 | [Test] 89 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 90 | { 91 | var sut = SutFactory(123.45M); 92 | var other = SutFactory(456.78M); 93 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 94 | } 95 | 96 | private static TSqlMoneyValue SutFactory() 97 | { 98 | return SutFactory(123.45M); 99 | } 100 | 101 | private static TSqlMoneyValue SutFactory(decimal value) 102 | { 103 | return new TSqlMoneyValue(value); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlNCharNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlNCharNullValueTests 10 | { 11 | private TSqlNCharNullValue _sut; 12 | private TSqlNCharSize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlNCharSize(100); 18 | _sut = new TSqlNCharNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.NChar, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.NChar, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlNVarCharNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlNVarCharNullValueTests 10 | { 11 | private TSqlNVarCharNullValue _sut; 12 | private TSqlNVarCharSize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlNVarCharSize(100); 18 | _sut = new TSqlNVarCharNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.NVarChar, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.NVarChar, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlUniqueIdentifierNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlUniqueIdentifierNullValueTests 10 | { 11 | private TSqlUniqueIdentifierNullValue _sut; 12 | 13 | [SetUp] 14 | public void SetUp() 15 | { 16 | _sut = TSqlUniqueIdentifierNullValue.Instance; 17 | } 18 | 19 | [Test] 20 | public void InstanceIsSqlNullValue() 21 | { 22 | Assert.That(TSqlUniqueIdentifierNullValue.Instance, Is.InstanceOf()); 23 | } 24 | 25 | [Test] 26 | public void IsSqlParameterValue() 27 | { 28 | Assert.That(_sut, Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void ToDbParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var result = _sut.ToDbParameter(parameterName); 37 | 38 | result.ExpectSqlParameter(parameterName, SqlDbType.UniqueIdentifier, DBNull.Value, true, 16); 39 | } 40 | 41 | [Test] 42 | public void ToSqlParameterReturnsExpectedInstance() 43 | { 44 | const string parameterName = "name"; 45 | 46 | var result = _sut.ToSqlParameter(parameterName); 47 | 48 | result.ExpectSqlParameter(parameterName, SqlDbType.UniqueIdentifier, DBNull.Value, true, 16); 49 | } 50 | 51 | [Test] 52 | public void DoesEqualItself() 53 | { 54 | Assert.That(_sut.Equals(TSqlUniqueIdentifierNullValue.Instance), Is.True); 55 | } 56 | 57 | [Test] 58 | public void DoesNotEqualOtherObjectType() 59 | { 60 | Assert.That(_sut.Equals(new object()), Is.False); 61 | } 62 | 63 | [Test] 64 | public void DoesNotEqualNull() 65 | { 66 | Assert.That(_sut.Equals(null), Is.False); 67 | } 68 | 69 | [Test] 70 | public void HasExpectedHashCode() 71 | { 72 | var result = _sut.GetHashCode(); 73 | 74 | Assert.That(result, Is.EqualTo(0)); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlUniqueIdentifierValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlUniqueIdentifierValueTests 10 | { 11 | [Test] 12 | public void IsSqlParameterValue() 13 | { 14 | var sut = SutFactory(); 15 | 16 | Assert.That(sut, Is.InstanceOf()); 17 | } 18 | 19 | [Test] 20 | public void ToDbParameterReturnsExpectedInstance() 21 | { 22 | const string parameterName = "name"; 23 | 24 | var sut = SutFactory(Guid.Empty); 25 | 26 | var result = sut.ToDbParameter(parameterName); 27 | 28 | result.ExpectSqlParameter(parameterName, SqlDbType.UniqueIdentifier, Guid.Empty, false, 16); 29 | } 30 | 31 | [Test] 32 | public void ToSqlParameterReturnsExpectedInstance() 33 | { 34 | const string parameterName = "name"; 35 | 36 | var sut = SutFactory(Guid.Empty); 37 | 38 | var result = sut.ToSqlParameter(parameterName); 39 | 40 | result.ExpectSqlParameter(parameterName, SqlDbType.UniqueIdentifier, Guid.Empty, false, 16); 41 | } 42 | 43 | [Test] 44 | public void DoesEqualItself() 45 | { 46 | var sut = SutFactory(); 47 | var instance = sut; 48 | Assert.That(sut.Equals(instance), Is.True); 49 | } 50 | 51 | [Test] 52 | public void DoesNotEqualOtherObjectType() 53 | { 54 | var sut = SutFactory(); 55 | Assert.That(sut.Equals(new object()), Is.False); 56 | } 57 | 58 | [Test] 59 | public void DoesNotEqualNull() 60 | { 61 | var sut = SutFactory(); 62 | Assert.That(sut.Equals(null), Is.False); 63 | } 64 | 65 | [Test] 66 | public void TwoInstanceAreEqualIfTheyHaveTheSameValue() 67 | { 68 | var sut = SutFactory(Guid.Empty); 69 | var other = SutFactory(Guid.Empty); 70 | Assert.That(sut.Equals(other), Is.True); 71 | } 72 | 73 | [Test] 74 | public void TwoInstanceAreNotEqualIfTheirValueDiffers() 75 | { 76 | var sut = SutFactory(Guid.NewGuid()); 77 | var other = SutFactory(Guid.NewGuid()); 78 | Assert.That(sut.Equals(other), Is.False); 79 | } 80 | 81 | [Test] 82 | public void TwoInstanceHaveTheSameHashCodeIfTheyHaveTheSameValue() 83 | { 84 | var sut = SutFactory(Guid.Empty); 85 | var other = SutFactory(Guid.Empty); 86 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.True); 87 | } 88 | 89 | [Test] 90 | public void TwoInstanceDoNotHaveTheSameHashCodeIfTheirValueDiffers() 91 | { 92 | var sut = SutFactory(Guid.NewGuid()); 93 | var other = SutFactory(Guid.NewGuid()); 94 | Assert.That(sut.GetHashCode().Equals(other.GetHashCode()), Is.False); 95 | } 96 | 97 | private static TSqlUniqueIdentifierValue SutFactory() 98 | { 99 | return SutFactory(Guid.NewGuid()); 100 | } 101 | 102 | private static TSqlUniqueIdentifierValue SutFactory(Guid value) 103 | { 104 | return new TSqlUniqueIdentifierValue(value); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlVarBinaryNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlVarBinaryNullValueTests 10 | { 11 | private TSqlVarBinaryNullValue _sut; 12 | private TSqlVarBinarySize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlVarBinarySize(100); 18 | _sut = new TSqlVarBinaryNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.VarBinary, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.VarBinary, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient.Tests/TSqlVarCharNullValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NUnit.Framework; 4 | using Projac.SqlClient; 5 | 6 | namespace Projac.Sql.Tests.SqlClient 7 | { 8 | [TestFixture] 9 | public class TSqlVarCharNullValueTests 10 | { 11 | private TSqlVarCharNullValue _sut; 12 | private TSqlVarCharSize _size; 13 | 14 | [SetUp] 15 | public void SetUp() 16 | { 17 | _size = new TSqlVarCharSize(100); 18 | _sut = new TSqlVarCharNullValue(_size); 19 | } 20 | 21 | [Test] 22 | public void IsSqlParameterValue() 23 | { 24 | Assert.That(_sut, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void ToDbParameterReturnsExpectedInstance() 29 | { 30 | const string parameterName = "name"; 31 | 32 | var result = _sut.ToDbParameter(parameterName); 33 | 34 | result.ExpectSqlParameter(parameterName, SqlDbType.VarChar, DBNull.Value, true, 100); 35 | } 36 | 37 | [Test] 38 | public void ToSqlParameterReturnsExpectedInstance() 39 | { 40 | const string parameterName = "name"; 41 | 42 | var result = _sut.ToSqlParameter(parameterName); 43 | 44 | result.ExpectSqlParameter(parameterName, SqlDbType.VarChar, DBNull.Value, true, 100); 45 | } 46 | 47 | [Test] 48 | public void DoesEqualItself() 49 | { 50 | var self = _sut; 51 | Assert.That(_sut.Equals(self), Is.True); 52 | } 53 | 54 | [Test] 55 | public void DoesNotEqualOtherObjectType() 56 | { 57 | Assert.That(_sut.Equals(new object()), Is.False); 58 | } 59 | 60 | [Test] 61 | public void DoesNotEqualNull() 62 | { 63 | Assert.That(_sut.Equals(null), Is.False); 64 | } 65 | 66 | [Test] 67 | public void HasExpectedHashCode() 68 | { 69 | var result = _sut.GetHashCode(); 70 | 71 | Assert.That(result, Is.EqualTo(100)); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/Legacy/TSql.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient.Legacy 8 | { 9 | /// 10 | /// Fluent T-SQL syntax. 11 | /// 12 | [Obsolete("Please use the SqlClientSyntax instead. This will be removed in a future release.")] 13 | public static partial class TSql 14 | { 15 | private static DbParameter[] CollectFromAnonymousType(object parameters) 16 | { 17 | if (parameters == null) 18 | return new DbParameter[0]; 19 | return ThrowIfMaxParameterCountExceeded( 20 | parameters. 21 | GetType(). 22 | GetProperties(BindingFlags.Instance | BindingFlags.Public). 23 | Where(property => typeof(IDbParameterValue).IsAssignableFrom(property.PropertyType)). 24 | Select(property => 25 | ((IDbParameterValue)property.GetGetMethod().Invoke(parameters, null)). 26 | ToDbParameter(FormatDbParameterName(property.Name))). 27 | ToArray()); 28 | } 29 | 30 | private static DbParameter[] ThrowIfMaxParameterCountExceeded(DbParameter[] parameters) 31 | { 32 | if (parameters.Length > Limits.MaxParameterCount) 33 | throw new ArgumentException( 34 | string.Format("The parameter count is limited to {0}.", Limits.MaxParameterCount), 35 | "parameters"); 36 | return parameters; 37 | } 38 | 39 | // ReSharper disable UnusedParameter.Local 40 | private static void ThrowIfMaxParameterCountExceeded(IDbParameterValue[] parameters) 41 | // ReSharper restore UnusedParameter.Local 42 | { 43 | if (parameters.Length > Limits.MaxParameterCount) 44 | throw new ArgumentException( 45 | string.Format("The parameter count is limited to {0}.", Limits.MaxParameterCount), 46 | "parameters"); 47 | } 48 | 49 | private static string FormatDbParameterName(string name) 50 | { 51 | return "@" + name; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/Limits.cs: -------------------------------------------------------------------------------- 1 | namespace Projac.SqlClient 2 | { 3 | internal static class Limits 4 | { 5 | public const int MaxByteSize = 8000; 6 | public const int MaxAnsiSize = 8000; 7 | public const int MaxUnicodeSize = 4000; 8 | public const int MaxParameterCount = 2098; 9 | } 10 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/SqlClientSyntax.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient 8 | { 9 | /// 10 | /// Fluent T-SQL syntax. 11 | /// 12 | public partial class SqlClientSyntax 13 | { 14 | private static DbParameter[] CollectFromAnonymousType(object parameters) 15 | { 16 | if (parameters == null) 17 | return new DbParameter[0]; 18 | return ThrowIfMaxParameterCountExceeded( 19 | parameters. 20 | GetType(). 21 | GetProperties(BindingFlags.Instance | BindingFlags.Public). 22 | Where(property => typeof(IDbParameterValue).IsAssignableFrom(property.PropertyType)). 23 | Select(property => 24 | ((IDbParameterValue)property.GetGetMethod().Invoke(parameters, null)). 25 | ToDbParameter(FormatDbParameterName(property.Name))). 26 | ToArray()); 27 | } 28 | 29 | private static DbParameter[] ThrowIfMaxParameterCountExceeded(DbParameter[] parameters) 30 | { 31 | if (parameters.Length > Limits.MaxParameterCount) 32 | throw new ArgumentException( 33 | string.Format("The parameter count is limited to {0}.", Limits.MaxParameterCount), 34 | "parameters"); 35 | return parameters; 36 | } 37 | 38 | // ReSharper disable UnusedParameter.Local 39 | private static void ThrowIfMaxParameterCountExceeded(IDbParameterValue[] parameters) 40 | // ReSharper restore UnusedParameter.Local 41 | { 42 | if (parameters.Length > Limits.MaxParameterCount) 43 | throw new ArgumentException( 44 | string.Format("The parameter count is limited to {0}.", Limits.MaxParameterCount), 45 | "parameters"); 46 | } 47 | 48 | private static string FormatDbParameterName(string name) 49 | { 50 | return "@" + name; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Projac.SqlClient/TSqlBigIntNullValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using System.Data.SqlClient; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient 8 | { 9 | /// 10 | /// Represents the T-SQL BIGINT NULL parameter value. 11 | /// 12 | public class TSqlBigIntNullValue : IDbParameterValue 13 | { 14 | /// 15 | /// The single instance of this value. 16 | /// 17 | public static readonly TSqlBigIntNullValue Instance = new TSqlBigIntNullValue(); 18 | 19 | private TSqlBigIntNullValue() 20 | { 21 | } 22 | 23 | /// 24 | /// Creates a instance based on this instance. 25 | /// 26 | /// The name of the parameter. 27 | /// 28 | /// A . 29 | /// 30 | public DbParameter ToDbParameter(string parameterName) 31 | { 32 | return ToSqlParameter(parameterName); 33 | } 34 | 35 | /// 36 | /// Creates a instance based on this instance. 37 | /// 38 | /// The name of the parameter. 39 | /// 40 | /// A . 41 | /// 42 | public SqlParameter ToSqlParameter(string parameterName) 43 | { 44 | #if NET46 || NET452 45 | return new SqlParameter( 46 | parameterName, 47 | SqlDbType.BigInt, 48 | 8, 49 | ParameterDirection.Input, 50 | true, 51 | 0, 52 | 0, 53 | "", 54 | DataRowVersion.Default, 55 | DBNull.Value); 56 | #elif NETSTANDARD2_0 57 | return new SqlParameter 58 | { 59 | ParameterName = parameterName, 60 | Direction = ParameterDirection.Input, 61 | SqlDbType = SqlDbType.BigInt, 62 | Size = 8, 63 | Value = DBNull.Value, 64 | SourceColumn = "", 65 | IsNullable = true, 66 | Precision = 0, 67 | Scale = 0, 68 | SourceVersion = DataRowVersion.Default 69 | }; 70 | #endif 71 | } 72 | 73 | private static bool Equals(TSqlBigIntNullValue value) 74 | { 75 | return ReferenceEquals(value, Instance); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified is equal to this instance. 80 | /// 81 | /// The to compare with this instance. 82 | /// 83 | /// true if the specified is equal to this instance; otherwise, false. 84 | /// 85 | public override bool Equals(object obj) 86 | { 87 | if (obj == null || obj.GetType() != GetType()) 88 | return false; 89 | return Equals((TSqlBigIntNullValue) obj); 90 | } 91 | 92 | /// 93 | /// Returns a hash code for this instance. 94 | /// 95 | /// 96 | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 97 | /// 98 | public override int GetHashCode() 99 | { 100 | return 0; 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/TSqlBitNullValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using System.Data.SqlClient; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient 8 | { 9 | /// 10 | /// Represents the T-SQL BIT NULL parameter value. 11 | /// 12 | public class TSqlBitNullValue : IDbParameterValue 13 | { 14 | /// 15 | /// The single instance of this value. 16 | /// 17 | public static readonly TSqlBitNullValue Instance = new TSqlBitNullValue(); 18 | 19 | private TSqlBitNullValue() 20 | { 21 | } 22 | 23 | /// 24 | /// Creates a instance based on this instance. 25 | /// 26 | /// The name of the parameter. 27 | /// 28 | /// A . 29 | /// 30 | public DbParameter ToDbParameter(string parameterName) 31 | { 32 | return ToSqlParameter(parameterName); 33 | } 34 | 35 | /// 36 | /// Creates a instance based on this instance. 37 | /// 38 | /// The name of the parameter. 39 | /// 40 | /// A . 41 | /// 42 | public SqlParameter ToSqlParameter(string parameterName) 43 | { 44 | #if NET46 || NET452 45 | return new SqlParameter( 46 | parameterName, 47 | SqlDbType.Bit, 48 | 1, 49 | ParameterDirection.Input, 50 | true, 51 | 0, 52 | 0, 53 | "", 54 | DataRowVersion.Default, 55 | DBNull.Value); 56 | #elif NETSTANDARD2_0 57 | return new SqlParameter 58 | { 59 | ParameterName = parameterName, 60 | Direction = ParameterDirection.Input, 61 | SqlDbType = SqlDbType.Bit, 62 | Size = 1, 63 | Value = DBNull.Value, 64 | SourceColumn = "", 65 | IsNullable = true, 66 | Precision = 0, 67 | Scale = 0, 68 | SourceVersion = DataRowVersion.Default 69 | }; 70 | #endif 71 | } 72 | 73 | private static bool Equals(TSqlBitNullValue value) 74 | { 75 | return ReferenceEquals(value, Instance); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified is equal to this instance. 80 | /// 81 | /// The to compare with this instance. 82 | /// 83 | /// true if the specified is equal to this instance; otherwise, false. 84 | /// 85 | public override bool Equals(object obj) 86 | { 87 | if (obj == null || obj.GetType() != GetType()) 88 | return false; 89 | return Equals((TSqlBitNullValue) obj); 90 | } 91 | 92 | /// 93 | /// Returns a hash code for this instance. 94 | /// 95 | /// 96 | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 97 | /// 98 | public override int GetHashCode() 99 | { 100 | return 0; 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/TSqlDateNullValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using System.Data.SqlClient; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient 8 | { 9 | /// 10 | /// Represents the T-SQL DATE NULL parameter value. 11 | /// 12 | public class TSqlDateNullValue : IDbParameterValue 13 | { 14 | /// 15 | /// The single instance of this value. 16 | /// 17 | public static readonly TSqlDateNullValue Instance = new TSqlDateNullValue(); 18 | 19 | private TSqlDateNullValue() 20 | { 21 | } 22 | 23 | /// 24 | /// Creates a instance based on this instance. 25 | /// 26 | /// The name of the parameter. 27 | /// 28 | /// A . 29 | /// 30 | public DbParameter ToDbParameter(string parameterName) 31 | { 32 | return ToSqlParameter(parameterName); 33 | } 34 | 35 | /// 36 | /// Creates a instance based on this instance. 37 | /// 38 | /// The name of the parameter. 39 | /// 40 | /// A . 41 | /// 42 | public SqlParameter ToSqlParameter(string parameterName) 43 | { 44 | #if NET46 || NET452 45 | return new SqlParameter( 46 | parameterName, 47 | SqlDbType.Date, 48 | 0, 49 | ParameterDirection.Input, 50 | true, 51 | 0, 52 | 0, 53 | "", 54 | DataRowVersion.Default, 55 | DBNull.Value); 56 | #elif NETSTANDARD2_0 57 | return new SqlParameter 58 | { 59 | ParameterName = parameterName, 60 | Direction = ParameterDirection.Input, 61 | SqlDbType = SqlDbType.Date, 62 | Size = 0, 63 | Value = DBNull.Value, 64 | SourceColumn = "", 65 | IsNullable = true, 66 | Precision = 0, 67 | Scale = 0, 68 | SourceVersion = DataRowVersion.Default 69 | }; 70 | #endif 71 | } 72 | 73 | private static bool Equals(TSqlDateNullValue value) 74 | { 75 | return ReferenceEquals(value, Instance); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified is equal to this instance. 80 | /// 81 | /// The to compare with this instance. 82 | /// 83 | /// true if the specified is equal to this instance; otherwise, false. 84 | /// 85 | public override bool Equals(object obj) 86 | { 87 | if (obj == null || obj.GetType() != GetType()) 88 | { 89 | return false; 90 | } 91 | 92 | return Equals((TSqlDateNullValue)obj); 93 | } 94 | 95 | /// 96 | /// Returns a hash code for this instance. 97 | /// 98 | /// 99 | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 100 | /// 101 | public override int GetHashCode() 102 | { 103 | return 0; 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Projac.SqlClient/TSqlIntNullValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Data.Common; 4 | using System.Data.SqlClient; 5 | using Projac.Sql; 6 | 7 | namespace Projac.SqlClient 8 | { 9 | /// 10 | /// Represents the T-SQL INT NULL parameter value. 11 | /// 12 | public class TSqlIntNullValue : IDbParameterValue 13 | { 14 | /// 15 | /// The single instance of this value. 16 | /// 17 | public static readonly TSqlIntNullValue Instance = new TSqlIntNullValue(); 18 | 19 | private TSqlIntNullValue() 20 | { 21 | } 22 | 23 | /// 24 | /// Creates a instance based on this instance. 25 | /// 26 | /// The name of the parameter. 27 | /// 28 | /// A . 29 | /// 30 | public DbParameter ToDbParameter(string parameterName) 31 | { 32 | return ToSqlParameter(parameterName); 33 | } 34 | 35 | /// 36 | /// Creates a instance based on this instance. 37 | /// 38 | /// The name of the parameter. 39 | /// 40 | /// A . 41 | /// 42 | public SqlParameter ToSqlParameter(string parameterName) 43 | { 44 | #if NET46 || NET452 45 | return new SqlParameter( 46 | parameterName, 47 | SqlDbType.Int, 48 | 4, 49 | ParameterDirection.Input, 50 | true, 51 | 0, 52 | 0, 53 | "", 54 | DataRowVersion.Default, 55 | DBNull.Value); 56 | #elif NETSTANDARD2_0 57 | return new SqlParameter 58 | { 59 | ParameterName = parameterName, 60 | Direction = ParameterDirection.Input, 61 | SqlDbType = SqlDbType.Int, 62 | Size = 4, 63 | Value = DBNull.Value, 64 | SourceColumn = "", 65 | IsNullable = true, 66 | Precision = 0, 67 | Scale = 0, 68 | SourceVersion = DataRowVersion.Default 69 | }; 70 | #endif 71 | } 72 | 73 | private static bool Equals(TSqlIntNullValue value) 74 | { 75 | return ReferenceEquals(value, Instance); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified is equal to this instance. 80 | /// 81 | /// The to compare with this instance. 82 | /// 83 | /// true if the specified is equal to this instance; otherwise, false. 84 | /// 85 | public override bool Equals(object obj) 86 | { 87 | if (obj == null || obj.GetType() != GetType()) 88 | return false; 89 | return Equals((TSqlIntNullValue) obj); 90 | } 91 | 92 | /// 93 | /// Returns a hash code for this instance. 94 | /// 95 | /// 96 | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 97 | /// 98 | public override int GetHashCode() 99 | { 100 | return 0; 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /src/Projac.Tests/CallRecordingConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace Projac.Tests 6 | { 7 | public class CallRecordingConnection 8 | { 9 | private readonly List> _calls; 10 | 11 | public CallRecordingConnection() 12 | { 13 | _calls = new List>(); 14 | } 15 | 16 | public void RecordCall(int handler, object message, CancellationToken token) 17 | { 18 | _calls.Add(new Tuple(handler, message, CancellationToken.None)); 19 | } 20 | 21 | public Tuple[] RecordedCalls 22 | { 23 | get 24 | { 25 | return _calls.ToArray(); 26 | } 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/Projac.Tests/ConcurrentResolveTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace Projac.Tests 5 | { 6 | [TestFixture] 7 | public class ConcurrentResolveTests 8 | { 9 | [Test] 10 | public void WhenEqualToHandlerMessageTypeHandlersCanNotBeNull() 11 | { 12 | Assert.Throws(() => 13 | ConcurrentResolve.WhenEqualToHandlerMessageType(null)); 14 | } 15 | 16 | [Test] 17 | public void WhenEqualToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 18 | { 19 | var sut = ConcurrentResolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]); 20 | Assert.Throws(() => sut(null)); 21 | } 22 | 23 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenEqualToHandlerMessageTypeCases")] 24 | public void WhenEqualToHandlerMessageTypeResolverReturnsExpectedResult( 25 | ProjectionHandler[] resolvable, 26 | object message, 27 | ProjectionHandler[] resolved) 28 | { 29 | var sut = ConcurrentResolve.WhenEqualToHandlerMessageType(resolvable); 30 | var result = sut(message); 31 | Assert.That(result, Is.EquivalentTo(resolved)); 32 | } 33 | 34 | [Test] 35 | public void WhenAssignableToHandlerMessageTypeHandlersCanNotBeNull() 36 | { 37 | Assert.Throws(() => 38 | ConcurrentResolve.WhenAssignableToHandlerMessageType(null)); 39 | } 40 | 41 | [Test] 42 | public void WhenAssignableToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 43 | { 44 | var sut = ConcurrentResolve.WhenAssignableToHandlerMessageType(new ProjectionHandler[0]); 45 | Assert.Throws(() => sut(null)); 46 | } 47 | 48 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenAssignableToHandlerMessageTypeCases")] 49 | public void WhenAssignableToHandlerMessageTypeResolverReturnsExpectedResult( 50 | ProjectionHandler[] resolvable, 51 | object message, 52 | ProjectionHandler[] resolved) 53 | { 54 | var sut = ConcurrentResolve.WhenAssignableToHandlerMessageType(resolvable); 55 | var result = sut(message); 56 | Assert.That(result, Is.EquivalentTo(resolved)); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /src/Projac.Tests/Projac.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac.Tests 5 | Projac.Tests 6 | netcoreapp2.0 7 | netcoreapp2.0;net46;net452 8 | AnyCPU 9 | Projac.SQLite.Tests 10 | true 11 | false 12 | 13 | 14 | 0.0.0 15 | 0.0.0 16 | 0.0.0 17 | 18 | 19 | False 20 | False 21 | Off 22 | 23 | 24 | False 25 | True 26 | Off 27 | 28 | 29 | false 30 | Projac.Tests 31 | Yves Reynhout 32 | BitTacklr 33 | Projac 34 | Projac.Tests unit tests Projac. 35 | Copyright © BitTacklr 2017. All rights reserved. 36 | en 37 | 38 | 39 | 40 | 41 | 42 | portable 43 | 44 | 45 | full 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/Projac.Tests/ProjectionHandlerTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | 6 | namespace Projac.Tests 7 | { 8 | [TestFixture] 9 | public class ProjectionHandlerTests 10 | { 11 | [Test] 12 | public void MessageCanNotBeNull() 13 | { 14 | Assert.Throws( 15 | () => new ProjectionHandler(null, (_, __, ___) => Task.FromResult(null)) 16 | ); 17 | } 18 | 19 | [Test] 20 | public void HandlerCanNotBeNull() 21 | { 22 | Assert.Throws( 23 | () => new ProjectionHandler(typeof(object), null) 24 | ); 25 | } 26 | 27 | [Test] 28 | public void ParametersArePreservedAsProperties() 29 | { 30 | var message = typeof(object); 31 | Func handler = (_, __, ___) => Task.FromResult(null); 32 | 33 | var sut = new ProjectionHandler(message, handler); 34 | 35 | Assert.That(sut.Message, Is.EqualTo(message)); 36 | Assert.That(sut.Handler, Is.EqualTo(handler)); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/Projac.Tests/ResolveTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace Projac.Tests 5 | { 6 | [TestFixture] 7 | public class ResolveTests 8 | { 9 | [Test] 10 | public void WhenEqualToHandlerMessageTypeHandlersCanNotBeNull() 11 | { 12 | Assert.Throws(() => 13 | Resolve.WhenEqualToHandlerMessageType(null)); 14 | } 15 | 16 | [Test] 17 | public void WhenEqualToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 18 | { 19 | var sut = Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]); 20 | Assert.Throws(() => sut(null)); 21 | } 22 | 23 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenEqualToHandlerMessageTypeCases")] 24 | public void WhenEqualToHandlerMessageTypeResolverReturnsExpectedResult( 25 | ProjectionHandler[] resolvable, 26 | object message, 27 | ProjectionHandler[] resolved) 28 | { 29 | var sut = Resolve.WhenEqualToHandlerMessageType(resolvable); 30 | var result = sut(message); 31 | Assert.That(result, Is.EquivalentTo(resolved)); 32 | } 33 | 34 | [Test] 35 | public void WhenAssignableToHandlerMessageTypeHandlersCanNotBeNull() 36 | { 37 | Assert.Throws(() => 38 | Resolve.WhenAssignableToHandlerMessageType(null)); 39 | } 40 | 41 | [Test] 42 | public void WhenAssignableToHandlerMessageTypeResolverThrowsWhenMessageIsNull() 43 | { 44 | var sut = Resolve.WhenAssignableToHandlerMessageType(new ProjectionHandler[0]); 45 | Assert.Throws(() => sut(null)); 46 | } 47 | 48 | [TestCaseSource(typeof(HandlerResolutionCases), "WhenAssignableToHandlerMessageTypeCases")] 49 | public void WhenAssignableToHandlerMessageTypeResolverReturnsExpectedResult( 50 | ProjectionHandler[] resolvable, 51 | object message, 52 | ProjectionHandler[] resolved) 53 | { 54 | var sut = Resolve.WhenAssignableToHandlerMessageType(resolvable); 55 | var result = sut(message); 56 | Assert.That(result, Is.EquivalentTo(resolved)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Projac.Tests/Signal.cs: -------------------------------------------------------------------------------- 1 | namespace Projac.Tests 2 | { 3 | public class Signal 4 | { 5 | private bool _set; 6 | 7 | public Signal() 8 | { 9 | _set = false; 10 | } 11 | 12 | public void Set() 13 | { 14 | _set = true; 15 | } 16 | 17 | public bool IsSet { get { return _set; } } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Projac.Tests/Testing/ProjectionTestSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | using Projac.Testing; 6 | 7 | namespace Projac.Tests.Testing 8 | { 9 | [TestFixture] 10 | public class ProjectionTestSpecificationTests 11 | { 12 | [Test] 13 | public void ResolverCanNotBeNull() 14 | { 15 | Assert.Throws(() => 16 | new ProjectionTestSpecification( 17 | null, 18 | new object[0], 19 | (session, token) => Task.FromResult(VerificationResult.Pass())) 20 | ); 21 | } 22 | 23 | [Test] 24 | public void ResolverReturnsExpectedResult() 25 | { 26 | var resolver = Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]); 27 | var sut = new ProjectionTestSpecification( 28 | resolver, 29 | new object[0], 30 | (session, token) => Task.FromResult(VerificationResult.Pass())); 31 | 32 | var result = sut.Resolver; 33 | 34 | Assert.That(result, Is.SameAs(resolver)); 35 | } 36 | 37 | [Test] 38 | public void MessagesCanNotBeNull() 39 | { 40 | Assert.Throws(() => 41 | new ProjectionTestSpecification( 42 | Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]), 43 | null, 44 | (session, token) => Task.FromResult(VerificationResult.Pass())) 45 | ); 46 | } 47 | 48 | [Test] 49 | public void MessagesReturnsExpectedResult() 50 | { 51 | var messages = new [] {new object(), new object() }; 52 | var sut = new ProjectionTestSpecification( 53 | Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]), 54 | messages, 55 | (session, token) => Task.FromResult(VerificationResult.Pass())); 56 | 57 | var result = sut.Messages; 58 | 59 | Assert.That(result, Is.SameAs(messages)); 60 | } 61 | 62 | [Test] 63 | public void VerificationCanNotBeNull() 64 | { 65 | Assert.Throws(() => 66 | new ProjectionTestSpecification( 67 | Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]), 68 | new object[0], 69 | null) 70 | ); 71 | } 72 | 73 | [Test] 74 | public void VerificationReturnsExpectedResult() 75 | { 76 | Func> verification = 77 | (session, token) => Task.FromResult(VerificationResult.Pass()); 78 | var sut = new ProjectionTestSpecification( 79 | Resolve.WhenEqualToHandlerMessageType(new ProjectionHandler[0]), 80 | new object[0], 81 | verification); 82 | 83 | var result = sut.Verification; 84 | 85 | Assert.That(result, Is.SameAs(verification)); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Projac/AnonymousProjection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Projac 6 | { 7 | /// 8 | /// Represent an anonymous projection. 9 | /// 10 | public class AnonymousProjection : IEnumerable> 11 | { 12 | private readonly ProjectionHandler[] _handlers; 13 | 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// The handlers. 18 | /// 19 | /// Throw when are null. 20 | /// 21 | public AnonymousProjection(ProjectionHandler[] handlers) 22 | { 23 | if (handlers == null) 24 | throw new ArgumentNullException("handlers"); 25 | 26 | _handlers = handlers; 27 | } 28 | 29 | /// 30 | /// Gets a read only collection of projection handlers. 31 | /// 32 | /// 33 | /// The projection handlers associated with this specification. 34 | /// 35 | public ProjectionHandler[] Handlers 36 | { 37 | get { return _handlers; } 38 | } 39 | 40 | /// 41 | /// Performs an implicit conversion from to ProjectionHandler{TConnection}. 42 | /// 43 | /// The instance. 44 | /// 45 | /// The result of the conversion. 46 | /// 47 | public static implicit operator ProjectionHandler[](AnonymousProjection instance) 48 | { 49 | return instance.Handlers; 50 | } 51 | 52 | /// 53 | /// Returns an enumerator that iterates through a copy of the handlers. 54 | /// 55 | /// 56 | /// An that can be used to iterate through a copy of the handlers. 57 | /// 58 | public ProjectionHandlerEnumerator GetEnumerator() 59 | { 60 | return new ProjectionHandlerEnumerator(Handlers); 61 | } 62 | 63 | /// 64 | /// Returns an enumerator that iterates through the collection. 65 | /// 66 | /// 67 | /// An that can be used to iterate through the collection. 68 | /// 69 | IEnumerator> IEnumerable>.GetEnumerator() 70 | { 71 | return GetEnumerator(); 72 | } 73 | 74 | /// 75 | /// Returns an enumerator that iterates through a collection. 76 | /// 77 | /// 78 | /// An object that can be used to iterate through the collection. 79 | /// 80 | IEnumerator IEnumerable.GetEnumerator() 81 | { 82 | return GetEnumerator(); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /src/Projac/ConcurrentResolve.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | 4 | namespace Projac 5 | { 6 | /// 7 | /// Represents the available concurrent resolvers. 8 | /// 9 | public static class ConcurrentResolve 10 | { 11 | /// 12 | /// Resolves the handlers that match the type of the message exactly. 13 | /// 14 | /// The set of resolvable handlers. 15 | /// A resolver. 16 | public static ProjectionHandlerResolver WhenEqualToHandlerMessageType(ProjectionHandler[] handlers) 17 | { 18 | return Resolve.WhenEqualToHandlerMessageType(handlers); 19 | } 20 | 21 | /// 22 | /// Resolves the handlers to which the message instance is assignable. 23 | /// 24 | /// The set of resolvable handlers. 25 | /// A resolver. 26 | public static ProjectionHandlerResolver WhenAssignableToHandlerMessageType(ProjectionHandler[] handlers) 27 | { 28 | if (handlers == null) 29 | throw new ArgumentNullException("handlers"); 30 | var cache = new ConcurrentDictionary[]>(); 31 | return message => 32 | { 33 | if (message == null) 34 | throw new ArgumentNullException("message"); 35 | ProjectionHandler[] result; 36 | if (!cache.TryGetValue(message.GetType(), out result)) 37 | { 38 | result = cache.GetOrAdd(message.GetType(), 39 | Array.FindAll(handlers, 40 | handler => handler.Message.IsInstanceOfType(message))); 41 | } 42 | return result; 43 | }; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/Projac/Projac.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Projac 5 | Projac 6 | netstandard2.0 7 | netstandard2.0;net46;net452 8 | AnyCPU 9 | Projac 10 | true 11 | 12 | 13 | 0.0.0 14 | 0.0.0 15 | 0.0.0 16 | 17 | 18 | False 19 | False 20 | true 21 | Off 22 | 23 | 24 | False 25 | True 26 | true 27 | Off 28 | 29 | 30 | true 31 | 0.0.0 32 | Projac 33 | Yves Reynhout 34 | Projac 35 | Projac 36 | Yves Reynhout 37 | BitTacklr 38 | Projac 39 | Projac provides lightweight infrastructure for authoring projections. 40 | Copyright © BitTacklr 2017. All rights reserved. 41 | en 42 | false 43 | https://github.com/yreynhout/Projac/blob/master/LICENSE 44 | https://github.com/yreynhout/Projac 45 | Please visit https://github.com/BitTacklr/Projac/wiki/Changes$(PackageVersion) . 46 | command and query responsibility segregation;cqrs;event sourcing;es;domain driven design;ddd 47 | true 48 | true 49 | false 50 | https://github.com/yreynhout/Projac 51 | git 52 | true 53 | false 54 | 55 | 56 | portable 57 | 58 | 59 | full 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/Projac/ProjectionHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Projac 6 | { 7 | /// 8 | /// Represents a handler of a particular type of message. 9 | /// 10 | public class ProjectionHandler 11 | { 12 | private readonly Type _message; 13 | private readonly Func _handler; 14 | 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// The message. 19 | /// The handler. 20 | /// 21 | /// Throw when or is 22 | /// null. 23 | /// 24 | public ProjectionHandler(Type message, Func handler) 25 | { 26 | if (message == null) throw new ArgumentNullException("message"); 27 | if (handler == null) throw new ArgumentNullException("handler"); 28 | _message = message; 29 | _handler = handler; 30 | } 31 | 32 | /// 33 | /// The type of message to handle. 34 | /// 35 | public Type Message 36 | { 37 | get { return _message; } 38 | } 39 | 40 | /// 41 | /// The function that handles the message. 42 | /// 43 | public Func Handler 44 | { 45 | get { return _handler; } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Projac/ProjectionHandlerEnumerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Projac 6 | { 7 | /// 8 | /// Represents a array enumerator. 9 | /// 10 | public class ProjectionHandlerEnumerator : IEnumerator> 11 | { 12 | private readonly ProjectionHandler[] _handlers; 13 | private int _index; 14 | 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// The handlers to enumerate. 19 | /// Thrown when are null. 20 | public ProjectionHandlerEnumerator(ProjectionHandler[] handlers) 21 | { 22 | if (handlers == null) throw new ArgumentNullException("handlers"); 23 | _handlers = handlers; 24 | _index = -1; 25 | } 26 | 27 | /// 28 | /// Advances the enumerator to the next element of the collection. 29 | /// 30 | /// 31 | /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. 32 | /// 33 | public bool MoveNext() 34 | { 35 | return _index < _handlers.Length && 36 | ++_index < _handlers.Length; 37 | } 38 | 39 | /// 40 | /// Sets the enumerator to its initial position, which is before the first element in the collection. 41 | /// 42 | public void Reset() 43 | { 44 | _index = -1; 45 | } 46 | 47 | /// 48 | /// Gets the element in the collection at the current position of the enumerator. 49 | /// 50 | /// 51 | /// Enumeration has not started. Call MoveNext. 52 | /// or 53 | /// Enumeration has already ended. Call Reset. 54 | /// 55 | public ProjectionHandler Current 56 | { 57 | get 58 | { 59 | if (_index == -1) 60 | throw new InvalidOperationException("Enumeration has not started. Call MoveNext."); 61 | if (_index == _handlers.Length) 62 | throw new InvalidOperationException("Enumeration has already ended. Call Reset."); 63 | 64 | return _handlers[_index]; 65 | } 66 | } 67 | 68 | /// 69 | /// Gets the element in the collection at the current position of the enumerator. 70 | /// 71 | object IEnumerator.Current 72 | { 73 | get { return Current; } 74 | } 75 | 76 | /// 77 | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 78 | /// 79 | public void Dispose() 80 | { 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /src/Projac/ProjectionHandlerResolver.cs: -------------------------------------------------------------------------------- 1 | namespace Projac 2 | { 3 | /// 4 | /// Resolves the handlers that match the specified . 5 | /// 6 | /// The message to resolve handlers for. 7 | /// The set of matching handlers. 8 | public delegate ProjectionHandler[] ProjectionHandlerResolver(object message); 9 | } -------------------------------------------------------------------------------- /src/Projac/Resolve.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Projac 6 | { 7 | /// 8 | /// Represents the available resolvers. 9 | /// 10 | public static class Resolve 11 | { 12 | /// 13 | /// Resolves the handlers that match the type of the message exactly. 14 | /// 15 | /// The set of resolvable handlers. 16 | /// A resolver. 17 | public static ProjectionHandlerResolver WhenEqualToHandlerMessageType(ProjectionHandler[] handlers) 18 | { 19 | if (handlers == null) 20 | throw new ArgumentNullException("handlers"); 21 | var cache = handlers. 22 | GroupBy(handler => handler.Message). 23 | ToDictionary(@group => @group.Key, @group => @group.ToArray()); 24 | return message => 25 | { 26 | if(message == null) 27 | throw new ArgumentNullException("message"); 28 | ProjectionHandler[] result; 29 | return cache.TryGetValue(message.GetType(), out result) ? 30 | result : 31 | new ProjectionHandler[0]; 32 | }; 33 | } 34 | 35 | /// 36 | /// Resolves the handlers to which the message instance is assignable. 37 | /// 38 | /// The set of resolvable handlers. 39 | /// A resolver. 40 | public static ProjectionHandlerResolver WhenAssignableToHandlerMessageType(ProjectionHandler[] handlers) 41 | { 42 | if (handlers == null) 43 | throw new ArgumentNullException("handlers"); 44 | var cache = new Dictionary[]>(); 45 | return message => 46 | { 47 | if (message == null) 48 | throw new ArgumentNullException("message"); 49 | ProjectionHandler[] result; 50 | if (!cache.TryGetValue(message.GetType(), out result)) 51 | { 52 | result = Array.FindAll(handlers, 53 | handler => handler.Message.IsInstanceOfType(message)); 54 | cache.Add(message.GetType(), result); 55 | } 56 | return result; 57 | }; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Projac/TaskExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Projac 6 | { 7 | internal static class TaskExtensions 8 | { 9 | public static async Task ExecuteAsync(this IEnumerable enumerable, CancellationToken cancellationToken) 10 | { 11 | foreach (var task in enumerable) 12 | { 13 | await task.ConfigureAwait(false); 14 | 15 | if (cancellationToken.IsCancellationRequested) 16 | { 17 | return; 18 | } 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/Projac/Testing/ProjectionTestSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Projac.Testing 6 | { 7 | /// 8 | /// Represent a test specification. 9 | /// 10 | /// The type of the connection. 11 | public class ProjectionTestSpecification 12 | { 13 | private readonly ProjectionHandlerResolver _resolver; 14 | private readonly object[] _messages; 15 | private readonly Func> _verification; 16 | 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// The projection handler resolver. 21 | /// The messages to project. 22 | /// The verification method. 23 | /// Thrown when 24 | /// 25 | /// or 26 | /// 27 | /// or 28 | /// is null. 29 | /// 30 | public ProjectionTestSpecification(ProjectionHandlerResolver resolver, object[] messages, Func> verification) 31 | { 32 | if (resolver == null) throw new ArgumentNullException("resolver"); 33 | if (messages == null) throw new ArgumentNullException("messages"); 34 | if (verification == null) throw new ArgumentNullException("verification"); 35 | _resolver = resolver; 36 | _messages = messages; 37 | _verification = verification; 38 | } 39 | 40 | /// 41 | /// Gets the projection handler resolver. 42 | /// 43 | /// 44 | /// The projection handler resolver. 45 | /// 46 | public ProjectionHandlerResolver Resolver 47 | { 48 | get { return _resolver; } 49 | } 50 | 51 | /// 52 | /// Gets the messages to project. 53 | /// 54 | /// 55 | /// The messages to project. 56 | /// 57 | public object[] Messages 58 | { 59 | get { return _messages; } 60 | } 61 | 62 | /// 63 | /// Gets the verification method. 64 | /// 65 | /// 66 | /// The verification method. 67 | /// 68 | public Func> Verification 69 | { 70 | get { return _verification; } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Projac/Testing/VerificationResultState.cs: -------------------------------------------------------------------------------- 1 | namespace Projac.Testing 2 | { 3 | /// 4 | /// The resulting state of the test specification verification. 5 | /// 6 | public enum VerificationResultState 7 | { 8 | /// 9 | /// The test specification verification passed. 10 | /// 11 | Passed = 0, 12 | /// 13 | /// The test specification verification failed. 14 | /// 15 | Failed 16 | } 17 | } -------------------------------------------------------------------------------- /src/Recipes/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | --------------------------------------------------------------------------------