Example of a generic repository implementation in C#:
using System;
using System.Collections.Generic;
using System.Linq;
public interface IRepository<T>
{
T GetById(int id);
IEnumerable<T> GetAll();
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
public class Repository<T> : IRepository<T>
{
private List<T> _entities;
public Repository()
{
_entities = new List<T>();
}
public T GetById(int id)
{
return _entities.FirstOrDefault(e => GetId(e) == id);
}
public IEnumerable<T> GetAll()
{
return _entities;
}
public void Add(T entity)
{
_entities.Add(entity);
Console.WriteLine("Added: " + entity.ToString());
}
public void Update(T entity)
{
Console.WriteLine("Updated: " + entity.ToString());
}
public void Delete(T entity)
{
_entities.Remove(entity);
Console.WriteLine("Deleted: " + entity.ToString());
}
// Helper method to retrieve the ID of an entity
private int GetId(T entity)
{
// Replace this with the appropriate logic to extract the ID from your entity
// For demonstration purposes, assuming an 'Id' property exists
var idProperty = typeof(T).GetProperty("Id");
return (int)idProperty.GetValue(entity);
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"Product: Id={Id}, Name={Name}";
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"Customer: Id={Id}, Name={Name}";
}
}
public class Program
{
public static void Main(string[] args)
{
IRepository<Product> productRepository = new Repository<Product>();
IRepository<Customer> customerRepository = new Repository<Customer>();
// Add products
Product newProduct = new Product { Id = 1, Name = "Keyboard" };
productRepository.Add(newProduct);
// Add customers
Customer newCustomer = new Customer { Id = 1, Name = "John Doe" };
customerRepository.Add(newCustomer);
// Update a product
Product existingProduct = productRepository.GetById(1);
if (existingProduct != null)
{
existingProduct.Name = "Wireless Keyboard";
productRepository.Update(existingProduct);
}
// Delete a customer
Customer customerToDelete = customerRepository.GetById(1);
if (customerToDelete != null)
{
customerRepository.Delete(customerToDelete);
}
}
}
In this example, we have a generic repository defined by the IRepository<T>
interface, which provides common methods for data access: GetById
, GetAll
, Add
, Update
, and Delete
.
The Repository<T>
class is the implementation of the generic repository, which uses a List<T>
to store the entities. The implementation showcases basic CRUD operations for the entities. It also includes a helper method GetId
to extract the ID from an entity, assuming there is an Id
property.
The Product
and Customer
classes are sample entities used in the example.
In the Main
method, we create instances of Repository<Product>
and Repository<Customer>
, representing repositories for the Product
and Customer
entities, respectively. We then demonstrate adding products and customers, updating a product, and deleting a customer using the repository methods.