Help me handle the business logic from form.



  • I'm just starting to learn C#. I'm trying to understand the design of the database application. Practically every time I hear the phrase "business logic must exist separate from form." But I don't quite understand how to do this in WinForms programming?

    Please provide comprehensive guidance or literature in this regard.

    UPD:

    To date, it has been understood that the use of WinForms requires the use of MVP pathers. And the only example of using the MVP for WinForm, which was able to build a working application, I was able to find this one. https://ru.stackoverflow.com/questions/413445/%D0%9A%D0%B0%D0%BA-%D0%BD%D0%B0%D1%87%D0%B0%D1%82%D1%8C-%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F-mvp-winforms ♪ Next to the information in it, I made the following app.

    Can you assess how well I've got the right application of the MVP pathter and the business logic from form?

    View

    using System.Linq;
    

    namespace EFCodeFirstMVP
    {
    interface IView
    {
    void SetData(IQueryable<Goods> items);
    }
    }

    using System;
    using System.Linq;
    using System.Windows.Forms;

    namespace EFCodeFirstMVP
    {
    public partial class Form1 : Form, IView
    {
    private readonly GoodsPresenter presenter;

        public Form1()
        {
            presenter = new GoodsPresenter(this, new GoodsModel());
            InitializeComponent();
        }
    
        public void SetData(IQueryable&lt;Goods&gt; items)
        {
            dataGridView1.DataSource = items.ToList();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            presenter.LoadData();
        }
    }
    

    }

    Presenter

    namespace EFCodeFirstMVP
    {
    class GoodsPresenter
    {
    private readonly IView view;
    private readonly IModel model;

        public GoodsPresenter(IView view, IModel model)
        {
            this.view = view;
            this.model = model;
        }
    
        public void LoadData()
        {
            var data = model.LoadData();
            view.SetData(data);
        }
    }
    

    }

    Model

    using System.Linq;

    namespace EFCodeFirstMVP
    {
    interface IModel
    {
    IQueryable<Goods> LoadData();
    }
    }

    using System.Linq;

    namespace EFCodeFirstMVP
    {
    class GoodsModel : IModel
    {
    public IQueryable<Goods> LoadData()
    {
    Context context = new Context();

            var items = from Items in context.Goods
                        select Items;
    
            return items;
        }
    }
    

    }

    Data

    using System.Data.Entity;

    namespace EFCodeFirstMVP
    {
    class Context: DbContext
    {
    public DbSet<Goods> Goods { get; set; }
    public DbSet<GoodsList> GoodsList { get; set; }

        public Context()
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists&lt;Context&gt;());
        }
    }
    

    public class Goods
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Barcode { get; set; }
    public int Price{ get; set; }
    }
    }



  • Okay, let's try to talk about this outside of WinForms.

    Look. You have two different things: the internal behavior of the program, and the way it shows it to the user. Imagine if you write radar software. Your program inside has a list of tracked planes. You take information from the sensors, count it, decide whether a new plane has arisen, or a false purpose, or a known plane has moved. It's all going on inside, and for that, it's not that necessary to interact with the user. This is the internal part of the program, model

    Now, you need to get this information to the operator. How will you be submission The information, in the form of a printout of a green text on a black background, or in the form of a three-dimensional holographic visualization, is not so important, and the model essentially does not depend on that part.

    So you have to write a model so the model doesn't know anything about the performance. It's not strictly necessary, but it allows the programme to be divided into independent parts and makes it easy to work with them.

    There are still open questions about how to pass the model user's actions, but this is a separate topic.

    Let's see a more grounded example: working with the database.

    Also, you have a model, a database, and an operation you're going to do. It's all set up in the module, probably hanging from the top of synchronization and asynchronicity, which can be considered to be over.

    The show should be simple.

    • show the user part of the model
    • accept the team user and deliver their models.
    • After updating the model, provide updated information

    Usually, the intermediate level is a business logic, a controller, a view of a model that can only be presented.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2