为引用另一个对象的对象创建数据访问层的最佳实践是什么?

发布于 2024-07-06 08:57:19 字数 300 浏览 11 评论 0原文

(对不起我的英语不好) 例如,在我的 DAL 中,我有一个 AuthorDB 对象,它有一个名称 和一个 BookDB 对象,它有一个 Title 和一个 IdAuthor。

现在,如果我想显示所有书籍及其相应作者的姓名,我必须获取所有书籍的集合,并使用 IdAuthor 属性为每本书找到作者的姓名。 这会对数据库进行大量查询,显然可以使用简单的 JOIN。

我有什么选择? 创建一个包含作者姓名和书名的“自定义”对象? 如果是这样,维护工作可能会变得很糟糕。

那么,有哪些选择呢? 谢谢你!

(sorry for my English)
For example, in my DAL,I have an AuthorDB object, that has a Name
and a BookDB object, that has a Title and an IdAuthor.

Now, if I want to show all the books with their corresponding author's name, I have to get a collection of all the Books, and for each of them, with the IdAuthor attribute, find the Author's name. This makes a lot of queries to the database, and obviously, a simple JOIN could be used.

What are my options? Creating a 'custom' object that contains the author's name and the title of the book? If so, the maintenance could become awful.

So, what are the options?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

舟遥客 2024-07-13 08:57:19

当可靠、高效和通用的工具可用时,不要编写有缺陷、低效和专业化的东西。 免费。

选择一个 ORM。 NHibernate、ActiveRecord、SubSonic、NPersist、LinqToEF、LinqToSQL、LLBLGenPro、DB4O、CSLA 等。

Don't write something buggy, inefficient, and specialized ... when reliable, efficient, and generic tools are available. For free.

Pick an ORM. NHibernate, ActiveRecord, SubSonic, NPersist, LinqToEF, LinqToSQL, LLBLGenPro, DB4O, CSLA, etc.

书信已泛黄 2024-07-13 08:57:19

您可以在数据库中创建一个内置联接的视图,并将一个对象绑定到该视图,例如 AuthorBooksDB。 它不会造成太严重的维护麻烦,因为视图可以隐藏任何潜在的更改并保持静态。

You can create a View in the database that has the join built into it and bind an an object to that, e.g. AuthorBooksDB. It doesn't create too bad a maintenance headache since the view can hide any underlying changes and remains static.

栖迟 2024-07-13 08:57:19

如果您可以将数据库查询与对象构建分开,那么您可以创建一个查询来获取所需的数据。 然后将该数据传递给您的构建器并让它返回您的书籍。

使用 Linq to SQL,可以轻松完成以下操作:

public IEnumerable<Book> AllBooks()
{
    return  from book in db.Books
            join author in db.Authors on book.AuthorId equals author.Id
            select new Book() 
                        { 
                            Title = book.Title, 
                            Author = author.Name,
                        };
}

使用 DataTables / DataSets 也可以实现同样的效果:

public IEnumerable<Book> AllBooks()
{
    DataTable booksAndAuthors = QueryAllBooksAndAuthors(); // encapsulates the sql query

    foreach (DataRow row in booksAndAuthors.Rows)
    {
        Book book = new Book();
        book.Title = row["Title"];
        book.Author = row["AuthorName"];
        yield return book;
    }
}

If you can separate the database query from the object building, then you could create a query to get the data you need. Then pass that data to your builder and let it return your books.

With Linq to SQL, that can be done as easily as:

public IEnumerable<Book> AllBooks()
{
    return  from book in db.Books
            join author in db.Authors on book.AuthorId equals author.Id
            select new Book() 
                        { 
                            Title = book.Title, 
                            Author = author.Name,
                        };
}

The same can be achieved with DataTables / DataSets:

public IEnumerable<Book> AllBooks()
{
    DataTable booksAndAuthors = QueryAllBooksAndAuthors(); // encapsulates the sql query

    foreach (DataRow row in booksAndAuthors.Rows)
    {
        Book book = new Book();
        book.Title = row["Title"];
        book.Author = row["AuthorName"];
        yield return book;
    }
}
白云不回头 2024-07-13 08:57:19

非常感谢您的投入。

实际上,我们试图使数据库对象尽可能接近数据库中相应表的实际列。 这就是为什么我们不能真正向 BookDB 对象添加(字符串)“Author”属性。

这是我在使用“View”对象时遇到的问题。 在数据库中,如果由于任何原因必须修改模式(例如:在 Book 表中,必须将“Title”列修改为“The_Title”,我们如何轻松地知道必须修改的所有“View”对象换句话说,当它们进行使用多个连接的查询时,我如何知道哪些对象必须被修改?

这里,由于我们有一个 AuthorsBooks 对象,我们可以通过名称看到它可能对书籍进行查询。但是,对于在表之间进行 4 或 5 个连接的对象,我们不能依赖对象名称。

(再次感谢您,这是一个很棒的网站!)

Thank you very much for your inputs.

Actually, we are trying to keep the database objects as close as possible to the actual columns of the corresponding table in the database. That's why we cannot really add a (string) 'Author' attribute to the BookDB object.

Here is the problem I see with using 'View' objects. In the database, if the schema has to be modified for any reason (e.g: In the Book table, the 'Title' column has to be modified for 'The_Title', how do we easily know all the 'View' objects that have to be modified? In other words, how to I know what objects have to be modified when they make queries that use multiple joins?

Here, since we have a AuthorsBooks object, we can see by the name that it probably makes a query to the book and author tables. However, with objects that make 4 or 5 joins between tables we cannot rely on the object name.

Any ideas? (Thank you again, this is a great site!)

水中月 2024-07-13 08:57:19

我建议您看一下领域驱动设计。 在 DDD 中,您从存储库获取所有业务对象。 存储库隐藏了您的数据存储和实现,并将解决您如何查询数据和跟踪数据库更改的问题。 由于每个业务对象都是从存储库中检索的,因此存储库将成为您的单点更改。 然后,存储库可以以您认为有效的任何方式查询数据库,然后从该数据构建域对象:

var books = new BookRepository().GetAllBooks();

您应该能够使用 Justice 提到的任何技术对存储库进行编码。

I suggest you take a look at Domain Driven Design. In DDD, you get all business objects from a repository. The repository hides your data store and implementation, and will solve your problems on how to query data and keeping track of database changes. Because every business object is retrieved from the repository, the repository will be your single point of change. The repository can then query your database in any way you find efficient, and then build your domain objects from that data:

var books = new BookRepository().GetAllBooks();

You should be able to code the repositories with any of the technologies mentioned by Justice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文