什么从DAL BLL到返回 [英] What to return from the DAL to BLL

查看:167
本文介绍了什么从DAL BLL到返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有它由一个应用程序:
用户界面(网页)
BLL(经理&安培;域对象)
DAL(数据访问类我的每个领域对象)。

I currently have an application which consists of: User Interface (web page) BLL (Manager & Domain Objects) DAL (DataAccess class for each of my Domain Objects).

我用下面的UI来搜索域对象。

I use the following in the UI to search for a domain object.

protect sub Button1_Click()
{
    IBook book = BookManager.GetBook(txtID.Text);
}

下面是我的BLL

public class BookManager 
{
    public static IBook GetBook(string bookId)
    {
        return BookDB.GetBook(bookId);
    }
}

public class Book : IBook
{
    private int? _id
    private string _name;
    private string _genre;

    public string Name
    {
        get { return _name; }
        private set 
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Invalid Name");
            _name = value;
        }
    }

    public string Genre
    {
        get { return _serial; }
        private set 
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Invalid Genre");
            _genre = value;
        }
    }

    // Other IBook Implementations

}

最后,这里是我DAL

And finally here is my DAL

public class BookDB
{
    public static IBook GetBook(int id)
    {
        // Get Book from database using sproc (not allowed to use any ORM)
        // ?? Create IBook Item?
        // return IBook
    }

一个人怎么会创建一个对象的iBook,并将其返回到管理器?
我想从BookDB返回一个DataTable到BookManager的并让它创建Book对象并返回它,但这似乎并不正确。
是否有另一种方式来做到这一点?

How would one create a IBook Object and return it to the Manager? I'm thinking of returning a DataTable from BookDB to BookManager and having it create the Book Object and return it, but that doesn't seem right. Is there another way to do this?

修改:
我决定每一层单独成一个项目,并尝试添加到BLL时的参考跑进在DAL层的循环依赖问题。
我无法从DAL访问图书类或接口或任何在BLL。
如果我只是在这里使用ado.net对象,并有我的经理创建从ado.net对象的实际对象?
下面是它奠定退出

BLL.Managers - BookManager
BLL.Interfaces IBook
BLL.Domain - Book
DAL - BookDB.

谢谢!

推荐答案

您可以创建一个只包含数据的虚拟书的对象。获取,设置属性和成员值。这本书,有1个属性在数据库中的每个字段,但不验证任何

You could create dummy Book objects that contain only data. Get, set properties and member values. This book, has 1 property for each field in the database, but doesn't validate anything.

您填写从数据库对象,然后将其发送到BLL。

You fill the object from the db, then send it to the BLL.

当你要保存的对象,也将其发送到BLL。

When you want to save the object, you also send it to the BLL.

您在BLL类可以换的和身边的物体,如果是有道理的。这样一来,很容易只发送回DAL。

Your classes in the BLL could wrap aroud those objects, if that makes sense. This way, it is easy to just send it back to the DAL.

虚拟图书:

public class DummyBook:IBook 
{
    private nullable<int> _id;
    private string _name;
    private string _genre;

    public string Id
    {
        get {return _id;}
        set {_id = value;}
    }

    public string Name 
    {
        get {return _name;}
        set {_name = value;}
    }

    public string Genre 
    {
        get {return _genre;}
        set {_genre= value;}
    }

}

DAL图书:

public class DALBook 
{
    public static IBook:GetBook(int id) 
    {
        DataTable dt;
        DummyBook db = new DummyBook();

        // Code to get datatable from database
        // ...
        // 

        db.Id = (int)dt.Rows[0]["id"];
        db.Name = (string)dt.Rows[0]["name"];
        db.Genre = (string)dt.Rows[0]["genre"];

        return db;
    }

    public static void SaveBook(IBook book) 
    {
        // Code to save the book in the database
        // you can use the properties from the dummy book
        // to send parameters to your stored proc.
    }
}

BLL图书:

public class Book : IBook
{
     private DummyBook _book;

     public Book(int id) 
     {
         _book = DALBook.GetBook(id);
     }

     public string Name 
     {
         get {return _book.Name;}
         set 
         {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("Invalid Name");
            }
            _book.Name = value;
         }
     }

     // Code for other Properties ...



     public void Save()
     {
         // Add validation if required
         DALBook.Save(_book);
     }

}

EDIT1:伪类应该在自己的项目(型号,就像在陈述意见是罚款)。引用将工作如下:

The dummy classes should go in their own project(Model, just as stated in the comments is fine). The references would work as follow:

在DAL参考的示范项目。

该BLL参考模型和DAL。

用户界面参考业务逻辑层。

The DAL References the Model Project.
The BLL References the Model and the DAL.
The UI References the BLL.

这篇关于什么从DAL BLL到返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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