Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to pass control values from form to separate class

Tags:

c#

winforms

I have a WinForms application that creates a huge, custom textfile. To do this requires the user to configure dozens of controls on the main form and then click a Submit button. What is the best way to pass all those control values to my Generator class? The problem is I'm ending up with 20+ parameters to pass to a Generate() method. I can group some of those into Config objects (i.e. the date/time controls can be passed into a DateTimeConfig object) and then pass these config objects into the Generate() method to minimize the number of parameters needed, but it still feels like there must be a better way. Any ideas?

EDIT: Thank you for your responses, but I was hoping for something other than a configuration object (as that's what my OP mentioned). I guess what I was hoping for is some way to serialize the values of all the controls automatically, without having to build a custom object and then modify that everytime a control changes or gets added/removed.

like image 952
BrazenTongue Avatar asked Feb 23 '23 22:02

BrazenTongue


2 Answers

Why not create a custom object class as container of your data and then pass the istance of this class to the Generator method as parameter?

like image 151
danyolgiax Avatar answered Apr 08 '23 02:04

danyolgiax


Look into Model Driven Development, where the parameters are grouped into business objects or data transfer objects (DTOs). This is a more intuitive way to group parameters than by datetime, etc.

If you place those model objects in a separate project, you can pass those objects around the solution (from one method to another and one project to another) without having to serialize them or mess with long parameter list ordering. You can even nest model objects inside other model objects.

In this example, notice you only have to change parameters in the Person object or the Address object and you never have to serialize or map anything:

// this is my business model object
public class Person
{
    public int PersonId { get; set; }
    public string PersonName { get; set; }

    // notice the Address object nested in the Person class
    public Address HomeAddress { get; set; }
}

// this is another class that lives inside the person class
public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

In one class you could have this method that receives a person with an address and passes the person to another class's SavePerson class:

public void ProcessPerson(Person person)
{
    person.PersonName = "Robert";

    Address address = new Address();
    address.City = "Austin";

    person.Address = address;

    SavePerson(person);
}

Some other class (even in another project) can take that person and pass it to the persistence layer:

public void SavePerson(Person person)
{
    database.Save(person);
}

So if I want to add a State to the person I just change the Address class and I'm all done in one step, I don't have to add the state field to all the method parameter lists:

 public class Address
 {
    public string Street { get; set; }
    public string City { get; set; }

    //  this is the new property
    public string State  { get; set; }
 }
like image 25
Robert Corvus Avatar answered Apr 08 '23 02:04

Robert Corvus