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?