-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Bean check columns in Getters (#25)
* Add ValidateGetColumns flag on Bean along with logic to throw if requesting a column not in the Bean * Break Bean creation out into BeanFactory, add BeanConfiguration layer for factory. Interfaces updated all round * Provision BeanFactory in tests for dispensing beans * Totally handle Bean configuration within Factory * Fix documentation and move BeanConfiguration to more appropriate part of Api * Add BeanFactoryTests class to check the effects of optional flags * Minor fix for website text * Rename BeanConfiguration getter to BeanOptions getter * Add BeanOptions to website body * Rename BeanConfiguration and BeanOptions and update all references * Fix broken test for BeanFactory * Slight refactor on Bean * Prevent Bean.Get from validating get columns twice closes #25
- Loading branch information
1 parent
bb6ac11
commit 8f9101b
Showing
18 changed files
with
275 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ packages/ | |
project.lock.json | ||
LimeBean.NuGet/*.nupkg | ||
LimeBean.NuGet/nuget.exe | ||
index.html | ||
index.html | ||
*.stackdump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Xunit; | ||
|
||
using LimeBean.Interfaces; | ||
using LimeBean.Exceptions; | ||
|
||
namespace LimeBean.Tests { | ||
|
||
public class BeanFactoryTests { | ||
|
||
[Fact] | ||
public void Dispense_ValidateGetColumns_Test() { | ||
IBeanFactory factory = new BeanFactory(); | ||
object one; | ||
object two; | ||
Bean bean; | ||
|
||
Func<bool, Bean> make = validateColumns => { | ||
factory.Options.ValidateGetColumns = false; | ||
Bean b = factory.Dispense("test"); | ||
Assert.Equal(typeof(Bean), b.GetType()); | ||
Assert.Equal(false, b.ValidateGetColumns); | ||
b.Put("one", 1); | ||
return b; | ||
}; | ||
|
||
// With ValidateGetColumns switched off | ||
bean = make(false); | ||
one = (int)bean["one"]; | ||
Assert.Equal(1, one); | ||
one = bean.Get<int>("one"); | ||
Assert.Equal(1, one); | ||
two = bean.Get<int>("two"); | ||
Assert.Equal(0, two); | ||
two = bean["two"]; | ||
Assert.Equal(null, two); | ||
|
||
// With ValidateGetColumns switched on | ||
bean = make(true); | ||
one = (int)bean["one"]; | ||
Assert.Equal(1, one); | ||
one = bean.Get<int>("one"); | ||
Assert.Equal(1, one); | ||
try { | ||
two = bean["two"]; | ||
} catch (Exception e) { | ||
Assert.IsType(typeof(ColumnNotFoundException), e); | ||
} | ||
try { | ||
two = bean.Get<int>("two"); | ||
} catch (Exception e) { | ||
Assert.IsType(typeof(ColumnNotFoundException), e); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using LimeBean.Interfaces; | ||
|
||
namespace LimeBean { | ||
|
||
internal class BeanFactory : IBeanFactory { | ||
internal BeanFactory() { } | ||
|
||
IBeanOptions _config; | ||
public IBeanOptions Options { | ||
get { | ||
if (_config == null) | ||
_config = new BeanOptions(); | ||
return _config; | ||
} | ||
} | ||
|
||
public Bean Dispense(string kind) { | ||
Bean bean = new Bean(kind); | ||
return ConfigureBean(bean); | ||
} | ||
|
||
public T Dispense<T>() where T : Bean, new() { | ||
T bean = new T(); | ||
return ConfigureBean(bean); | ||
} | ||
|
||
private T ConfigureBean<T>(T bean) where T : Bean { | ||
bean.Dispensed = true; | ||
bean.ValidateGetColumns = Options.ValidateGetColumns; | ||
return bean; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.