Comments (9)
Update: exception goes away if I explicitly ignore all properties I don't set up.
from objectfiller.net.
Hey,
first of all, thank you for using ObjectFiller!
Could it be that you have a circular reference in your class somehow?
I show u what i mean:
class Program
{
static void Main(string[] args)
{
Filler<TestClass> filler = new Filler<TestClass>();
var foo = filler.Create();
}
}
public class TestClass
{
public string Name { get; set; }
public int Bla { get; set; }
public ICollection<TestClass2> TestClasses { get; set; }
}
public class TestClass2
{
public TestClass Recursion { get; set; }
}
The TestClass has a property which contains an ICollection of TestClass2. TestClass2 contains a property of type TestClass. The Problem is that ObjectFiller creates TestClass2 instances and after this it creates again TestClass instances which will create again TestClass2 instances, which will create again TestClass instances and so on! It is an endless circle with the result of a stackoverflow. If this is somehow your case than i know this issue but i have actually no idea how to find out if there is a circular dependency in the class structure. Maybe with a kind of breadcrumb or so. You can avoid this by ignore the property which causes the circle. Like this:
class Program
{
static void Main(string[] args)
{
Filler<TestClass> filler = new Filler<TestClass>();
filler.Setup()
.SetupFor<TestClass2>()
.OnProperty(x => x.Recursion).IgnoreIt();
var foo = filler.Create();
}
}
public class TestClass
{
public string Name { get; set; }
public int Bla { get; set; }
public ICollection<TestClass2> TestClasses { get; set; }
}
public class TestClass2
{
public TestClass Recursion { get; set; }
}
If this is not your case, is it possible for you to give me your class structure and the setup of your ObjectFiller that i'm able to reconstruct this issue?
And because of your question with the setup manager... The SetupManager finds the right setup for a specific type. So by default the settings of the Objectfiller gets inherited. So for example this one:
class Program
{
static void Main(string[] args)
{
Filler<TestClass> filler = new Filler<TestClass>();
filler.Setup()
.OnType<int>().Use(() => 1)
.SetupFor<AnotherClass>();
var foo = filler.Create();
}
}
public class TestClass
{
public int Number { get; set; }
public ICollection<AnotherClass> Foo { get; set; }
}
public class AnotherClass
{
public int AnotherNumber { get; set; }
}
With this setup i say: Every property of type int should be a 1! So the property "Number" in the "TestClass" will be 1 and the "AnotherNumber" in "AnotherClass" will be also 1. But when i change .SetupFor<AnotherClass>();
to .SetupFor<AnotherClass>(true);
, it means that the ObjectFiller uses the defaultsettings for Anotherclass and does not inherit the settings from the parent (TestClass). This means that the "AnotherNumber" in the "AnotherClass" is now a random int number, but the "Number" in the "TestClass" is still a 1! So you are able to whether inherit your settings through your class hierarchy or to be more specific on types you want to be more specific. Or did i misunderstand your question?
Cheers,
Roman
from objectfiller.net.
Yup, I suppose I had circular references but I did have IgnoreAllOfUnknownType specified in the config so I expected it not to matter :) As a suggestion, it might be nice if there was a way to make that the default somehow - i.e. don't touch properties other than what I've set up.
As for the SetupManager stuff; from what I could tell, if I do something like
var x = new Filler().Setup().......
var y = new Filler().Setup()......
because of the call to SetupManager.Clear in the second constructor, the setup I did for x is pretty much lost and it reverts to the default behavior. I might be wrong though and did something else wrong! I'll fork your code and have a closer look when I have a bit more time :)
from objectfiller.net.
Interesting ideas! But why you want to create two Filler instances?
You can do something like that:
class Program
{
static void Main(string[] args)
{
Filler<Person> filler = new Filler<Person>();
filler.Setup()
.OnProperty(x => x.FirstName).Use(new RealNames(RealNameStyle.FirstNameOnly))
.OnProperty(x => x.LastName).Use(new RealNames(RealNameStyle.LastNameOnly));
Person firstPerson = filler.Create();
Person secondPerson = filler.Create();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Just call filler.Create() two times!
The other idea to ignore all non configured items is a good one! I will create a issue for that!
Thank you!
from objectfiller.net.
Well the two fillers are for different types, not necessarily related to one another :)
from objectfiller.net.
Thought I'd describe a bit what I'm doing, i.e. what I'm using ObjectFiller for right now; maybe we have very different scenarios in mind.
I've started a very small project and I simply want to populate the database with a bunch of objects containing some random data. It's not the first time I've searched for a small library that does just this, and this time I found this one that looked promising :)
I'm using EF code first so naturally I'm just creating a bunch of objects and letting EF save them. ObjectFiller does exactly what I want for something like a User class for instance, with first and last names and so on. For something like a user's address, which is stored in a separate object (let's say UserDetails) I can of course use the PatternGenerator.
But I also have some other objects I want to create, and I'm doing all of this in the seed method of my database initializer so it'd be handy if I just defined the rules first (since it's for several types it can easily get to 100-200 lines of code with all the rules for each property of each type) and then just use the fillers I defined to creatively populate some objects.
Of course I could get it working by just instantiating fillers exactly when I needed them, but it'd be nice if I separated my concerns a little more :)
from objectfiller.net.
Hmm, i think i have a good solution. I will create a branch for this in the next days when i find some time.
It will look something like that:
[TestMethod]
public void TestFillerSettings()
{
Filler<Person> filler = new Filler<Person>();
FillerSettings fillerSettings = filler.Setup()
.OnType<IAddress>().CreateInstanceOf<Address>()
.OnProperty(x => x.Age).Use(new IntRange(18, 32))
.OnProperty(x => x.FirstName).Use(new RealNames(RealNameStyle.FirstNameOnly))
.Result;
Filler<Person> otherFiller = new Filler<Person>();
otherFiller.Setup(fillerSettings);
Person p = otherFiller.Create();
}
So you make your setup like you do it everytime. When you have finished the setup, you call "Result". This will give you the whole FillerSettings! Then i will create an overload for the Setup method, which takes the FillerSettings. After this you can call directly Create or Fill or continue setup the filler based on the FillerSettings.
I think i like that!
Next steps could be that the FillerSettings are serializable to XML or JSON! Then you can save the settings somewhere to share them with others for example...
from objectfiller.net.
Yup, that sounds good as well. Won't solve the SetupManager getting nuked as part of the constructor of the Filler though, which is what disturbs me the most. I'm doing a pull request right now with a suggestion for that, and a couple of other PRs with... stuff. Let me know what you think ;)
from objectfiller.net.
Will get fixed in #22 !
from objectfiller.net.
Related Issues (20)
- Randomizer for type URI is missing
- Allow to specify a seed to get allways the same data. HOT 2
- Configuration by template object
- Give us control over Collection counts HOT 1
- Feature request: More range
- Implement phone number generator HOT 2
- Support for immutable types
- Make copy of existing object with one or more property changes. HOT 1
- .Net 5.0? HOT 3
- Invalid DateTime values due to daylight savings time HOT 3
- Support for IDictionary HOT 3
- Properties on base classes not populated beyond 2 levels HOT 4
- Object reference not set to instance of an object when using records HOT 7
- Automatically fill a type that inherits from List<T> HOT 2
- ObjectFiller fails to fill IReadOnlyCollection HOT 1
- Add support for using an interface when creating an instance of Filler<T> HOT 3
- Merging setup information based on inheritance and implementation
- Exception when creating a collection of simple objects
- Please write Type overload for methods too
- Please add unlimited mergeable setup HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objectfiller.net.