0% found this document useful (0 votes)
38 views8 pages

Lec 4

The repository pattern provides an abstraction layer between the domain and data layers. It defines a common interface for accessing data and decouples the data access logic from the domain layer. To implement it, a repository interface is first created defining data access methods. Then a repository class implements this interface using a data store like Entity Framework. The domain layer interacts solely with the repository interface to remain independent of the underlying data access implementation.

Uploaded by

Osa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

Lec 4

The repository pattern provides an abstraction layer between the domain and data layers. It defines a common interface for accessing data and decouples the data access logic from the domain layer. To implement it, a repository interface is first created defining data access methods. Then a repository class implements this interface using a data store like Entity Framework. The domain layer interacts solely with the repository interface to remain independent of the underlying data access implementation.

Uploaded by

Osa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Repository Pattern

1. Repository Design Pattern acts as a middleman or middle layer


between the rest of the application and the data access logic

2.The repository design pattern is a software design pattern that


provides an abstraction layer between the domain layer and the data
access layer.

3.It decouples the domain layer from the details of how data is stored
and retrieved.
4. It makes the process easier to change the data access mechanism
without affecting the domain layer.
Repository Pattern
Repository Pattern
To implement the repository pattern, you can follow these steps:

1.Create an interface for the repository.


 This interface will define the methods that the repository will expose
to the domain layer.
2.Implement the repository interface. The implementation of the
repository will depend on the data access mechanism that you are using.
For example, if you are using a database, you can implement the
repository using the Entity Framework.

1. Use the repository in the domain layer. The domain layer will use the
repository to access data.
// The interface for the repository.
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
void Save(T entity);
}
Repository Pattern
1.The IEnumerable<T> interface represents a sequence of objects that can be
enumerated.
2.The T generic parameter specifies the type of the objects in the sequence.

3.The IEnumerable<T> interface has a single method called


GetEnumerator() that returns an IEnumerator<T> object.
4.The IEnumerator<T> object can be used to iterate through the sequence of
objects.

5.The IEnumerable<T> interface is used to abstract the concept of a sequence


of objects.
6.This allows us to write code that can be used to iterate through any sequence
of objects, regardless of the specific type of the objects in the sequence
// The implementation of the repository.
public class Repository<T> : IRepository<T>
{
private readonly DbContext _dbContext;
public Repository(DbContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<T> GetAll()
{
return _dbContext.Set<T>().ToList();
}
public T GetById(int id)
{
return _dbContext.Set<T>().Find(id);
}
public void Save(T entity)
{
_dbContext.SaveChanges();
}
}
// The application class.
public class Application
{ private readonly IRepository<Customer> _customerRepository;
public Application(IRepository<Customer> customerRepository)
{
_customerRepository = customerRepository;
}
public void GetAllCustomers()
{ var customers = _customerRepository.GetAll();
foreach (var customer in customers)
{
Console.WriteLine(customer.Name);
}
}
public void GetCustomerById(int id)
{ var customer = _customerRepository.GetById(id);
Console.WriteLine(customer.Name);
}
public void SaveCustomer(Customer customer)
{
_customerRepository.Save(customer);
}
}
// The main method.
public class Program
{
public static void Main(string[] args)
{
var dbContext = new DbContext();
var customerRepository = new Repository<Customer>(dbContext);

var application = new Application(customerRepository);

application.GetAllCustomers();
application.GetCustomerById(1);
application.SaveCustomer(new Customer { Name = "John Doe" });
}
}

You might also like