Are there any tools to populate class properties w

2020-02-07 01:17发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 2 years ago.

What I'd like to do is create a class with some attributes on different properties, pass that class to another that will set the properties with appropriate random data... here in pseudo code:

public class Customer
{
   [Attribute("FirstName")]
   private string CustomerFirstName;
   public {get;set} //etc

   [Attribute("LastName")]
   private string CustomerLastName;
   public {get;set;} //etc

   [Attribute("DateTime")]
   private DateTime CustomerSignUpDate;
   public DateTime {get;set;} //yadda

   [Attribute("Phone")]
   private string CustomerPhone;
   public string {get;set;} //yadda
}

And then do like this

IList<Customer> CustomerList=ClassFillerOutClass(new Customer(),5);

And the result would be a List of 5 Customers that have appropriate 'random' data in their properties.

If this doesn't exist...I guess I could start a project myself to do...I just don't want to reinvent the wheel if it's not necessary.

EDIT: I forgot a piece. I'm looking to use this as a test tool. So in the example above I could quickly create a list of 5 customers with random but appropriate values. And then say pass that to my persistence method, and have something I can check against. I'm trying to avoid manually creating a populated object everytime for my TDD purposes.

EDIT 2: Ok so I started rolling my own...I'll post it on Codeplex this weekend and link it here...I clearly won't be done but it'll be a start if anyone else wants to work on it.

回答1:

Ok...so I never found one....so I decided to start my own. Check out: Object Hydrator.

The project is presently a quick spike...but I think it has potential...let me know if you have ideas how to make it better.



回答2:

I tried AutoFixture (http://autofixture.codeplex.com/) and it worked well for me. I was able to generate an object with a deep hierarchy of children in one line of code.



回答3:

Nowadays you have also NBuilder ( nbuilder.org ) that does the same.

I don't know if both projects are linked.



回答4:

I don't know your goals here, but I'll keep this close to the code level. This approach may not work for you, but it has in the past for me.

You can generate random data and place it into your database. There are a few commercial at-cost products that do this. The one I use is SQL Data Generator by RedGate.

With that data in hand, you can do some text manipulations in SQL to convert columnar data:

Table Customer
FirstName | LastName | SignUpDate | Phone
Bob         Smith      1/2/2009     555-555-1212
Jane        Doe        9/11/2009    555-300-1334
...

Into:

new Customer () 
{
  CustomerFirstName = "Bob", 
  CustomerLastName = "Smith", 
  CustomerSignUpDate = DateTime.Parse("1/2/2009"), 
  Phone = "555-555-1212"
},
new Customer () 
{
  CustomerFirstName = "Jane", 
  CustomerLastName = "Doe", 
  CustomerSignUpDate = DateTime.Parse("9/11/2009"), 
  Phone = "555-300-1334
},

and wrap this into the following using a text editor (this is C# 3.0 syntax):

public class FakeCustomerRepository
{
  private IList<Customer> m_Customers = new List<Customer>()
               {
                  [insert SQL text transform here]
               };

  public Customer Get(...)
  {
    return m_Customers.Find(...);
  }
}

Again, I don't know your needs and whether you need random data at runtime. The approach described above is only helpful if you want to generate a lot of data quickly and statically.



回答5:

All of the object fillers suggested do not work for my complex objects.

I have super complex nested objects with arrays with nested arrays and this is half way working for me. http://hydrator.codeplex.com/

I fills everything but the arrays.



回答6:

you can use Object Hydrator in c#

URL http://objecthydrator.codeplex.com/

and use like

public Employee GetEmployee()
{
     return new Hydrator<Employee>().GetSingle();
}

public IList< Employee> GetEmployees()
{
 return new Hydrator<Employee>().GetList(100);
}

Anand More