Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangodong committed Dec 20, 2015
1 parent cf96522 commit 909983f
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SimpleObjectFactory/bin
SimpleObjectFactory/obj
SimpleObjectFactory.Test/bin
SimpleObjectFactory.Test/obj
107 changes: 107 additions & 0 deletions SimpleObjectFactory.Test/ObjectFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SimpleObjectFactory.Test
{
[TestClass]
public class ObjectFactory
{

[TestMethod]
public void ImplementationOnly()
{

var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType<Implementation>();

var service = serviceFactory.GetInstance<Implementation>();
Assert.IsNotNull(service);

}

[TestMethod]
public void CorrectDerivative()
{

var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType<IInterface, Implementation>();

var service = serviceFactory.GetInstance<IInterface>();
Assert.IsNotNull(service);

}

[TestMethod]
public void ParameterlessCtor()
{

var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType<ClassWithParameterlessCtor, ClassWithParameterlessCtor>();

var service = serviceFactory.GetInstance<ClassWithParameterlessCtor>();
Assert.IsNotNull(service);

}

[TestMethod]
public void ParameterizedCtor()
{

var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType<ClassWithParameterlessCtor, ClassWithParameterlessCtor>();
serviceFactory.RegisterType<ClassWithParameterizedCtor, ClassWithParameterizedCtor>();

var service = serviceFactory.GetInstance<ClassWithParameterizedCtor>();
Assert.IsNotNull(service);
Assert.IsNotNull(service.Parent);

}

[TestMethod]
public void UndeclaredParameterCtor()
{

var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType<ClassWithParameterizedCtor, ClassWithParameterizedCtor>();

var service = serviceFactory.GetInstance<ClassWithParameterizedCtor>();
Assert.IsNull(service);

}

[TestMethod]
public void StaticInstance()
{
var expected = new Implementation();
var serviceFactory = new SimpleObjectFactory.ObjectFactory();
serviceFactory.RegisterType(expected);

var actual = serviceFactory.GetInstance<Implementation>();

Assert.IsNotNull(actual);
Assert.AreSame(expected, actual);
}

private interface IInterface
{
}

private class Implementation : IInterface
{
}

private class ClassWithParameterlessCtor
{
}

private class ClassWithParameterizedCtor
{
public ClassWithParameterlessCtor Parent { get; set; }

public ClassWithParameterizedCtor(ClassWithParameterlessCtor parent)
{
Parent = parent;
}
}

}
}
36 changes: 36 additions & 0 deletions SimpleObjectFactory.Test/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("SimpleObjectFactory.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimpleObjectFactory.Test")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[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("9d500b3a-c6a3-4d58-b823-5357a6535d84")]

// 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")]
89 changes: 89 additions & 0 deletions SimpleObjectFactory.Test/SimpleObjectFactory.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9D500B3A-C6A3-4D58-B823-5357A6535D84}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SimpleObjectFactory.Test</RootNamespace>
<AssemblyName>SimpleObjectFactory.Test</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</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" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ObjectFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SimpleObjectFactory\SimpleObjectFactory.csproj">
<Project>{58d9f88d-1877-466a-ab71-ef00ffc596a6}</Project>
<Name>SimpleObjectFactory</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
7 changes: 7 additions & 0 deletions SimpleObjectFactory/IObjectCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimpleObjectFactory
{
public interface IObjectCreator
{
object GetService(ObjectFactory factory);
}
}
52 changes: 52 additions & 0 deletions SimpleObjectFactory/ObjectFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;

namespace SimpleObjectFactory
{
public class ObjectFactory
{

private readonly Dictionary<Type, IObjectCreator> registry = new Dictionary<Type, IObjectCreator>();

public void RegisterType<TService, TImplementation>() where TImplementation : TService
{
if (!typeof(TService).IsAssignableFrom(typeof(TImplementation)))
{
throw new ArgumentException($"{typeof(TService).Name} is not assignable from {typeof(TImplementation).Name}");
}

if (registry.ContainsKey(typeof(TService)))
{
throw new ArgumentException($"{typeof(TService).Name} has been added previously");
}

registry.Add(typeof(TService), new SingletonObjectCreator<TImplementation>());
}

public void RegisterType<TImplementation>()
{
RegisterType<TImplementation, TImplementation>();
}

public void RegisterType<TImplementation>(TImplementation implementation)
{
registry.Add(typeof(TImplementation), new PrebuiltObjectCreator(implementation));
}

public TContract GetInstance<TContract>() where TContract : class
{
return GetInstance(typeof(TContract)) as TContract;
}

public object GetInstance(Type type)
{
if (registry.ContainsKey(type))
{
return registry[type].GetService(this);
}

return null;
}

}
}
17 changes: 17 additions & 0 deletions SimpleObjectFactory/PrebuiltObjectCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SimpleObjectFactory
{
internal class PrebuiltObjectCreator : IObjectCreator
{
private readonly object instance;

public PrebuiltObjectCreator(object instance)
{
this.instance = instance;
}

public object GetService(ObjectFactory factory)
{
return instance;
}
}
}
36 changes: 36 additions & 0 deletions SimpleObjectFactory/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("SimpleObjectFactory")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimpleObjectFactory")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[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("58d9f88d-1877-466a-ab71-ef00ffc596a6")]

// 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")]
Loading

0 comments on commit 909983f

Please sign in to comment.