loresoft / datagenerator Goto Github PK
View Code? Open in Web Editor NEWAutomatically generate data for an object
License: MIT License
Automatically generate data for an object
License: MIT License
I've been using DataGenerator to do things like:
var data = DataGenerator.Generator.Default.Single<MyType>();
But sometimes I need to be able to do this:
class MyType
{...}
class MyTypeMocked : MyType
{
void MyTypeMocked()
{
//initialize the class in place here, not via object creation by DataGenerator
}
}
Is there any way to use DataGenerator to update the current object in this way?
How to generate random text to specific character
snippet from your code
bool isReadonly = !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral;
if (!isReadonly)
_setter = new Lazy<Action<object, object>>(() => DelegateFactory.CreateSet(_fieldInfo));
HasSetter = !isReadonly;
I stumbled on this when trying to seed data into my database using linqpad connecting with LinqToSql. I had to override MappingProfile.Property to allow for Fields. When I did I was getting an exception that there is no setter.
I spit out the all the fields of the class and all of them had IsInitOnly = false
and IsLiteral = false
. So as a test I made the following test
void Main()
{
//Setup();
object a = new ClientSurveyItem();
a = new Clown();
var fieldInfo = a.GetType().GetField("Section");
bool isReadonly = !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral;
fieldInfo.Dump($"isReadonly:{isReadonly}");
//var clientSurveyItem = _dataManager.CreateClientSurveyItem().Result;
}
class Clown
{
public string Section;
}
my output is isReadonly:True
yet I can very easily do a.Section = "somethign";
tried the same thing with a struct and I get same results
The class that I am generating my random data from has nullable numeral types and the IntegerSource
and DecimalSource
does not like them. So I tried using Convert
but I get a runtime error that says...
The expression is not a member access expression.
Is there any way around this other than changing my properties?
I believe it would be useful (certainly for my own use but probably for others) if this library supported the ability to provide the seed used for the Random() RNG. This would allow test data to be duplicated simply by tracking the seed that was used to generate the data instead of having to hold onto all of the data generated at the time.
My initial thoughts on how this would work:
Generator.Default.Configure(c => c
.Seed(133)
.Entity<User>(e =>
{
e.Property(p => p.FirstName).DataSource<FirstNameSource>();
e.Property(p => p.LastName).DataSource<LastNameSource>();
e.Property(p => p.Address1).DataSource<StreetSource>();
e.Property(p => p.City).DataSource<CitySource>();
e.Property(p => p.State).DataSource<StateSource>();
e.Property(p => p.Zip).DataSource<PostalCodeSource>();
e.Property(p => p.Note).DataSource<LoremIpsumSource>();
e.Property(p => p.Password).DataSource<PasswordSource>();
// array of values
e.Property(p => p.Status).DataSource(new[] { Status.New, Status.Verified });
// don't generate
e.Property(p => p.Budget).Ignore();
// static value
e.Property(p => p.IsActive).Value(true);
// delegate value
e.Property(p => p.Created).Value(() => DateTime.Now);
})
);
var users = Generator.Default.List<User>(10);
This would produce the exact same results each time. I will look into creating a PR for this support if you are willing to accept PRs. At first glance though, it appears to be non-trivial due to the static ThreadLocal<Random>
usage for the RNGs. I believe this is just to keep your library thread safe but that your library doesn't actually create any threads?
If no seed is configured then it could default to the way things are done now. If a seed IS provided and there are multiple threads or at minimum things must remain thread safe, it could be handled in a few different ways:
RandomGenerator
class. There would be a theoretical performance hit due to the lock but I believe it would be negligible and could be benchmarked to compare.GetNextSeed()
where each ThreadLocal would call that to get its seed.Hi, I want ignore some properties of entity, but i don't know how.
I would grateful if you can help me.
Thx.
When your model contains a property of type ICollection, the property cannot be assigned from the generator. It results in an exception mentioning "the object should implement IConvertible". This happens when the Convert.ChangeType is hit.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.