Configuration

Unlike a lot of object generation frameworks, configuration is a large part of what drives AutoPoco.

Generating plain old objects with completely empty properties might work for a lot of tests, but it's also useful to have meaningful data attached to generated objects, for example:
  • Email addresses
  • First/last names
  • Identity columns
  • Postcodes
  • Addresses

While the simplest configuration of AutoPoco allows for the scanning of an assembly and population of properties/fields by convention, it is possible to start with that and build up what you consider to be meaningful data over time.

Here is an example of our simple user class being given specific details about itself in the configuration step:

mFactory = AutoPocoContainer.Configure(x =>
{
    x.Conventions(c =>
    {
        c.UseDefaultConventions();
    });

    x.AddFromAssemblyContainingType<SimpleUser>();
    x.Include<SimpleUser>()
        .Setup(c => c.EmailAddress).Use<EmailAddressSource>()
        .Setup(c => c.FirstName).Use<FirstNameSource>()
        .Setup(c => c.LastName).Use<LastNameSource>()
        .Invoke(c => c.SetPassword(Use.Source<String, PasswordSource>()));
    x.Include<SomeType>()
        .Setup(c => c.SomeString).Use<RandomStringSource>(5,10);
});

  • Each property that wants defined data has been allocated a data source.
  • This data source will be instantiated when a session is created from a configured factory
  • SetPassword will be invoked with a value from the PasswordSource on object creation
  • Every time a SimpleUser is requested, the data source will be asked for the "next" value and that will be placed in the SimpleUser object
  • During object creation, calls to "Impose" can override the Datasource provided values
  • SomeType uses a RandomStringSource, which will generate a random string between 5-10 characters in length
    • (therefore demonstrating that parameters can be passed into data sources)

A complete list of AutoPoco provided data sources can be found here
  • Note: Shortcuts to certain data sources are exposed as extension methods on certain types
    • On strings, Random(int min, int max) will set the property to a random length between min and max
    • On all types, Value(Object obj) will set the property to some hard coded value

Last edited Apr 15, 2010 at 9:24 AM by robashton, version 7

Comments

No comments yet.