0

This is more of a theoretical question, but I was wondering what's the best way to pass information within forms. I'll explain my issue:

I have my Mainform class which manages the whole application:

public class Mainform : Form
{
        private static AddressBook _addressbook = new AddressBook();
        private static TemplateManager _templateManager = new TemplateManager();
        /*...*/
}

Furthermore, I have another class which is created by Mainform:

public partial class TemplateLists : Form
{
        //To be filled with Mainform's information.
        private List<Template> _genericTemplates;
        private Client _clientToFill;

        //In this case, I decided to pass the information through the constructor.
        public TemplateLists(List<Template> genericTemplates, Client client)
        {
            InitializeComponent();
            _genericTemplates = genericTemplates;
            _clientToFill = client;
        }
}

The issue is that, in order for TemplateLists to recieve the _genericTemplates information, I don't know if it's best done through the constructor, a method or public properties and why. In any case, I know how to implement them all, but I don't know which is best and I don't have any reasons to pick one over another.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
Javier García Manzano
  • 1,024
  • 2
  • 12
  • 25

3 Answers3

0

Better to use Dependency Injection or Service Location. Check this and this for reference.

Community
  • 1
  • 1
Yahya
  • 3,386
  • 3
  • 22
  • 40
  • Why should I used the patterns over a simple solution like passing values through the constructor? – Javier García Manzano Oct 19 '15 at 11:18
  • @JavierGarcíaManzano It's all down to flexibility and modularity it provides. With DI, you define it once, and also say if you want it singleton or not. And you don't have to manually set it again. – Yahya Oct 19 '15 at 13:10
0

There is no such thing as "the best" solution. It really comes down to how the form is used. For example it depends what the generic templates are. If they are necessary for the form to be created, it's a good idea to pass it via constructor to prevent that the form is instanciated incomplete.

If they are optional you could assign the after the form is created.

The implementation details (dependency injection, concrete coupling, etc.) depends on how the form is going to be used.

DerApe
  • 3,097
  • 2
  • 35
  • 55
0

Basically your whole question can be brought down to when should one use constructor (ctor) parameters vs properties.

Constructor parameters:
These should be used to set mandatory values on your instance. In other words these are the values without which your class instance cannot function.

Class properties:
These should be used when the values are optional for your object's functioning.

Consider an example, wherein your class pulls data via a service (which in turn talks to database etc). Also you intend to perform some sort of logging. In this case, you know that your class will not work without a service instance but you logging can be optional for this class. So while instantiating StoreManager you have option to set logger if you wish.

public class StoreManager
{
    private readonly IService dataService;

    public StoreManager(IService dataService) 
    {
        if(dataService == null)
        {
            // Do not allow to go further.
            throw new ArgumentException();
        }

        this.dataService = dataService;
    }

    public ILogger Logger
    {
        get;
        set;
    }

    public IList<Product> GetProducts()
    {
        var products = dataService.GetProducts();

        // logging is optional
        if(Logger != null) {
            Logger.Trace("Products fetched {0}", products.Count);
        }
    }
}
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • Ok, that kind of explains it. At some point I had read that it wasn't a good idea to work with constructors because they were "auto-generated" by VS or things of the kind, so I got confused. – Javier García Manzano Oct 19 '15 at 11:17