前言
在上一篇中写完了Winform使用依赖注入,这一篇来个小实战,基于依赖注入实现一个简单仓储模式的架构,并使用SqlSugar ORM来访问数据库。
以下代码为了方便,忽略了接口类,若要在实际项目中使用的话,建议分别对Repository
和Service
加上接口规范约束、降低耦合、方便扩展。
开发环境:.NET Framework版本:4.8
开发工具:Visual Studio 2022
实现步骤
通过Nuget安装
System.Data.SQLite
、SqlSugar
以及AutoFac
创建数据库实体类,并使用SqlSugar特性标注字段
[Serializable]
[SugarTable("t_user")]
public class User
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
[SugarColumn(IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }
}
创建
BaseRepository
继承SqlSugar的SimpleClient
public class BaseRepository<T> : SimpleClient<T> where T : class, new()
{
public BaseRepository(ISqlSugarClient context = null) : base(context)
{
}
}
创建
UserRepository
继承BaseRepository
public class UserRepository:BaseRepository<User>
{
public UserRepository(ISqlSugarClient context) : base(context) { }
}
创建
UserService
继承UserRepository
public class UserService:UserRepository
{
public UserService(ISqlSugarClient context) :base(context)
{ }
}
在
Program
中使用AutoFac分别对SqlSugar、Service、Form进行依赖注入static void Register() { var builder = new ContainerBuilder(); //SqlSugar注入 builder.Register(options => { SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.Sqlite, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = "Data Source=" + Application.StartupPath + "\\data.db" }); sqlSugar.Aop.OnLogExecuted = (sql, pars) => { Console.WriteLine(sql); }; return sqlSugar; }).As<ISqlSugarClient>(); //服务注入 builder.RegisterType<UserService>(); //窗体注入 var dataAccess = Assembly.GetExecutingAssembly(); builder.RegisterAssemblyTypes(dataAccess) .Where(t => t.BaseType == typeof(Form) || t.BaseType == typeof(BaseForm)); Container = builder.Build(); }
在Form中分别实现增删改查
public partial class Form1 : BaseForm { readonly UserService _userService; readonly Form2 _form2; BindingList<User> bindList = new BindingList<User>(); public Form1(UserService userService, Form2 form2) { InitializeComponent(); _userService = userService; _form2 = form2; dataGridView1.DataSource = bindList; } private void btn_query_Click(object sender, EventArgs e) { var list = _userService.GetList(); bindList.SetData(list); } private void btn_add_Click(object sender, EventArgs e) { if (_form2.ShowDialog() == DialogResult.OK) { User user = new User { Name = _form2.UserName, Age = new Random().Next(1, 100) }; _userService.Insert(user); MessageBox.Show("添加成功"); btn_query_Click(null, null); } } private void btn_edit_Click(object sender, EventArgs e) { if (dataGridView1.CurrentRow == null) { MessageBox.Show("未选中任何行"); return; } int index = dataGridView1.CurrentRow.Index; _form2.UserName = dataGridView1[1, index].Value.ToString(); if (_form2.ShowDialog() == DialogResult.OK) { User user = bindList[index]; user.Name = _form2.UserName; _userService.Update(user); MessageBox.Show("修改成功"); btn_query_Click(null, null); } } private void btn_remove_Click(object sender, EventArgs e) { if (dataGridView1.CurrentRow == null) { MessageBox.Show("未选中任何行"); return; } int index = dataGridView1.CurrentRow.Index; _userService.Delete(bindList[index]); MessageBox.Show("删除成功"); btn_query_Click(null, null); } }
实现效果
☛☛☛点击此处下载源码☚☚☚