Skip to content

Commit 8a411b9

Browse files
gokTyBalDgokTyBalD
authored andcommitted
Changed fill API
1 parent 01cb24f commit 8a411b9

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

ObjectFiller.Test/ObjectFillerTest.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ObjectFillerTest
1616
public void TestFillPerson()
1717
{
1818
Person p = new Person();
19-
ObjectFiller<Person> objectFiller = new ObjectFiller<Person>(p);
19+
ObjectFiller<Person> objectFiller = new ObjectFiller<Person>();
2020
objectFiller.Setup()
2121
.RegisterInterface<IAddress, Address>()
2222
.RandomizerForType(new MnemonicStringPlugin(10))
@@ -26,11 +26,7 @@ public void TestFillPerson()
2626
.SetupFor<Address>()
2727
.IgnoreProperties(a => a.City, a => a.Country);
2828

29-
30-
31-
Person pFilled = objectFiller.Fill();
32-
33-
29+
Person pFilled = objectFiller.Fill(p);
3430
}
3531
}
3632
}

ObjectFiller/ObjectFiller.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
54
using System.Linq;
65
using System.Reflection;
76

87
namespace ObjectFiller
98
{
109
public class ObjectFiller<T> where T : class
1110
{
12-
private T _objectToFill;
13-
14-
public ObjectFiller(T objectToFill)
15-
: this()
16-
{
17-
_objectToFill = objectToFill;
18-
}
19-
2011
public ObjectFiller()
2112
{
2213
SetupManager.Clear();
@@ -49,23 +40,21 @@ public T Fill(ObjectFillerSetup setup)
4940
/// </summary>
5041
public T Fill()
5142
{
52-
try
53-
{
54-
if (_objectToFill == null)
55-
{
56-
_objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());
57-
}
43+
T objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());
5844

59-
Fill(_objectToFill);
45+
Fill(objectToFill);
6046

47+
return objectToFill;
48+
}
6149

62-
return _objectToFill;
63-
}
64-
finally
65-
{
66-
_objectToFill = null;
67-
}
50+
/// <summary>
51+
/// Fills your object instance. Call this after you finished your setup with the FluentAPI
52+
/// </summary>
53+
public T Fill(T instanceToFill)
54+
{
55+
FillInternal(instanceToFill);
6856

57+
return instanceToFill;
6958
}
7059

7160

@@ -106,7 +95,7 @@ private object CreateInstanceOfType(Type type, ObjectFillerSetup currentSetup)
10695
}
10796

10897

109-
private void Fill(object objectToFill)
98+
private void FillInternal(object objectToFill)
11099
{
111100
var currentSetup = SetupManager.GetFor(objectToFill.GetType());
112101

@@ -179,7 +168,7 @@ private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
179168
{
180169
object result = CreateInstanceOfType(type, currentSetup);
181170

182-
Fill(result);
171+
FillInternal(result);
183172
return result;
184173
}
185174

@@ -272,7 +261,7 @@ private object GetInterfaceInstance(Type interfaceType, ObjectFillerSetup setup)
272261
MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { interfaceType });
273262
result = genericMethod.Invoke(setup.InterfaceMocker, null);
274263
}
275-
Fill(result);
264+
FillInternal(result);
276265
return result;
277266
}
278267

0 commit comments

Comments
 (0)