作者:地沟油
1.在ivw.Unity新增AutofacContainer类。
AutofacContainer代码:
1 using Autofac; 2 using Autofac.Builder; 3 using System; 4 using System.Threading; 5 6 namespace ivw.Unity 7 { 8 public class AutofacContainer 9 { 10 private Autofac.IContainer container; 11 #region 使用单例模式对对象实例化 12 private static AutofacContainer aufacContainer = null; 13 14 /// <summary> 15 /// 16 /// </summary> 17 /// <returns></returns> 18 public static AutofacContainer GetInstance() 19 { 20 if (aufacContainer == null) 21 { 22 //使用原子操作方式实现单例模式,解决了多线程问题与双重判断加锁的代码多问题 23 Interlocked.CompareExchange<AutofacContainer>(ref aufacContainer, new AutofacContainer(), null); 24 } 25 return aufacContainer; 26 } 27 28 /// <summary> 29 /// 30 /// </summary> 31 private AutofacContainer() => container = BuildAutofacContainer(); 32 #endregion 33 34 private Autofac.IContainer BuildAutofacContainer() 35 { 36 try 37 { 38 var builder = new ContainerBuilder(); 39 RegisterTypes(builder); 40 var container = builder.Build(ContainerBuildOptions.None); 41 return container; 42 } 43 catch (Exception ex) 44 { 45 throw ex; 46 } 47 } 48 49 private static void RegisterTypes(ContainerBuilder builder) 50 { 51 #region 注册User 52 53 #endregion 54 } 55 56 #region 兼容EF和ADO.NET 57 public T GetObject<T>() => container.Resolve<T>(); 58 59 public T GetObject<T>(string name) => container.ResolveNamed<T>(name); 60 #endregion 61 } 62 }
ServiceHelper代码:
1 using ivw.Services; 2 using ivw.IDao; 3 namespace ivw.Unity 4 { 5 public static class ServiceHelper 6 { 7 #region 公共属性 8 9 #endregion 10 11 #region 设置Service值 12 public static void SetService() 13 { 14 15 } 16 #endregion 17 } 18 }
2.这样,框架算是完成了。接下来,我们试试怎么用。
3.在ivw.Models新增一个空的实体数据模型。
4.在数据库新建表Users。
1 USE [IVW] 2 GO 3 4 /****** Object: Table [dbo].[Users] Script Date: 2017/11/24 9:20:20 ******/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 CREATE TABLE [dbo].[Users]( 12 [Id] [int] IDENTITY(1,1) NOT NULL, 13 [UserCode] [nvarchar](10) NOT NULL, 14 [UserName] [nvarchar](20) NOT NULL, 15 [Byname] [nvarchar](20) NULL, 16 [Password] [nvarchar](128) NULL, 17 [PasswordSalt] [nvarchar](128) NULL, 18 [PasswordFormat] [int] NULL, 19 [State] [int] NOT NULL, 20 [Email] [nvarchar](50) NULL, 21 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 22 ( 23 [Id] ASC 24 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 25 ) ON [PRIMARY] 26 GO 27 28 ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_State] DEFAULT ((0)) FOR [State] 29 GO 30 31 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'Id' 32 GO 33 34 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户代码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'UserCode' 35 GO 36 37 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'UserName' 38 GO 39 40 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户别名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'Byname' 41 GO 42 43 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'加密的密码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'Password' 44 GO 45 46 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'密码散列' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'PasswordSalt' 47 GO 48 49 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'密码格式' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'PasswordFormat' 50 GO 51 52 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户状态' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'State' 53 GO 54 55 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Email' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'Email' 56 GO 57 58 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户信息' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users' 59 GO
5.Users插入一条数据。
1 INSERT [dbo].[Users] ([Id], [UserCode], [UserName], [Byname], [Password], [PasswordSalt], [PasswordFormat], [State], [Email]) VALUES (1, N'Admin', N'管理员', NULL, N'000', NULL, NULL, 1, NULL)
6.从数据库更新模型,把刚刚新建的Users表更新上去。
7.在ivw.Dao中新建一个文件夹SqlServer,文件夹内新增UsersDao类,继承接口IUsersDao。
在ivw.IDao中新增一个IUsersDao接口。
在ivw.Services新增一个UserServices类。
UsersDao代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using ivw.IDao; 8 using ivw.Models; 9 10 namespace ivw.Dao.SqlServer 11 { 12 public class UserDao : IUserDao 13 { 14 /// <summary> 15 /// 登陆 16 /// </summary> 17 /// <param name="UserCode">用户代码</param> 18 /// <param name="Password">用户密码</param> 19 /// <returns></returns> 20 public List<Users> Login(String UserCode, String Password) 21 { 22 using (DBContainer db = new DBContainer()) 23 { 24 var cc = db.Users.Where(a => a.UserCode.ToLower() == UserCode.ToLower() && a.Password == Password); 25 return cc.ToList(); 26 } 27 } 28 } 29 } 30
IUserDao代码:
1 using ivw.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ivw.IDao 9 { 10 public interface IUserDao 11 { 12 /// <summary> 13 /// 登陆 14 /// </summary> 15 /// <param name="UserCode">用户代码</param> 16 /// <param name="Password">用户密码</param> 17 /// <returns></returns> 18 List<Users> Login(String UserCode, String Password); 19 } 20 }
UserServices代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using ivw.IDao; 8 using ivw.Models; 9 10 namespace ivw.Services 11 { 12 public class UserServices 13 { 14 private readonly IUserDao userDao; 15 public UserServices(IUserDao userDao) 16 { 17 this.userDao = userDao; 18 } 19 public Users Login(String UserCode, String Password) 20 { 21 var cc = from a in userDao.Login(UserCode, Password) 22 where a.State==1 23 select a; 24 return cc.FirstOrDefault(); 25 } 26 } 27 }
8.Autofac注入。
(1)在AutofacContainer.RegisterTypes中写入。
1 private static void RegisterTypes(ContainerBuilder builder) 2 { 3 #region 注册User 4 builder.RegisterType<Dao.SqlServer.UserDao>().As<IDao.IUserDao>(); 5 #endregion 6 }
(2)在ServiceHelper中写入。
1 using ivw.Services; 2 using ivw.IDao; 3 namespace ivw.Unity 4 { 5 public static class ServiceHelper 6 { 7 #region 公共属性 8 public static UserServices UserServices { get; set; } 9 #endregion 10 11 static ServiceHelper() => SetService(); 12 13 #region 设置Service值 14 public static void SetService() 15 { 16 UserServices = new UserServices(AutofacContainer.GetInstance().GetObject<IUserDao>()); 17 } 18 #endregion 19 } 20 } 21
8.在frmLogin中新增两个TextEdit、LabelControl,两个按钮,分别是登录、取消,一个PanelControl.
9.frmLogin后台代码。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Linq; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using DevExpress.XtraEditors; 11 using ivw.Unity; 12 using ivw.Models; 13 14 namespace ivw.Windows 15 { 16 public partial class frmLogin : DevExpress.XtraEditors.XtraForm 17 { 18 public frmLogin() 19 { 20 InitializeComponent(); 21 this.KeyPreview = true; 22 this.sbtnLogin.Click += SbtnLogin_Click; 23 this.sbtnCancel.Click += SbtnCancel_Click; 24 this.KeyDown += FrmLogin_KeyDown; 25 } 26 27 private void SbtnLogin_Click(object sender, EventArgs e) 28 { 29 try 30 { 31 Users sRet = ServiceHelper.UserServices.Login(txteUserCode.Text, txtePassword.Text); 32 if (sRet != null) 33 { 34 this.DialogResult = DialogResult.OK; 35 } 36 37 } 38 catch (Exception ex) 39 { 40 41 } 42 } 43 private void SbtnCancel_Click(object sender, EventArgs e) => System.Diagnostics.Process.GetCurrentProcess().Kill(); 44 45 #region 快捷键 46 private void FrmLogin_KeyDown(object sender, KeyEventArgs e) 47 { 48 if (e.KeyCode == Keys.Enter) 49 { 50 if (txteUserCode.ContainsFocus) 51 { 52 txtePassword.Focus(); 53 } 54 else if (txtePassword.ContainsFocus) 55 { 56 SbtnLogin_Click(null, null); 57 } 58 } 59 } 60 #endregion 61 62 } 63 }
10.新增一个frmMain窗体,在Program写入以下代码。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace ivw.Windows 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 //启用皮肤 18 DevExpress.Skins.SkinManager.EnableFormSkins(); 19 DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged(); 20 DevExpress.Skins.SkinManager.EnableMdiFormSkins(); 21 22 Application.EnableVisualStyles(); 23 Application.SetCompatibleTextRenderingDefault(false); 24 frmLogin frmLogin = new frmLogin(); 25 frmLogin.Activate(); 26 frmLogin.ShowDialog(); 27 if (frmLogin.DialogResult == DialogResult.OK) 28 { 29 Application.Run(new frmMain()); 30 } 31 } 32 } 33 }
11.调试。输入帐号密码登录。
12.成功登录,进入frmMain界面。