我在WinForms应用程序中实现了Repository模式:
的UnitOfWork:
using RCCGSPP.Core;
using RCCGSPP.Persistence.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace RCCGSPP.Persistence
{
//Implements the Logic for Methods in the IUnitOfWork Interface
public class UnitOfWork
{
//Our App contextClassName
private readonly SPPContext _context;
private DbContext dbContext;
//Recieves our App ContextClassName
public UnitOfWork(SPPContext context)
{
//stores our App ContextName in _context
_context = context;
//Then uses the context to initialise both Repositories
Persons = new PersonRepository(_context);
SundayServices = new SundayServiceRepository(_context);
UserPasses = new UserRepository(_context);
NewComers = new NewComerRepository(_context);
}
public UnitOfWork(DbContext dbContext)
{
this.dbContext = dbContext;
}
//properties
public PersonRepository Persons { get; private set; }
public SundayServiceRepository SundayServices { get; private set; }
public UserRepository UserPasses { get; private set; }
public NewComerRepository NewComers { get; private set; }
//Calls the SaveChanges on the Context
public int Complete()
{
return _context.SaveChanges();
}
//Implementation of the Dispose Method to Dispose the Context
public void Dispose()
{
_context.Dispose();
}
}
}
My Form我想使用我的UnitOfWork,我已将它声明为readonly属性,所以我将它包含在构造函数中以初始化它,但是因为表单是从另一个单击加载时我得到的&# 34; There is no argument given that corresponds to the required formal parameter 'unitOfWork'
"
表格使用UNITOFWORK的地方
public partial class Register : MaterialForm
{
private readonly IUnitOfWork _unitOfWork;
string userName;
string psswrd;
string Confirmpsswrd;
public Register(IUnitOfWork unitOfWork)
{
InitializeComponent();
//Set your preferred colors &theme (Material Skin)
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Blue400, Primary.Red900, Primary.Brown900, Accent.LightBlue200, TextShade.BLACK);
//prevent Form from Resizing
Sizable = false;
//UnitOfWork
_unitOfWork = unitOfWork;
}
private void Register_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e)
{
//Get the User email and Password to register in the DB
userName = textEmail.Text;
psswrd = textPassword.Text;
Confirmpsswrd = textConfirmPassword.Text;
//Compare the password
bool conRes = ComparePassword(psswrd, Confirmpsswrd);
if (conRes)
{
//Insert to db using the UnitOfWork
UserPass userToDb = new UserPass
{
UserName = this.userName,
password = this.psswrd,
};
_unitOfWork.UserPasses.Add(userToDb);
//Commit calling complete()
_unitOfWork.Complete();
//FeedBack Registered Sucessfull
LabelErrorPassword.Text = "Successful, Login!";
}
else
{
LabelErrorPassword.BackColor = Color.Red;
LabelErrorPassword.Text = "The Passwords don't match!"; //show in the Label that password are not the same
}
}
/**********Method to compare Password**********************/
public bool ComparePassword(string pss1, string pss2)
{
if (pss1.Equals(pss2))
{
return true;
}
else return false;
}
}
lOADING MY fORM:
private void BtnRegister_Click(object sender, EventArgs e)
{
//lOAD THE rEGISTER fORM,
Register nForm = new Register();
nForm.Show();
}
如何在WinForm应用程序中使用我的UnitOfWork。
答案 0 :(得分:1)
正如@stuartd所说,建议使用WinForms中Form的无参数构造函数。但是当然你可以添加带参数的构造函数。
public Register()
{
InitializeComponent();
//Set your preferred colors &theme (Material Skin)
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Blue400, Primary.Red900, Primary.Brown900, Accent.LightBlue200, TextShade.BLACK);
//prevent Form from Resizing
Sizable = false;
//UnitOfWork, not initialized here
//_unitOfWork = unitOfWork;
}
public Register(IUnitOfWork unitOfWork) : this() // call default constructor!
{
_unitOfWork = unitOfWork;
}
我强烈建议将所有自动生成和自定义初始化代码保留在默认(无参数)构造函数中。因此,WinForms设计师不会停止工作。
我们在多个项目中结合DI容器使用此策略,没有任何问题。
答案 1 :(得分:1)
谢谢大家。
我使用Simple Ijector包将UnitOfWork注入我的Program.cs中,如下所示:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Bootstrap();
//Get instance of my Registered HomeForm
Application.Run((container.GetInstance<Home>()));
}
private static void Bootstrap()
{
// Create the container as usual.
container = new Container();
// Register my types, for instance:
container.Register<UnitOfWork, UnitOfWork>();
//Register my HomeForm
container.Register<Home>();
// Optionally verify the container.
//container.Verify();
}
}
然后在我的主要表格中:我将注册的UnitOfWork注入其构造函数中:
//Declare my UnitOfwORK
private readonly UnitOfWork _unitOfWork;
public Home(UnitOfWork unitOfWork)
{
InitializeComponent();
this._unitOfWork = unitOfWork;
}
然后我将它传递给新的注册表单,该表单将在单击注册按钮时加载,在其中我将使用UnitOfWork持久化并提交到数据库
//lOAD THE rEGISTER fORM,
Register nForm = new Register(this._unitOfWork);
nForm.Show();
}
最后在Register中我使用UnitOfWork来执行我的CRUD操作并提交给DB:
public partial class Register : MaterialForm
{
//Declare my UnitOfwORK
private readonly UnitOfWork _unitOfWork;
string userName;
string psswrd;
string Confirmpsswrd;
//I Inject IUnitOfOfClass with the Help of Simple Injector dependency injection library
public Register(UnitOfWork unitOfWork)
{
InitializeComponent();
//Set your preferred colors &theme (Material Skin)
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Blue400, Primary.Red900, Primary.Brown900, Accent.LightBlue200, TextShade.BLACK);
//prevent Form from Resizing
Sizable = false;
_unitOfWork = unitOfWork;
}
private void SetUnitOfWork()
{
}
private void Register_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e)
{
//Get the User email and Password to register in the DB
userName = textEmail.Text;
psswrd = textPassword.Text;
Confirmpsswrd = textConfirmPassword.Text;
//Compare the password
bool conRes = ComparePassword(psswrd, Confirmpsswrd);
if (conRes)
{
//Insert to db using the UnitOfWork
UserPass userToDb = new UserPass
{
UserName = this.userName,
password = this.psswrd,
LastAcess = DateTime.Now
};
_unitOfWork.UserPasses.Add(userToDb);
//Commit calling complete()
_unitOfWork.Complete();
//FeedBack Registered Sucessfull
LabelErrorPassword.Text = "Successful, Login!";
}
else
{
LabelErrorPassword.BackColor = Color.Red;
LabelErrorPassword.Text = "The Passwords don't match!"; //show in the Label that password are not the same
}
}
/**********Method to compare Password**********************/
public bool ComparePassword(string pss1, string pss2)
{
if (pss1.Equals(pss2))
{
return true;
}
else return false;
}
}
它可能不是实现它的最佳方式,但它工作得很好,我测试了它,我可以在数据库中看到数据。