0

I created a website using the following structure:

Class Project - Called DataAccessLayer > Added a Dataset > Add a tableAdapter and Datatable with a query called GetcustomersByID(ID)

Class Project - Called BusinessLayer > Created some code to call into the DataAccessLayer and return the results in a CustomerDataTable for the query GetcustomersByID(ID)

Web Project - Added a reference to the BusinessLayer.

From this point i can add an ObjectDataSource and tie it to the Business Layer and call the appropriate method (in this example GetCustomersByID(ID)).

I then wanted to add an extra layer where i was hoping to load all customer data into a customer object. So i add another class called Customers and add all the fields as [B]properties [/B](CustomerID, FirstName, Surname, AddressOne etc).

How could i load all the details from the BusinessLayer into this object, so i could write code such as

Dim myCustomer as Customer
....
...... Some code to get the data and load it into the Customer object.

If myCustomer.Firstname = "Bob" Then
....
End If
4

2 回答 2

0

好吧,我不知道您希望这个应用程序扩展到什么程度,但您可能需要考虑查看 Active Record 模式或 Domain Model 模式,以帮助决定如何构建您的层。

您可以使用 DTO 或 POCO 对象代替填充数据表,并使用像 Dapper.NET 这样的 ORM 将数据直接填充到它们中。然后您可以使用 AutoMapper 从 DTO 填充您的业务对象。

如果您使用 Active Record,您可以让您的业务对象直接调用数据并自行填充。

你可以做无数不同的事情。

此外,您可能不希望前端调用服务层或外观层,而不是直接调用您的业务逻辑。

或者更好的是删除关系数据源并使用 RavenDB 或 Mongo 等文档数据库,并一起删除冗余层调用。存储灵活的数据对象而不是死板的行。然后您会发现填充您的业务对象将是一件轻而易举的事。

于 2013-02-10T17:21:37.407 回答
0

为了从数据表中提取数据,您可以执行以下操作:

Customer customer = dt.AsEnumerable().Select(row => 
        // construct and map all the properties
        new Customer
        {
            Id = row.Field<int>("Id"),
            Firstname = row.Field<string>("Name")
        }).FirstOrDefault();

在 VB.NET 中是这样的(虽然,我不是 VB 人):

Dim customer As Customer = dt.AsEnumerable().[Select](Function(row) New Customer() With     { _
    Key .Id = row.Field(Of Integer)("Id"), _
    Key .Firstname = row.Field(Of String)("Name") _
}).FirstOrDefault()
于 2013-02-10T16:36:53.890 回答