如何使用我实现一个接口,而无需创建于实现的依赖? [英] How do I use my implementation of an interface without creating a dependency on the implementation?

查看:133
本文介绍了如何使用我实现一个接口,而无需创建于实现的依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用我从学习SOLID原则做一个简单的ASP.NET网络表单中获得的知识。

I'm trying to use the knowledge I've gained from studying SOLID principles to make a simple ASP.NET Webform.

我已经设置了我的解决方案分为3个项目:主asp.net web表单项目中,数据访问接口类库项目,而数据访问类库项目(其中有在数据访问接口项目)。

I've set up my solution into 3 projects: the main asp.net webforms project, the Data Access Interfaces class library project, and the Data Access class library project (which has the implementations for the interfaces in the Data Access Interfaces project).

我在数据访问接口组件,它看起来像这样( ICoinStorage 接口>硬币只是一个DTO类,生活在数据访问接口组装):

I have an ICoinStorage interface in the Data Access Interfaces assembly that looks like this (Coin is just a DTO class that lives in the Data Access Interfaces assembly):

public interface ICoinStorage
{
    void Persist(IEnumerable<Coin> coins);
}

和该接口的实现使用ADO.NET中的数据访问组装名为 CoinSqlServerStorage 看起来是这样的:

And the implementation of that interface uses ADO.NET in the Data Access assembly called CoinSqlServerStorage looks like this:

public class CoinSqlServerStorage : ICoinStorage
{
    private string sqlConnectionString;

    public CoinSqlServerStorage(string connectionStringName)
    {
        sqlConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    }

    public void Persist(IEnumerable<Coin> coins)
    {
        using (var connection = new SqlConnection(sqlConnectionString))
        using (var command = new SqlCommand() { Connection = connection })
        {
            foreach (var coin in coins)
            {
                command.Parameters.AddWithValue("@Name", coin.Name);
                command.Parameters.AddWithValue("@Weight", coin.Weight);
                command.Parameters.AddWithValue("@Thickness", coin.Thickness);
                command.Parameters.AddWithValue("@Value", coin.Value);
                command.Parameters.AddWithValue("@Condition", (int)coin.Condition);

                command.CommandText = "INSERT INTO Coins (Name, Weight, Thickness, Value, ConditionID) " +
                                      "VALUES (@Name, @Weight, @Thickness, @Value, @Condition);";

                command.Connection.Open();
                command.ExecuteNonQuery();
            }
        }
    }
}

我的问题是:我如何使用在 web表单项目 CoinSqlServerStorage 类,而无需创建在数据访问组件的依赖?我想让它使用户可以访问一个 InsertCoin.aspx 页面来定义新的硬币,并将它存储新硬币在数据库...

My question is: How do I use the CoinSqlServerStorage class in the webforms project without creating a dependency on the Data Access assembly? I want to make it so the user can visit an InsertCoin.aspx page to define a new coin and have it store the new coin in the database...

我的问题出现了,当我准备让在 Page.Load 事件 CoinSqlServerStorage 类的一个实例在 InsertCoin.aspx 页面,但意识到这会造成对数据访问的依赖,而不是仅仅组装依赖在数据访问接口装配...

My problem arose when I was ready to make an instance of the CoinSqlServerStorage class in the Page.Load event of the InsertCoin.aspx page, but realized this would create a dependency on the Data Access assembly instead of just being dependent on the Data Access Interfaces assembly...

我该如何继续?

推荐答案

在这种情况下,您可以创建一个以上的方案,并呼吁像数据访问DI 这将有到数据访问引用数据访问接口项目。该项目将有责任提供所需要的数据访问接口的实现对所有其他项目(将需要)。

In that case you can create one more project and call that like Data Access DI which will have a references to Data Access and Data Access Interfaces projects. This project will have a responsibility to provider the required realization of Data Access Interfaces to all other project (that will require that).

但即使在这种情况下,你将有两个依赖:数据访问接口数据访问DI - 第一一会提供接口,第二个 - 将提供的实现

But even in this case you will have a two dependencies: Data Access Interfaces and Data Access DI - first one will provide interfaces and the second one - will provide the realizations.

数据访问DI 项目将分离你的其他项目从变现,即使你将有更多的一个数据访问无论项目,说:数据访问蒙戈数据访问SQL 数据访问乌鸦

This Data Access DI project will isolate your other projects from the realizations even when you will have more that one Data Access Whatever projects, say: Data Access Mongo, Data Access Sql, Data Access Raven etc.

这篇关于如何使用我实现一个接口,而无需创建于实现的依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆