Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions ObjectFiller.Test/ObjectFillerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ObjectFillerTest
public void TestFillPerson()
{
Person p = new Person();
ObjectFiller<Person> objectFiller = new ObjectFiller<Person>(p);
ObjectFiller<Person> objectFiller = new ObjectFiller<Person>();
objectFiller.Setup()
.RegisterInterface<IAddress, Address>()
.RandomizerForType(new MnemonicStringPlugin(10))
Expand All @@ -26,11 +26,7 @@ public void TestFillPerson()
.SetupFor<Address>()
.IgnoreProperties(a => a.City, a => a.Country);



Person pFilled = objectFiller.Fill();


Person pFilled = objectFiller.Fill(p);
}
}
}
39 changes: 14 additions & 25 deletions ObjectFiller/ObjectFiller.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace ObjectFiller
{
public class ObjectFiller<T> where T : class
{
private T _objectToFill;

public ObjectFiller(T objectToFill)
: this()
{
_objectToFill = objectToFill;
}

public ObjectFiller()
{
SetupManager.Clear();
Expand Down Expand Up @@ -49,23 +40,21 @@ public T Fill(ObjectFillerSetup setup)
/// </summary>
public T Fill()
{
try
{
if (_objectToFill == null)
{
_objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());
}
T objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());

Fill(_objectToFill);
Fill(objectToFill);

return objectToFill;
}

return _objectToFill;
}
finally
{
_objectToFill = null;
}
/// <summary>
/// Fills your object instance. Call this after you finished your setup with the FluentAPI
/// </summary>
public T Fill(T instanceToFill)
{
FillInternal(instanceToFill);

return instanceToFill;
}


Expand Down Expand Up @@ -106,7 +95,7 @@ private object CreateInstanceOfType(Type type, ObjectFillerSetup currentSetup)
}


private void Fill(object objectToFill)
private void FillInternal(object objectToFill)
{
var currentSetup = SetupManager.GetFor(objectToFill.GetType());

Expand Down Expand Up @@ -179,7 +168,7 @@ private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
{
object result = CreateInstanceOfType(type, currentSetup);

Fill(result);
FillInternal(result);
return result;
}

Expand Down Expand Up @@ -272,7 +261,7 @@ private object GetInterfaceInstance(Type interfaceType, ObjectFillerSetup setup)
MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { interfaceType });
result = genericMethod.Invoke(setup.InterfaceMocker, null);
}
Fill(result);
FillInternal(result);
return result;
}

Expand Down