Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# N层-将详细信息加载到对象中_C#_.net_Vb.net_N Tier Architecture - Fatal编程技术网

C# N层-将详细信息加载到对象中

C# N层-将详细信息加载到对象中,c#,.net,vb.net,n-tier-architecture,C#,.net,Vb.net,N Tier Architecture,我使用以下结构创建了一个网站: 类项目-名为DataAccessLayer>添加了一个数据集>添加了一个tableAdapter和一个名为GetCustomerById(ID)的查询的Datatable 类项目-调用BusinessLayer>创建了一些代码来调用DataAccessLayer,并在CustomerDataTable中返回查询getCustomerById(ID)的结果 Web项目-添加了对BusinessLayer的引用 从这一点上,我可以添加ObjectDataSource并

我使用以下结构创建了一个网站:

类项目-名为DataAccessLayer>添加了一个数据集>添加了一个tableAdapter和一个名为GetCustomerById(ID)的查询的Datatable

类项目-调用BusinessLayer>创建了一些代码来调用DataAccessLayer,并在CustomerDataTable中返回查询getCustomerById(ID)的结果

Web项目-添加了对BusinessLayer的引用

从这一点上,我可以添加ObjectDataSource并将其绑定到业务层,并调用适当的方法(在本例中为GetCustomerById(ID))

然后,我想添加一个额外的层,希望将所有客户数据加载到客户对象中。因此,我添加了另一个名为Customers类,并将所有字段添加为[B]properties[/B](CustomerID、FirstName、姓氏、AddressOne等)

如何将BusinessLayer中的所有细节加载到此对象中,以便编写如下代码

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

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

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

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();

我不知道你希望这个应用程序有多大,但是你可能想考虑一下主动记录模式或者领域模型模式来帮助你决定如何构建你的层。p> 您可以使用DTO或POCO对象并使用类似Dapper.NET的ORM将数据直接填充到数据表中,而不是填充数据表。然后,您可以使用AutoMapper从DTO填充业务对象

如果您使用活动记录,您可以让业务对象直接调用数据并填充它们自己

你可以做无数不同的事情

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


或者更好的办法是删除关系数据源,使用像RavenDB或Mongo这样的文档数据库,同时删除冗余的层调用。存储灵活的数据对象,而不是僵硬的行。然后,您会发现填充业务对象将是一件轻而易举的事。

我不确定我是否理解这个问题。您想从数据访问对象映射到业务对象吗?我想知道的是,如何加载返回结果的Customer类,以便编写一些代码来查看customername是否为“Bob”,或者如果无法将结果加载到类中,如何检查Customer name是否为Bob,你有一个数据表,你想把它映射到一个类吗?是的,没错。当调用名为GetCustomerById(ID)的业务层方法时,将返回一个Datatable。是否有使用DTO方法的示例?或者,即使你能给我举个例子,数据传输对象只是一个保存数据的对象,没有逻辑。它应该有属性来保存数据,仅此而已。看一看Dapper并复习一下简单的教程。您将看到如何调用数据库,然后将数据映射到DTO对象。
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()