A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 满面春风 中级黑马   /  2014-5-12 08:04  /  853 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

3.创建业务逻辑层
数据访问层(DAL)将数据访问的细节从表示层中分离出来了,可它却不能处理任何的业务规则。比如判断数据的有效性。这些工作将由业务逻辑层(简称BLL)来承担,在以下应用程序中,将BLL实现为App_Code文件夹中的一系列的类。每一个BLL类都对应DAL中的一个TableAdapter,它们都从各自的TableAdapter中得到读取、插入、修改以及删除等方法以应用合适的业务规则。
第一步:创建BLL类
在App_Code文件夹中创建2个类文件。在解决方案浏览器(Solution Explorer)中右键点击App_Code文件夹,并选择新建项目(New Item),然后在弹出的对话框中选择“类”模板(Class template)就可以创建新的类文件了。将这2个文件分别命名为UserBLL以及JobBLL。

第二步:通过BLL类访问类型化数据集
为UserBLL和JobBLL类分别添加如下方法:
(1) UserBLL.css
           updateUser(string UserName, String PersonIDNumber, int SchoolID, string Password, string Sex, string TrueName, bool InPosition)
           updateUser(String PersonIDNumber, string TrueName, string BirthDate, string Nation, string NativePlace, string Polity, string JoinPolityTime, string Telephone, string MobiePhone, string Email)
           getPersons(int SchoolID,string TrueName)
           getPersonByPID(string PersonIDNumber)
           deleteUser(string UserName, String PersonIDNumber, int? SchoolID)
           addUser(string UserName, String PersonIDNumber, int SchoolID, string Password, string Sex, string TrueName, bool InPosition)
(2)JobBLL.css
           getPersonJob(string PersonIDNumber)
           updateUser(String PersonIDNumber, string Post1, string Post2, string JoinTime, int? CountryWorkedTime, string MasteSubject, string SecondSubject, string SchoolPhase, int? MotherClassTime)
以下为JobBLL.css的代码(UserBLL.css的代码太长,不列出)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TeacherTableAdapters;

/// <summary>
/// JobBLL 的摘要说明
/// </summary>

[System.ComponentModel.DataObject]
public class JobBLL
{
    private JobInfoTableAdapter _JobAdapter = null;

        public JobBLL()
        {
                //
                // TODO: 在此处添加构造函数逻辑
                //
        }

    protected JobInfoTableAdapter Adapter
    {
        get
        {
            if (_JobAdapter == null)
                _JobAdapter = new JobInfoTableAdapter();

            return _JobAdapter;
        }
    }

    //select
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
    public Teacher.JobInfoDataTable getPersonJob(string PersonIDNumber)
    {
        return Adapter.GetPersonJobByPID(PersonIDNumber);
    }

    //update
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool updateUser(String PersonIDNumber, string Post1, string Post2, string JoinTime, int? CountryWorkedTime, string MasteSubject, string SecondSubject, string SchoolPhase, int? MotherClassTime)
    {
        if (string.IsNullOrEmpty(PersonIDNumber))
        {
            return false;
        }
        
        try
        {
        
            Teacher.JobInfoDataTable PersonJob = Adapter.GetPersonJobByPID(PersonIDNumber);
            Teacher.JobInfoRow PersonJobPID;

            if (PersonJob.Count == 1)
            {
                PersonJobPID = PersonJob[0];
            }            
            else
            {
                return false;
            }

            if (!string.IsNullOrEmpty(Post1))
            {
                PersonJobPID.Post1 = Post1;
            }
            else
            {
                PersonJobPID.SetPost1Null();
            }
            if (!string.IsNullOrEmpty(Post2))
            {
                PersonJobPID.Post2 = Post2;
            }
            else
            {
                PersonJobPID.SetPost2Null();
            }
            if (!string.IsNullOrEmpty(MasteSubject))
            {
                PersonJobPID.MasteSubject = MasteSubject;
            }
            else
            {
                PersonJobPID.SetMasteSubjectNull();
            }
            if (!string.IsNullOrEmpty(SecondSubject))
            {
                PersonJobPID.SecondSubject = SecondSubject;
            }
            else
            {
                PersonJobPID.SetSecondSubjectNull();
            }            
            if (!string.IsNullOrEmpty(SchoolPhase))
            {
                PersonJobPID.SchoolPhase = SchoolPhase;
            }
            else
            {
                PersonJobPID.SetSchoolPhaseNull();
            }

            if (!(CountryWorkedTime == null))
            {
                PersonJobPID.CountryWorkedTime = CountryWorkedTime.Value;
            }
            else
            {
                PersonJobPID.SetCountryWorkedTimeNull();
            }
            if (!(MotherClassTime == null))
            {
                PersonJobPID.MotherClassTime = MotherClassTime.Value;
            }
            else
            {
                PersonJobPID.SetMotherClassTimeNull();
            }
            if (!string.IsNullOrEmpty(JoinTime))
            {
                PersonJobPID.JoinTime = DateTime.Parse(JoinTime);
            }
            else
            {
                PersonJobPID.SetJoinTimeNull();
            }

            int rowAffect1 = Adapter.Update(PersonJobPID);
            return (rowAffect1 == 1);
        }
        catch (System.Configuration.Provider.ProviderException e)
        {
            return false;
        }
    }

说明:
(1)using TeacherTableAdapters; 引用DAL层命名空间,自动生成,必须。否则无法使用类。
(2)JobInfoTableAdapter类,对应DAL中的TableAdapter适配器JobInfo,通过这个类来调用增、删及改等数据操作。
(3)使用JobInfoDataTable PersonJob来装载查询返回的数据,是DataSet中的强类型数据表,结构和数据类型由数据库定义。使用JobInfoRow PersonJobPID来载入表中的某一行。行字段访问用如下形式:Row变量.字段名(如PersonJobPID.Post1)。
Teacher.JobInfoDataTable PersonJob = Adapter.GetPersonJobByPID(PersonIDNumber);
Teacher.JobInfoRow PersonJobPID = PersonJob[0];
(4)数据库中某些表字段在设计时可能被允许空值(null),强类型DataTable中对字段的判空与赋空值不能采用以下形式:
           !string.IsNullOrEmpty(PersonJobPID.Post1)
           PersonJobPID.Post1 == null;
           PersonJobPID.Post1 = null;
正确的做法是采用行类型变量(Teacher.JobInfoRow  PersonJobPID)为每个字段生成的“空值方法”操作:
           void PersonJobPID.SetPost1Null();
           bool PersonJobPID.IsPost1Null()
(5)JobInfoDataTable或JobInfoRow中数据存储在内存中,修改后的数据要反映到数据库中采用适配器方法Update().
int rowAffect1 = Adapter.Update(PersonJobPID);
至此业务逻辑层构建完成。

2 个回复

倒序浏览
架构这样的知识是在我们进入黑马之后才能学到吗
回复 使用道具 举报
顶一个!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马