Các phương pháp hay nhất về đăng nhập WinForm
Tôi có một đăng nhập làm việc cho một Dự án WinForms. Tôi sử dụng program.cstệp để khởi chạy biểu mẫu đăng nhập. Tôi không chắc không có cách nào tốt hơn để thực hiện điều này.
Đây là tệp program.cs của tôi:
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);
}
}
}
Mọi đề xuất hoặc bình luận đều được hoan nghênh.
Trả lời
- Bạn có một thuộc tính tĩnh công khai UserName có thể được đặt bởi bất kỳ ai, ở bất kỳ đâu trong mã của bạn. Bạn đã không nêu chi tiết việc sử dụng thuộc tính này.
Nếu đó là biểu mẫu đăng nhập cần cập nhật nó và nó sẽ không bao giờ cập nhật lại, hãy yêu cầu lớp Chương trình của bạn đặt trường này.
Nếu là một lớp khác cập nhật trường này, có thể thuộc tính này không thuộc về đây.
Bạn có một số mã bị trùng lặp, vì vậy phải có cách để làm tốt hơn.
Theo quan điểm của tôi, bạn đang thực hiện nhiều việc khác nhau trong phương pháp Chính của mình, có thể bạn có thể đặt các thao tác này trong các phương thức riêng biệt: Init, Login, Start.
Sử dụng bằng cách sử dụng
Bạn đặt Trình trình bày cho MainForm trong Thuộc tính thẻ, tôi nghĩ thuộc tính này thuộc về một thuộc tính cụ thể. Bằng cách đó MainForm có thể kiểm soát ai có quyền truy cập vào nó, nếu không, bất kỳ đối tượng nào có quyền truy cập vào MainForm đều có thể khôi phục Thẻ và do đó Người trình bày. Một vấn đề khác là thuộc tính Tag là một đối tượng, vì vậy bạn sẽ phải chuyển đổi nó mỗi khi bạn muốn sử dụng nó.
Vì vậy, tất cả cùng nhau sẽ trở thành:
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);
}
}
}
}
Điều đầu tiên tôi nhận thấy. Sao mã. Bất cứ khi nào bạn nhìn thấy điều này, suy nghĩ đầu tiên của bạn sẽ là "Phải có một cách tốt hơn.".
Trong trường hợp này, hãy gán biến kết quả cho DialogResult.Nonesau đó chỉ hiển thị hộp thoại bên trong vòng lặp:
var result = DialogResult.None;
while(result != DialogResult.OK)
{
result = loginForm.ShowDialog();
if(result == DialogResult.Cancel)
{
System.Environment.Exit(1);
}
}