asp.net – 从DAL返回BLL

前端之家收集整理的这篇文章主要介绍了asp.net – 从DAL返回BLL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前有一个申请,其中包括
用户界面(网页)
BLL(经理&域对象)
DAL(我的每个域对象的DataAccess类).

我在UI中使用以下内容搜索域对象.

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

这是我的BLL

  1. public class BookManager
  2. {
  3. public static IBook GetBook(string bookId)
  4. {
  5. return BookDB.GetBook(bookId);
  6. }
  7. }
  8.  
  9. public class Book : IBook
  10. {
  11. private int? _id
  12. private string _name;
  13. private string _genre;
  14.  
  15. public string Name
  16. {
  17. get { return _name; }
  18. private set
  19. {
  20. if (string.IsNullOrEmpty(value))
  21. throw new Exception("Invalid Name");
  22. _name = value;
  23. }
  24. }
  25.  
  26. public string Genre
  27. {
  28. get { return _serial; }
  29. private set
  30. {
  31. if (string.IsNullOrEmpty(value))
  32. throw new Exception("Invalid Genre");
  33. _genre = value;
  34. }
  35. }
  36.  
  37. // Other IBook Implementations
  38.  
  39. }

最后这是我的DAL

  1. public class BookDB
  2. {
  3. public static IBook GetBook(int id)
  4. {
  5. // Get Book from database using sproc (not allowed to use any ORM)
  6. // ?? Create IBook Item?
  7. // return IBook
  8. }

如何创建一个IBook对象并将其返回给Manager?
我正在考虑将DataTable从BookDB返回到BookManager,并创建Book对象并返回它,但这似乎不正确.
有另一种方法吗?

编辑:
我决定在尝试添加对BLL的引用时,将每个层分隔为一个项目,并在DAL层中遇到循环依赖问题.
我无法从DAL访问书类或界面或BLL中的任何内容.
我应该在这里使用ado.net对象,让我的经理从ado.net对象创建实际的对象吗?
这是它的铺设

  1. BLL.Managers - BookManager
  2. BLL.Interfaces IBook
  3. BLL.Domain - Book
  4. DAL - BookDB.

谢谢!

解决方法

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

您从数据库填充对象,然后发送到BLL.

当您要保存对象时,您也将其发送到BLL.

您的BLL中的类可以包裹这些对象,如果这是有道理的.这样,很容易将其发回DAL.

虚拟书:

  1. public class DummyBook:IBook
  2. {
  3. private nullable<int> _id;
  4. private string _name;
  5. private string _genre;
  6.  
  7. public string Id
  8. {
  9. get {return _id;}
  10. set {_id = value;}
  11. }
  12.  
  13. public string Name
  14. {
  15. get {return _name;}
  16. set {_name = value;}
  17. }
  18.  
  19. public string Genre
  20. {
  21. get {return _genre;}
  22. set {_genre= value;}
  23. }
  24.  
  25. }

DAL书:

  1. public class DALBook
  2. {
  3. public static IBook:GetBook(int id)
  4. {
  5. DataTable dt;
  6. DummyBook db = new DummyBook();
  7.  
  8. // Code to get datatable from database
  9. // ...
  10. //
  11.  
  12. db.Id = (int)dt.Rows[0]["id"];
  13. db.Name = (string)dt.Rows[0]["name"];
  14. db.Genre = (string)dt.Rows[0]["genre"];
  15.  
  16. return db;
  17. }
  18.  
  19. public static void SaveBook(IBook book)
  20. {
  21. // Code to save the book in the database
  22. // you can use the properties from the dummy book
  23. // to send parameters to your stored proc.
  24. }
  25. }

BLL书:

  1. public class Book : IBook
  2. {
  3. private DummyBook _book;
  4.  
  5. public Book(int id)
  6. {
  7. _book = DALBook.GetBook(id);
  8. }
  9.  
  10. public string Name
  11. {
  12. get {return _book.Name;}
  13. set
  14. {
  15. if (string.IsNullOrEmpty(value))
  16. {
  17. throw new Exception("Invalid Name");
  18. }
  19. _book.Name = value;
  20. }
  21. }
  22.  
  23. // Code for other Properties ...
  24.  
  25.  
  26.  
  27. public void Save()
  28. {
  29. // Add validation if required
  30. DALBook.Save(_book);
  31. }
  32.  
  33. }

Edit1:虚拟类应该在自己的项目中(模型,正如在评论中所述).参考文献的工作如下:

DAL参考模型项目.BLL引用模型和DAL.UI引用BLL.

猜你在找的asp.Net相关文章