GithubHelp home page GithubHelp logo

Comments (9)

GothikX avatar GothikX commented on May 29, 2024

Update: exception goes away if I explicitly ignore all properties I don't set up.

from objectfiller.net.

Tynamix avatar Tynamix commented on May 29, 2024

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.

GothikX avatar GothikX commented on May 29, 2024

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.

Tynamix avatar Tynamix commented on May 29, 2024

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.

GothikX avatar GothikX commented on May 29, 2024

Well the two fillers are for different types, not necessarily related to one another :)

from objectfiller.net.

GothikX avatar GothikX commented on May 29, 2024

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.

Tynamix avatar Tynamix commented on May 29, 2024

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.

GothikX avatar GothikX commented on May 29, 2024

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.

Tynamix avatar Tynamix commented on May 29, 2024

Will get fixed in #22 !

from objectfiller.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.