在3层架构使用BAL的?如何调用方法从DAL到BAL [英] Use of BAL in 3 tier architecture?How to call methods from DAL to BAL

查看:523
本文介绍了在3层架构使用BAL的?如何调用方法从DAL到BAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,以3层架构及以下是我DAL code

I am a newbie to 3 tier architecture and below is my DAL code

public static int Insert(string firstname, string lastname, DateTime dob, string gender,string email, string password)
    {
        // bool flag = false;
        SqlParameter pid;
        SqlParameter result;

        SqlConnection con = Generic.DBConnection.OpenConnection();

        try
        {

            SqlCommand cmd1 = new SqlCommand("Insertreg", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@FirstName", firstname);
            cmd1.Parameters.AddWithValue("@LastName", lastname);
            cmd1.Parameters.AddWithValue("@Dob", dob);
            cmd1.Parameters.AddWithValue("@Gender", gender);
           cmd1.Parameters.AddWithValue("@EmailId", email);
            cmd1.Parameters.AddWithValue("@Password", password);
            result = cmd1.Parameters.Add("@result", System.Data.SqlDbType.Int);
            result.Direction = System.Data.ParameterDirection.Output;
            pid = cmd1.Parameters.Add("@id", System.Data.SqlDbType.Int);
            pid.Direction = System.Data.ParameterDirection.Output;
            return cmd1.ExecuteNonQuery();


            con.Close();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

这在BAL

 public int insert(string firstname,string lastname,DateTime dob,string gender,string email,string password)
    {
      ProfileMasterDAL dal=new ProfileMasterDAL();
      try
      {
          return ProfileMasterDAL.Insert(firstname, lastname, dob, gender,email, password);
      }
      catch (Exception ex)
      {

          throw ex;
      }
        finally
      {
          dal = null;
      }

    }

我在我的UI

  ProfileMasterBLL pmBLL = new ProfileMasterBLL();
 pmBLL.insert(firstname, lastname, dob, gender, mobile, country, state, email, password);

这是正确的方法为code在3层??我的意思是如何从DAL方法调用BAL进入UI?如果没有建议我一些好的way.Thanks。

Is this the correct way to code in 3 tier??I mean how to call methods from DAL to BAL and into UI?If not suggest me some good way.Thanks.

推荐答案

通常我做到以下几点:


  1. 定义一个业务层(BL,你怎么称呼它BAL)。这里包含业务实体的定义。它还定义了接口检索/保存/删除您使用任何模式的数据(资料库,内容等)。

  2. 定义一个数据访问层(DAL)。这包含检索/保存/删除接口的实际执行情况。

  3. 定义UI层。这包含用户界面元素(窗体,控件,模型,控制器等),可使用BL加载数据。

该引用如下:


  1. 的BL不知道DAL或UI。

  2. 的DAL知道BL。该DAL不知道UI。

  3. 的UI知道BL。用户界面不知道DAL。

大问题你可能是,如何在BL检索/保存/删除数据时,它不知道DAL,因此在DAL不能创建类的实例。那么,这就是一个小<一href=\"https://www.google.nl/search?q=dependency%20injection&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8\"相对=nofollow> 就派上用场了依赖注入。你所要连接的是DAL级的BL-接口的注入。

The big question for you probably is, how does the BL retrieve/save/delete data when it doesn't know the DAL, and therefore cannot create an instance of a class in the DAL. Well, this is where a little Dependency Injection comes in handy. All you have to wire up is the injection of the DAL-class to the BL-interface.

希望这是有道理的。我用它作为我的标准的3层实现,它的工作绝对没有问题。具体来说,我用<一个href=\"https://www.google.nl/search?q=entity%20framework%20poco&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8\"相对=nofollow>实体框架与POCO 获取的实体,我用的是DI是一个自定义的,但<一href=\"http://stackoverflow.com/questions/21288/which-net-dependency-injection-frameworks-are-worth-looking-into\">any对那些在那里的就行了。

Hope this makes sense. I use it as my standard 3-tier implementation, and it works absolutely without problems. Specifically, I use Entity Framework with POCO for entities, and the DI I use is a custom one, but any of the ones out there will do.

该BL不知道DAL。


  • 的BL定义了一个接口(可以称之为IRepository),它可以用做它需要做的。

  • 的DAL定义一个类(库),它实现了接口IRepository。所以库的实际执行是在DAL。

  • 显然BL不能直接创建存储库的一个实例。这就是依赖注入进来,这允许开发人员创建它通常不能做一个类的实例。这方面的一个简单的版本原油,是使用<一个href=\"https://www.google.nl/search?q=c#%20reflection&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8\"相对=nofollow>反射。

  • The BL defines an interface (lets call it IRepository) which it can use to do what it needs to do.
  • The DAL defines a class (Repository) which implements the interface IRepository. So the actual implementation of the repository is in the DAL.
  • Obviously the BL cannot create an instance of the repository directly. This is where dependency injection comes in, this allows the developer to create an instance of a class where it normally cannot be done. A simple crude version of this, is to use reflection.

我希望这会更有意义。

这篇关于在3层架构使用BAL的?如何调用方法从DAL到BAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆