using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Entities
{
public class Country
{
public int Id { get; set; }
public string Name { get; set; } = "Bharat";
//Navigation Property
public ICollection <State> states { get; set; }= new HashSet<State>();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Entities
{
public class State
{
public int Id { get; set; }
public string Name { get; set; } = "Maharashtra";
public int CountryId { get; set; }
//Navigation Property
public Country? Country { get; set; }
//Navigation Property
public ICollection <District> districts { get; set; } = new HashSet<District>();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Entities
{
public class District
{
public int Id { get; set; }
public string Name { get; set; } = "Kolhapur";
public int StateId { get; set; }
//Navigation Property
public State? State { get; set; }
}
}
In the Repository Pattern, interfaces play a significant role in defining contracts for data access operations. Interfaces provide a way to define the operations that a repository should support without specifying how those operations are implemented.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.Entities;
namespace Test.Repositories.Interfaces
{
public interface ICountryRepo
{
IEnumerable<Country> GetAll();
Country GetById(int id);
void Save(Country country);
void Edit(Country country);
void RemoveData(Country country);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.Entities;
using Test.Repositories.Interfaces;
namespace Test.Repositories.Implementations
{
public class CountryRepo : ICountryRepo
{
private readonly ApplicationDbContext _context;
public CountryRepo(ApplicationDbContext context)
{
_context = context;
}
public void Edit(Country country)
{
_context.Countries.Update(country);
_context.SaveChanges();
}
public IEnumerable<Country> GetAll()
{
var countries = _context.Countries.ToList();
return countries;
}
public Country GetById(int id)
{
var country = _context.Countries.Find(id);
return country;
}
public void RemoveData(Country country)
{
_context.Remove(country);
_context.SaveChanges();
}
public void Save(Country country)
{
_context.Add(country);
_context.SaveChanges();
}
}
}