I just wanna ask. Is it OK to create one interface
per table
or class
like this:
public interface ICustomer{
long id;
string firstname;
string lastname;
}
[Table("Customer")]
public class Customer : ICustomer{
public long id {get;set;}
//implementation etc...
}
public interface ICustomerDetails{
long id;
bool isMarried;
string billingAddress;
}
[Table("CustomerDetails")]
public class CustomerDetails : ICustomerDetails{
[Key]
public long id {get;set;}
//implementation etc...
}
I want to apply DI principle and I have 50 tables right now. I'm going to follow the SOLID principle. This is existing project. I'm newbie in OOP and trying to learn this principle
Without knowing more about WHY you want to do this, it's hard to give more details. However, based on what you've provided in your question, the short answer is NO. You wouldn't normally inject objects like these (entities).
Typically, you'd inject a service, or a repository, or something that has functionality (methods), because that functionality will need to be different in unit tests (you don't want to actually do anything with a database in a unit test, for example).
If you want to instantiate a customer, then just do that. No need to inject it. For example (this is more MVC than WebApi, but the principle is the same):
public class Customer
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public interface ICustomerRepository
{
List<Customer> GetCustomers();
Customer GetCustomer(long id);
void SaveCustomer(Customer customer);
}
public class CustomerRepository : ICustomerRepository
{
public List<Customer> GetCustomers()
{
// something here to retrieve the list of customers from a database
}
public Customer GetCustomer(long id)
{
// something here to retrieve the specific customer from a database
}
public void SaveCustomer(Customer customer)
{
// save the customer
}
}
Then you would inject the repository into your controller:
public class CustomerController : Controller
{
private readonly ICustomerRepository _customerRepository;
public CustomerController(ICustomerRepository repository)
{
_customerRepository = repository;
}
// GET: Customer
public ActionResult Index()
{
var customers = _customerRepository.GetCustomers();
// typically, here you would map the customers to a viewmodel, but for the sake of brevity...
return View(customers);
}
[HttpPost]
public ActionResult Create(FormCollection collection)
{
// there are better ways to do this (look at model binding), but as an example:
var customer = new Customer();
customer.Id = collection["id"];
customer.FirstName = collection["firstname"];
customer.LastName = collection["lastname"];
_customerRepository.SaveCustomer(customer);
return RedirectToAction("Index");
}
}
If this isn't what you were going for, perhaps you can provide more detail in your question and get a better idea of how to accomplish your goals. But if you just want to experiment with Dependency Injection, this should suffice. So, there's no need for the interfaces you've created (ICustomerDetails and ICustomer).