Please explain how object instantiation works here whilst it's not being used - seemingly

49 Views Asked by At

For the code below, how does object mainPresenter or new Presenter(mainView, connectionString)even work here? The instance is never used. This code is part of a MVP (passive view) in WinForms project I'm doing for practice. I see many people do this but where does the instantiated Presenter object go? What's happening here?

using PresentationLayer.Presenters;

namespace PresentationLayer
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();

            //--------------------------------------------

            string connectionString = "Data Source=.;Initial Catalog=Music;Integrated Security=True";
            IMainView mainView = new MusicForm();
            Presenter mainPresenter = new Presenter(mainView, connectionString);

            //--------------------------------------------
            Application.Run((Form)(mainView));
        }
    }
}

Here's the presenter just in case one needs it:

using Models;
using ServiceLayer;

namespace PresentationLayer.Presenters
{
    public class Presenter
    {
        private readonly IMainView _mainView;
        private readonly IAlbumRepository _albumRepository;
        private IEnumerable<Album>? _albumList;
        private readonly BindingSource _bindingSource;

        //------------------------------------------------------------
        public Presenter(IMainView mainView, string connectionString) 
        {
            _bindingSource = new BindingSource();
            _mainView = mainView;
            _albumRepository = new AlbumRepository(connectionString);
            _mainView.GetAll += GetAll;
            _mainView.SearchInAlbums += SearchInAlbums;
        }

        //--------------- main functionality --------------------------

        private void GetAll(object? sender, EventArgs e)
        {
            _albumList = _albumRepository.GetAll();
            SetBindingSource(_albumList);
        }

        private void SearchInAlbums(object? sender, string fndAlbumsByPhrase)
        {
            _albumList = _albumRepository.SearchByAlbum(fndAlbumsByPhrase);
            SetBindingSource(_albumList);
        }


        //------------------helpers / utilities ------------------------

        private void SetBindingSource(IEnumerable<Album> coll)
        {
            _bindingSource.DataSource = _albumList;
            _mainView.SetAlbumsBindingSource(_bindingSource);
        }
    }
}
0

There are 0 best solutions below