Alt*_*ept 10 architecture asp.net data-access-layer bll
我目前有一个应用程序,它包括:用户界面(网页)BLL(管理器和域对象)DAL(我的每个域对象的DataAccess类).
我在UI中使用以下内容来搜索域对象.
protect sub Button1_Click()
{
IBook book = BookManager.GetBook(txtID.Text);
}
Run Code Online (Sandbox Code Playgroud)
这是我的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
}
Run Code Online (Sandbox Code Playgroud)
最后这是我的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
}
Run Code Online (Sandbox Code Playgroud)
如何创建一个IBook对象并将其返回给Manager?我正在考虑将一个DataTable从BookDB返回到BookManager并让它创建Book对象并将其返回,但这似乎不对.还有另一种方法吗?
编辑:我决定将每个图层分成一个项目,并在尝试添加对BLL的引用时在DAL层中遇到循环依赖问题.我无法从DAL访问Book Class或Interface或BLL中的任何内容.我应该在这里使用ado.net对象并让我的经理从ado.net对象创建实际对象吗?这是它的布局
BLL.Managers - BookManager
BLL.Interfaces IBook
BLL.Domain - Book
DAL - BookDB.
Run Code Online (Sandbox Code Playgroud)
谢谢!
您可以创建仅包含数据的虚拟Book对象.获取,设置属性和成员值.本书对数据库中的每个字段都有1个属性,但不验证任何内容.
您从db填充对象,然后将其发送到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;}
}
}
Run Code Online (Sandbox Code Playgroud)
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.
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Edit1:虚拟类应该放在他们自己的项目中(模型,正如评论中所述的那样).参考文献将如下工作:
DAL引用模型项目.
BLL引用模型和DAL.
UI引用BLL.
小智 0
你想要返回的DataTable是与数据库相关的,对于BLL来说,它不应该关心你正在使用什么数据库以及schema是什么。您可以使用 DB-Object Mapper 将 dbtable 映射到 DAL 中的对象。
| 归档时间: |
|
| 查看次数: |
5128 次 |
| 最近记录: |