I am starting a project to put into practice the lessons learned in "Clean Code" by Robert "Uncle Bob" Martin.
The project is a website that displays the names of items in a DB table. It uses ASP.NET MVC Framework, and I have tried my best to separate domains in the Model. I have used Test Driven Design, but there is a lot of code already, so I have left it out here.
It is extremely basic, but it uses a lot of concepts, and I would like to make sure I have understood everything right before adding complexity.
I am interested in learning how I could have better followed Agile concepts, even if overkill for this little application (or course, any other advice is good to take). In particular, I feel like to controller is doing a lot of logic for a single function (and it is difficult to unit test).
DataAccessLayer.dll
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer.DataTransferObject
{
public class Customer
{
public string Name { get; set; }
public Customer(string inputName)
{
Name = inputName;
}
}
}
ICustomerRepository.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer.Repositories.Interfaces
{
public interface ICustomerRepository
{
ICollection<DataTransferObject.Customer> GetAllCustomerss();
}
}
SQLPlusCustomerRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer.DataTransferObject;
using System.Data;
namespace DataAccessLayer.Repositories
{
public class SQLPlusCustomerRepository : Interfaces.ICustomerRepository
{
private IDbConnection connection;
public SQLPlusCustomerRepository(IDbConnection inputConnection)
{
connection = inputConnection;
}
public ICollection<Customer> GetAllCustomers()
{
ICollection<Customer> resultList = new List<Customer>();
const string getAllCustomerQuery = "SELECT NAME FROM CUSTOMERS";
using (connection)
{
connection.Open();
IDbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandType = CommandType.Text;
dbCommand.CommandText = getAllCustomerQuery;
IDataReader dbDataReader = dbCommand.ExecuteReader();
while (dbDataReader.Read())
{
resultList.Add(new Customer(dbDataReader.GetValue(0).ToString()));
}
return resultList;
}
}
}
}
CustomerDisplay (WebSite)
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CustomerDisplay.Models
{
public class Customer
{
public string Name { get; set; }
public Customer(DataAccessLayer.DataTransferObject.Customer dtoCustomer)
{
Name = dtoCustomer.Name;
}
}
}
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXX.SQLplus;
namespace CustomerDisplay.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
SQLplusConnection connection = new SQLplusConnection(
Properties.Settings.Default.connectionString
);
DataAccessLayer.Repositories.SQLPlusCustomerRepository repository =
new DataAccessLayer.Repositories.SQLPlusCustomerRepository(connection);
ICollection<Models.Customer> viewItemsList = new List<Models.Customer>();
foreach (DataAccessLayer.DataTransferObject.Customer dtoCustomer in repository.GetAllCustomers()){
viewItemsList.Add(new Models.Customers(dtoCustomers));
}
return View(viewItemsList);
}
}
}
Index.cshtml
@model ICollection<CustomerDisplay.Models.Customer>
@foreach(CustomerDisplay.Models.Customer Customer in Model)
{
<span>@Customer.Name</span>
}