Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Plant can create models using property setters; returns reasonable er…

…rors for invalid properties
  • Loading branch information...
commit 5ecaa7210f32fbba06cddd0bfafc26605c5f20bf 1 parent a0ba839
James Brechtel authored
View
5 .gitignore
@@ -0,0 +1,5 @@
+bin
+obj
+_ReSharper*
+*.ReSharper.user
+*.suo
View
50 Plant.Core/AbstractPlant.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Plant.Core
+{
+ public abstract class AbstractPlant
+ {
+ private readonly IDictionary<Type, object> defaultValuesByType = new Dictionary<Type, object>();
+
+ protected AbstractPlant()
+ {
+ Setup();
+ }
+
+ public virtual T Create<T>() where T : new()
+ {
+ var instance = new T();
+ if(defaultValuesByType.ContainsKey(typeof(T)))
+ SetProperties(defaultValuesByType[typeof(T)], instance);
+ return instance;
+ }
+
+ public virtual T Create<T>(object userSpecifiedProperties) where T : new()
+ {
+ var instance = Create<T>();
+ SetProperties(userSpecifiedProperties, instance);
+ return instance;
+ }
+
+ private static void SetProperties<T>(object propertyValues, T instance)
+ {
+ var properties = propertyValues.GetType().GetProperties().ToList();
+ properties.ForEach(property =>
+ {
+ var instanceProperty = instance.GetType().GetProperties().FirstOrDefault(prop => prop.Name == property.Name);
+ if(instanceProperty == null) throw new PropertyNotFoundException();
+ var value = property.GetValue(propertyValues, null);
+ instanceProperty.SetValue(instance, value, null);
+ });
+ }
+
+ protected virtual void Seed<T>(object defaults)
+ {
+ defaultValuesByType.Add(typeof(T), defaults);
+ }
+
+ public abstract void Setup();
+ }
+}
View
55 Plant.Core/Plant.Core.csproj
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Plant.Core</RootNamespace>
+ <AssemblyName>Plant.Core</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </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.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AbstractPlant.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="PropertyNotFoundException.cs" />
+ </ItemGroup>
+ <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>
View
35 Plant.Core/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+using System.Reflection;
+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("Plant.Core")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Plant.Core")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
+[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("20bc006c-9f78-4841-a651-1399f65ca210")]
+
+// 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("0.1.0.0")]
+[assembly: AssemblyFileVersion("0.1.0.0")]
View
8 Plant.Core/PropertyNotFoundException.cs
@@ -0,0 +1,8 @@
+using System;
+
+namespace Plant.Core
+{
+ public class PropertyNotFoundException : Exception
+ {
+ }
+}
View
56 Plant.Tests/AbstractPlantTest.cs
@@ -0,0 +1,56 @@
+using NUnit.Framework;
+using Plant.Core;
+using Plant.Tests.TestModels;
+
+namespace Plant.Tests
+{
+ [TestFixture]
+ public class AbstractPlantTest
+ {
+ [Test]
+ public void Should_Create_Instance_Of_Specified_Type()
+ {
+ Assert.IsInstanceOf(typeof(Person), new TestPlant().Create<Person>());
+ }
+
+ [Test]
+ public void Should_Create_Instance_With_Requested_Properties()
+ {
+ Assert.AreEqual("James", new TestPlant().Create<Person>(new { FirstName = "James"}).FirstName);
+ }
+
+ [Test]
+ public void Should_Use_Default_Instance_Values()
+ {
+ //this test relies on the setup of TestPlant below
+ Assert.AreEqual("Barbara", new TestPlant().Create<Person>().FirstName);
+ }
+
+ [Test]
+ [ExpectedException(typeof(PropertyNotFoundException))]
+ public void Should_Throw_PropertyNotFound_Exception_When_Given_Invalid_Property()
+ {
+ new TestPlant().Create<Person>(new { Foo = "Nothing" });
+ }
+
+ [Test]
+ public void Should_Setup_Type_Without_Defaults()
+ {
+ Assert.AreEqual("Toyota", new TestPlant().Create<Car>(new { Make = "Toyota"}).Make);
+ }
+
+ }
+
+ class TestPlant : AbstractPlant
+ {
+ public override void Setup()
+ {
+ Seed<Person>(new
+ {
+ FirstName = "Barbara"
+ });
+ }
+ }
+
+
+}
View
65 Plant.Tests/Plant.Tests.csproj
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{5E62668A-4944-4538-8CB7-98E09E018D56}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Plant.Tests</RootNamespace>
+ <AssemblyName>Plant.Tests</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </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="nunit.framework">
+ <HintPath>..\testlib\nunit.framework.dll</HintPath>
+ </Reference>
+ <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.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AbstractPlantTest.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="TestModels\Car.cs" />
+ <Compile Include="TestModels\Person.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Plant.Core\Plant.Core.csproj">
+ <Project>{9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}</Project>
+ <Name>Plant.Core</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <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>
View
36 Plant.Tests/Properties/AssemblyInfo.cs
@@ -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("Plant.Tests")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Plant.Tests")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
+[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("5ea962bf-0d0a-41bf-9ff1-f4ba22e44931")]
+
+// 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")]
View
7 Plant.Tests/TestModels/Car.cs
@@ -0,0 +1,7 @@
+namespace Plant.Tests.TestModels
+{
+ public class Car
+ {
+ public string Make { get; set; }
+ }
+}
View
7 Plant.Tests/TestModels/Person.cs
@@ -0,0 +1,7 @@
+namespace Plant.Tests.TestModels
+{
+ public class Person
+ {
+ public string FirstName { get; set; }
+ }
+}
View
26 Plant.sln
@@ -0,0 +1,26 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plant.Core", "Plant.Core\Plant.Core.csproj", "{9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plant.Tests", "Plant.Tests\Plant.Tests.csproj", "{5E62668A-4944-4538-8CB7-98E09E018D56}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9E2CBD60-B28B-45EF-A075-D3D13FA0D52B}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5E62668A-4944-4538-8CB7-98E09E018D56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5E62668A-4944-4538-8CB7-98E09E018D56}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5E62668A-4944-4538-8CB7-98E09E018D56}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5E62668A-4944-4538-8CB7-98E09E018D56}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
View
BIN  Plant.suo
Binary file not shown
View
0  README → README.txt
File renamed without changes
View
BIN  testlib/nunit.framework.dll
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.