从 DAL 返回到 BLL 的内容 [英] What to return from the DAL to BLL

查看:25
本文介绍了从 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

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 对象并将其返回给管理器?我正在考虑将一个 DataTable 从 BookDB 返回到 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 访问 Book Class 或 Interface 或 BLL 中的任何内容.我应该在这里只使用 ado.net 对象并让我的经理从 ado.net 对象创建实际对象吗?这是它的布局

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

谢谢!

推荐答案

您可以创建仅包含数据的虚拟 Book 对象.获取、设置属性和成员值.这本书对数据库中的每个字段都有 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.
UI 引用 BLL.

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

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

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