我正在努力学习如何进行单元测试和模拟.我理解TDD和基本测试的一些原理.但是,我正在考虑重构下面的代码,这些代码是在没有测试的情况下编写的,并且我试图了解它是如何更改以使其可测试的.
public class AgentRepository
{
public Agent Select(int agentId)
{
Agent tmp = null;
using (IDataReader agentInformation = GetAgentFromDatabase(agentId))
{
if (agentInformation.Read())
{
tmp = new Agent();
tmp.AgentId = int.Parse(agentInformation["AgentId"].ToString());
tmp.FirstName = agentInformation["FirstName"].ToString();
tmp.LastName = agentInformation["LastName"].ToString();
tmp.Address1 = agentInformation["Address1"].ToString();
tmp.Address2 = agentInformation["Address2"].ToString();
tmp.City = agentInformation["City"].ToString();
tmp.State = agentInformation["State"].ToString();
tmp.PostalCode = agentInformation["PostalCode"].ToString();
tmp.PhoneNumber = agentInformation["PhoneNumber"].ToString();
}
}
return tmp;
}
private IDataReader GetAgentFromDatabase(int agentId)
{
SqlCommand cmd = new SqlCommand("SelectAgentById");
cmd.CommandType = CommandType.StoredProcedure;
SqlDatabase sqlDb = new SqlDatabase("MyConnectionString");
sqlDb.AddInParameter(cmd, "AgentId", DbType.Int32, …Run Code Online (Sandbox Code Playgroud) 我有多个共享一些数据协定的WCF服务,需要使用svcutil.exe生成客户端代码.我使用两种最明显的方法来解决错误并需要一些帮助.
但首先,这是服务:
[ServiceContract( Namespace = "http://www.me.com/services/" )]
public interface IFooService {
[OperationContract]
Response RunFoo( Request request );
}
[ServiceContract( Namespace = "http://www.me.com/services/" )]
public interface IBarService {
[OperationContract]
Response RunBar( Request request );
}
Run Code Online (Sandbox Code Playgroud)
响应和请求在单独的程序集中定义:
[DataContract( Namespace = "http://www.me.com/shared/" )]
public class Request {
[DataMember]
public int Input { get; set; }
}
[DataContract( Namespace = "http://www.me.com/shared/" )]
public class Response {
[DataMember]
public int Result { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这些服务以一些微不足道的方式实现,编译,发布 - 让我们现在切换到客户端.
在svcutil命令行中包含这两个服务 - 如下所示:
svcutil /o:Client.cs http://hostname.com/FooService.svc …Run Code Online (Sandbox Code Playgroud) 根据维基百科的说法,该术语最初是由数据库专家Joe Celko在1982年创造的,这里引用了他1997年的文章,揭示了他的意思:"标准的[结构化编程]解决方案是用嵌套的IF-THEN取代GOTO -ELSE语句和交换机分层如此之深,冗余太多,你有一个控制路径,看起来像一盘烤宽面条."
然而,相同的维基百科条目定义了不同的术语,明确指的是多层应用程序:"......不同的子系统,例如...... Web应用程序代码,业务逻辑和关系数据库." 因此,随着程序员开发新的方法和体系结构,术语的含义也随之发展.
我可以想到两个"烤宽面条代码"的现代定义:
这个词显然意味着批评(就像你可以通过称之为"意大利面条代码"来批评我的东西).但这对你意味着什么,你什么时候使用它?