WinFormログオンのベストプラクティス

Aug 17 2020

WinFormsプロジェクトのログオンが機能しています。このprogram.csファイルを使用してログインフォームを起動します。これを実装するためのより良い方法がないかどうかはわかりません。

これが私のprogram.csファイルです:

    using System;
    using System.Windows.Forms;
    using WindowsFormsApp.Presenters;
    using WindowsFormsApp.Views;
    using Autofac;
    using DbContexts;
    using Serilog;

    namespace WindowsFormsApp
    {
        internal static class Program
        {
            public static IContainer Container { get; private set; }
            public static string UserName { get; set; }

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            private static void Main()
            {
                var builder = new ContainerBuilder();

                builder.Register(c => new MyContext());
            
                Container = builder.Build();

                Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .WriteTo.Console()
                    .WriteTo.RollingFile("log-{Date}.txt")
                    .CreateLogger();
                Log.Information("Application Started");

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                var loginForm = new LoginForm();
                var results = loginForm.ShowDialog();

                if (results == DialogResult.Cancel)
                    System.Environment.Exit(1);

                while (results != DialogResult.OK)
                {
                    results = loginForm.ShowDialog();
                    if (results == DialogResult.Cancel)
                        System.Environment.Exit(1);
                }

                var mainFormView = new MainFormView();

                mainFormView.Tag = new MainFormPresenter(mainFormView);

                Application.Run(mainFormView);
            }
        }
    }

提案やコメントは大歓迎です。

回答

4 MartinVerjans Aug 17 2020 at 16:18
  1. コード内のどこにいても、誰でも設定できるパブリック静的プロパティUserNameがあります。このプロパティの使用法については詳しく説明していません。
  • 更新する必要があるのがログオンフォームであり、それが二度と更新されない場合は、プログラムクラスにこのフィールドを設定してもらいます。

  • このフィールドを更新するのが別のクラスである場合、このプロパティはここに属していない可能性があります。

  1. 重複しているコードがあるので、もっとうまくやる方法がなければなりません。

  2. 私の見解では、Mainメソッドでさまざまなことを行っています。おそらく、これらの操作をInit、Login、Startの別々のメソッドに入れることができます。

  3. を使用して使用

  4. MainFormのPresenterをTagプロパティに配置します。これは、特定のプロパティに属していると思います。このようにして、MainFormは、誰がアクセスできるかを制御できます。そうでない場合、MainFormにアクセスできるオブジェクトは、タグ、つまりプレゼンターを回復できます。もう1つの問題は、Tagプロパティがオブジェクトであるため、使用するたびに変換する必要があることです。

だから、すべて一緒にそれはなります:

using System;
using System.Windows.Forms;
using WindowsFormsApp.Presenters;
using WindowsFormsApp.Views;
using Autofac;
using DbContexts;
using Serilog;

namespace WindowsFormsApp
{
    internal static class Program
    {
        public static IContainer Container { get; private set; }
        public static string UserName { get; private set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Init();
            Login();
            Start();
        }

        private void Init()
        {
            var builder = new ContainerBuilder();
            builder.Register(c => new MyContext());            
            Container = builder.Build();

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.RollingFile("log-{Date}.txt")
                .CreateLogger();
            Log.Information("Application Started");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        }

        private void Login()
        {
            using (loginForm = new LoginForm())
            {
                var results DialogResult.None;
                do
                {
                    results = loginForm.ShowDialog();
                    if (results == DialogResult.Cancel)
                        System.Environment.Exit(1);
                } while (results != DialogResult.OK);
                //Since we logged on correctly, we can update UserName (I guess)
                UserName = loginForm.ValidatedUserName;
            }
        }
 
        private void Start()
        {
            using (var mainFormView = new MainFormView())
            {
                mainFormView.Presenter = new MainFormPresenter(mainFormView);
                Application.Run(mainFormView);
            }
        }
    }
}
2 tinstaafl Aug 17 2020 at 06:45

私が最初に気づいたこと。コードの重複。あなたがこれを見るときはいつでも、あなたの最初の考えは「より良い方法がなければならない」であるべきです。

この場合、結果変数を割り当ててDialogResult.None、ループ内にダイアログを表示します。

var result = DialogResult.None;
while(result != DialogResult.OK)
{
    result = loginForm.ShowDialog();
    if(result == DialogResult.Cancel)
    {
        System.Environment.Exit(1);
    }
}