Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

James Taylor tech-test #8

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions TechTest/AddCustomers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
USE [Customers]
GO

-- The initial customer table
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Customer'))
BEGIN
CREATE TABLE [dbo].[Customer]
(
[CustomerId] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
[Name] NVARCHAR(100) NOT NULL,
[Country] VARCHAR(2) NOT NULL,
[DateOfBirth] DATETIME NOT NULL
)
END
25 changes: 25 additions & 0 deletions TechTest/AddOrders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
USE [Orders]
GO

-- base table
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Orders'))
BEGIN
CREATE TABLE [dbo].[Orders]
(
[OrderId] INT NOT NULL PRIMARY KEY,
[Amount] DECIMAL(10, 2) NOT NULL,
[VAT] DECIMAL(10, 2) NOT NULL
)
END

-- first migration
IF NOT EXISTS(SELECT 1 FROM sys.columns
WHERE Name = N'CustomerId'
AND Object_ID = Object_ID(N'dbo.Orders'))
BEGIN
ALTER TABLE dbo.Orders
ADD CustomerId INT NOT NULL DEFAULT 0;
END
55 changes: 55 additions & 0 deletions TechTest/AnyCompany.Data.Contract/AnyCompany.Data.Contract.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7F4D6121-26EF-48A2-A2F4-BBD5D114D8E7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany.Data.Contract</RootNamespace>
<AssemblyName>AnyCompany.Data.Contract</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\ICustomerRepository.cs" />
<Compile Include="Repositories\IOrderRepository.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany.Models\AnyCompany.Models.csproj">
<Project>{695D4D06-2336-4B91-BA68-4A4924CF4729}</Project>
<Name>AnyCompany.Models</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions TechTest/AnyCompany.Data.Contract/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AnyCompany.Data.Contract")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AnyCompany.Data.Contract")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7f4d6121-26ef-48a2-a2f4-bbd5d114d8e7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using AnyCompany.Models;

namespace AnyCompany.Data.Contract.Repositories
{
public interface ICustomerRepository
{
Customer Load(int customerId);
IEnumerable<Customer> GetList();
}
}
11 changes: 11 additions & 0 deletions TechTest/AnyCompany.Data.Contract/Repositories/IOrderRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using AnyCompany.Models;

namespace AnyCompany.Data.Contract.Repositories
{
public interface IOrderRepository
{
void Add(Order order);
IEnumerable<Order> GetList();
}
}
81 changes: 81 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/AnyCompany.Data.Dapper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CEF90CEA-CEDF-44B4-9203-9B9ADF9A15CE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany.Data.Dapper</RootNamespace>
<AssemblyName>AnyCompany.Data.Dapper</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dapper, Version=1.60.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.1.60.6\lib\net451\Dapper.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Enums\ConnectionType.cs" />
<Compile Include="Factories\ConnectionFactory.cs" />
<Compile Include="Factories\IConnectionFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\CustomerRepository.cs" />
<Compile Include="Repositories\CustomerRepositoryWrapper.cs" />
<Compile Include="Repositories\OrderRepository.cs" />
<Compile Include="Sql\SqlStatements.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>SqlStatements.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany.Data.Contract\AnyCompany.Data.Contract.csproj">
<Project>{7F4D6121-26EF-48A2-A2F4-BBD5D114D8E7}</Project>
<Name>AnyCompany.Data.Contract</Name>
</ProjectReference>
<ProjectReference Include="..\AnyCompany.Models\AnyCompany.Models.csproj">
<Project>{695D4D06-2336-4B91-BA68-4A4924CF4729}</Project>
<Name>AnyCompany.Models</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Sql\SqlStatements.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>SqlStatements.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
8 changes: 8 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/Enums/ConnectionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnyCompany.Data.Dapper.Enums
{
public enum ConnectionType
{
CustomerDb = 0,
OrderDb = 1
}
}
27 changes: 27 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/Factories/ConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Data;
using System.Data.SqlClient;
using AnyCompany.Data.Dapper.Enums;

namespace AnyCompany.Data.Dapper.Factories
{
public class ConnectionFactory : IConnectionFactory
{
private readonly string _customerConnectionString;
private readonly string _orderDbConnectionString;

public ConnectionFactory(string customerConnectionString, string orderDbConnectionString)
{
_customerConnectionString = customerConnectionString;
_orderDbConnectionString = orderDbConnectionString;
}

public IDbConnection Create(ConnectionType connectionType)
{
var connectionString = connectionType == ConnectionType.CustomerDb
? _customerConnectionString
: _orderDbConnectionString;

return new SqlConnection(connectionString);
}
}
}
10 changes: 10 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/Factories/IConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Data;
using AnyCompany.Data.Dapper.Enums;

namespace AnyCompany.Data.Dapper.Factories
{
public interface IConnectionFactory
{
IDbConnection Create(ConnectionType connectionType);
}
}
36 changes: 36 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AnyCompany.Data.Dapper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AnyCompany.Data.Dapper")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cef90cea-cedf-44b4-9203-9b9adf9a15ce")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
23 changes: 23 additions & 0 deletions TechTest/AnyCompany.Data.Dapper/Repositories/CustomerRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using AnyCompany.Data.Dapper.Sql;
using AnyCompany.Models;
using Dapper;

namespace AnyCompany.Data.Dapper.Repositories
{
public static class CustomerRepository
{
public static Customer Load(int customerId)
{
var connectionString = ConfigurationManager.ConnectionStrings["CustomerConnectionString"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
return connection.Query<Customer>(SqlStatements.LoadCustomerById,
new {CustomerId = customerId}).FirstOrDefault();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using AnyCompany.Data.Contract.Repositories;
using AnyCompany.Data.Dapper.Enums;
using AnyCompany.Data.Dapper.Factories;
using AnyCompany.Data.Dapper.Sql;
using AnyCompany.Models;
using Dapper;

namespace AnyCompany.Data.Dapper.Repositories
{
public class CustomerRepositoryWrapper : ICustomerRepository
{
private readonly IConnectionFactory _connectionFactory;

public CustomerRepositoryWrapper(IConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
}

// using this to act as a proxy to the static CustomerRepository - I assumed this was legacy that
// we're trying to abstract and eventually replace
public Customer Load(int customerId)
{
return CustomerRepository.Load(customerId);
}

public IEnumerable<Customer> GetList()
{
using (var connection = _connectionFactory.Create(ConnectionType.CustomerDb))
{
connection.Open();
return connection.Query<Customer>(SqlStatements.GetAllCustomers).ToList();
}
}
}
}
Loading