七层登录
2016-02-13 14:33
330 查看
敲七层登录经历了漫长岁月,敲七层之前先用三层敲了几条线。敲七层的时候就是在三层的基础上加上两个设计模式,其中一个是抽象工厂+反射+配置文件,另一个就是加上了一个外观模式。最先添加的是抽象工厂,因为知道它的作用就为了可以灵活的换数据库。在加抽象工厂的时候我们不妨看一遍设计模式这样我们会对抽象工厂有更深的理解,才能知道下面BLL--Factory--IDAL中的UML图中的连线为什么要这样画。
在敲七层的时候令人头痛的就是反射+配置文件了,出了很多错误,不过不要忘记百度哟(在出了问题后居然忘了百度,百度后才发现好多人写了类似博客)。
七层登录这是自己写的一个例子,虽然可以进入主窗体了但是还有很多需要完善的地方。U层D层B层代码和三层是一样的,只不过加了两个设计模式。
U层
外观层
想了很长时间也不知道外观层在这里有什么作用,仅仅是传递一个参数?
B层
在这里值得注意的是 return(接口)Assembly.Load("D层程序集").CreateInstance(“D层命名空间.类名”)
接口
D层代码和之前简单三层登录中的代码是相同的在这里就不写了,万事开头难,的确是这样,刚开始的对七层毫无头绪,但是我们认真研究一番,就能发现其实很简单的,会感觉越来越有趣呢。
在敲七层的时候令人头痛的就是反射+配置文件了,出了很多错误,不过不要忘记百度哟(在出了问题后居然忘了百度,百度后才发现好多人写了类似博客)。
七层登录这是自己写的一个例子,虽然可以进入主窗体了但是还有很多需要完善的地方。U层D层B层代码和三层是一样的,只不过加了两个设计模式。
U层
<span style="font-size:18px;"> //获取文本框中的内容 string UserName = txtUserName.Text.Trim(); string Password = txtPassword.Text; //判断文本框是否为空 if (txtPassword.Text == string.Empty) { MessageBox.Show("信息不完整!", "登录"); } if (txtUserName.Text == string.Empty) { MessageBox.Show("用户名不能为空!", "登录"); } //判断卡号和是否正确 LoginFacade FLogin = new LoginFacade();//实例化外观层 EntityClass.LoginInfo user = FLogin.SelectUser(UserName,Password );//向下传送数据,以及放回实体型数据 if (user != null) { MessageBox.Show("登录成功"); this.Close(); this.DialogResult = System.Windows.Forms.DialogResult.OK; } else { MessageBox.Show("您输入的账号或密码有误"); }</span>
外观层
想了很长时间也不知道外观层在这里有什么作用,仅仅是传递一个参数?
<span style="font-size:18px;">public class LoginFacade { LoginBLL BLogin = new LoginBLL(); LoginInfo MLogin = new LoginInfo(); public LoginInfo SelectUser(string userName, string password) { LoginInfo user = new LoginInfo(); LoginBLL Blogin = new LoginBLL(); user = Blogin.SelectUser(userName, password); return user; } }</span>
B层
<span style="font-size:18px;">public class LoginBLL { public LoginInfo SelectUser(string userName, string password) { EntityClass.LoginInfo user=new EntityClass.LoginInfo ();//实例化实体 LoginFactory Datacess = new LoginFactory();//实例化工厂 IDAL.LoginIDAL ILogin;//接口不能实例化 ILogin = Datacess.SelectUser() ;//调用工厂的selectUser方法创建Iuser接口实例 user = ILogin.GetUser(userName, password); return user ; } }</span>工厂层
<span style="font-size:18px;">public class LoginFactory { //读配置文件 // public static readonly string sql = System.Configuration.ConfigurationManager.AppSettings["DB"]; public static readonly string strDB = System.Configuration.ConfigurationManager.AppSettings["DBString"]; //创建接口 public LoginIDAL SelectUser() { return (LoginIDAL)Assembly.Load("ChargeSystemDAL").CreateInstance("ChargeSystem.DAL.SqlLogin"); } }</span>
在这里值得注意的是 return(接口)Assembly.Load("D层程序集").CreateInstance(“D层命名空间.类名”)
接口
<span style="font-size:18px;">public interface LoginIDAL { // EntityClass.LoginInfo user = new EntityClass.LoginInfo(); //定义方法 EntityClass.LoginInfo GetUser(string userName, string password); }</span>
D层代码和之前简单三层登录中的代码是相同的在这里就不写了,万事开头难,的确是这样,刚开始的对七层毫无头绪,但是我们认真研究一番,就能发现其实很简单的,会感觉越来越有趣呢。
相关文章推荐
- Educational Codeforces Round 7 E. Ants in Leaves(DFS+贪心)
- C++计算几何库
- PDRIVER_FS_NOTIFICATION routine
- IOS面试题目
- 【信息资源管理之宏观了解】
- label标签跳出循环
- Java byte 数组转换 16 进制字符串
- 博客导航
- [转]Hibernate查询对象所有字段,单个字段 ,几个字段取值的问题
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
- 在UIView中裁剪需要位置生成图片
- Building Maintainable Software-java篇之Separate Concerns in Modules
- 项目技术总结二之SSO单点登录
- kinect 2.0开发笔记之一
- LA3667
- [小脚本] bash维护并行下载任务
- java面试第二天
- Android Studio如何使用logcat
- Android中的主题、样式和国际化
- 【面试论述】const的作用、使用举例与#define联系