【Translation】接口 vs. 实体类

在设计.net程序架构的时候,我更倾向于使用接口而不是实体类在作为函数的参数。

我们来看看下面这个例子:

第一个方法public IList<Article> Get(),他调用数据库,并得到一个包含了查询结果数据集合的SqlDataReader,然后调用第二个方法private IList<Article> FillArticles(SqlDataReader reader)的将SqlDataReader中的结果添加到IList<Article>中。

 

public  IList < Article >  Get()
{
    SqlConnection connection 
=   new  SqlConnection(_connectionString);
    SqlCommand command 
=   new  SqlCommand();
    command.Connection 
=  connection;
    command.CommandType 
=  CommandType.StoredProcedure;
    command.CommandText 
=   " GetAllArticles " ;
 
    SqlDataReader reader 
=  command.ExecuteReader(CommandBehavior.SingleResult);
 
    
return  FillArticles(reader);
}

 

 

private  IList < Article >  FillArticles(SqlDataReader reader)
{
    List
< Article >  articles  =   new  List < Article > ();
    
while  (reader.Read())
    {
        Article article 
=   new  Article();
        article.ArticleID 
=  ( int )reader[ " ArticleID " ];
        article.Title 
=  reader[ " Title " ];
        article.Body 
=  reader[ " Body " ];
        article.Published 
=  (DateTime)reader[ " Published " ];
        articles.Add(article);
    }
    
return  articles;
}

通过上面这个例子你可以发现,FillArticles方法需要一个SqlDataReader (这是一个实体类)。好,现在需求变了,现在数据都存储在了XML文件中,这个时候,我们得到就是XmlDataReader实际没有这个类型)而不是SqlDataReader了。很不幸,你唯一能做的就是修改这块的源代码。

 

那么,我们怎么样才能避免这样的问题呢?我们假设SqlDataReaderXmlDataReader都实现了IDataReader接口。我们只需要把代码修改成如下的样子即可解决开始遇到的问题了:

 

 

private  IList < Article >  FillArticles(IDataReader reader)
{
    List
<Article> articles = new List<Article>();
    
while (reader.Read())
    
{
        Article article 
= new Article();
        article.ArticleID 
= (int)reader["ArticleID"];
        article.Title 
= reader["Title"];
        article.Body 
= reader["Body"];
        article.Published 
= (DateTime)reader["Published"];
        articles.Add(article);
    }

    
return articles;
}

 

这就是使用接口作为方法的参数,还不使用实体类的好处;)

 

原文

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值