我目前有一个申请,其中包括:
用户界面(网页)
BLL(经理&域对象)
DAL(我的每个域对象的DataAccess类).
用户界面(网页)
BLL(经理&域对象)
DAL(我的每个域对象的DataAccess类).
- 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对象并将其返回给Manager?
我正在考虑将DataTable从BookDB返回到BookManager,并创建Book对象并返回它,但这似乎不正确.
有另一种方法吗?
编辑:
我决定在尝试添加对BLL的引用时,将每个层分隔为一个项目,并在DAL层中遇到循环依赖问题.
我无法从DAL访问书类或界面或BLL中的任何内容.
我应该在这里使用ado.net对象,让我的经理从ado.net对象创建实际的对象吗?
这是它的铺设
- BLL.Managers - BookManager
- BLL.Interfaces IBook
- BLL.Domain - Book
- DAL - BookDB.
谢谢!
解决方法
您可以创建仅包含数据的虚拟Book对象.获取,设置属性和成员值.这本书,在数据库中的每个字段有1个属性,但不验证任何东西.
您从数据库填充对象,然后发送到BLL.
当您要保存对象时,您也将其发送到BLL.
您的BLL中的类可以包裹这些对象,如果这是有道理的.这样,很容易将其发回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:虚拟类应该在自己的项目中(模型,正如在评论中所述).参考文献的工作如下:
DAL参考模型项目.BLL引用模型和DAL.UI引用BLL.