Search code examples
c#asp.netwebformsdependency-managementsolid-principles

How do I use my implementation of an interface without creating a dependency on the implementation?


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

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).

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

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

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...

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...

How do I proceed?


Solution

  • 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).

    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.

    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.