示例
添加一个.NET Core类库项目,项目名称RepositoryContracts
添加Entities项目引用
添加ICountriesRepository.cs
using Entities;
namespace RepositoryContracts
{
/// <summary>
/// Represents data access logic for managing Country entity
/// </summary>
public interface ICountriesRepository
{
/// <summary>
/// Adds a new country object to the data store
/// </summary>
/// <param name="country">Country object to add</param>
/// <returns>Returns the country object after adding it to the data store</returns>
Task<Country?> AddCountryAsync(Country country);
/// <summary>
/// Returns all countries in the data store
/// </summary>
/// <returns>All countries from the table</returns>
Task<IEnumerable<Country>?> GetAllCountriesAsync();
/// <summary>
/// Returns a country object based on the given country id;
/// otherwise, it returns null
/// </summary>
/// <param name="countryId">CountryId to search</param>
/// <returns>Matching country or null</returns>
Task<Country?> GetCountryByCountryIdAsync(Guid countryId);
/// <summary>
/// Returns a country object based on the given country name
/// </summary>
/// <param name="countryName">Country name to search</param>
/// <returns>Matching country or null</returns>
Task<Country?> GetCountryByCountryNameAsync(string countryName);
}
}
添加IPersonsRepository.cs
using Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace RepositoryContracts
{
/// <summary>
/// Represents data access logic for managing Person entity
/// </summary>
public interface IPersonsRepository
{
/// <summary>
/// Adds a person object to the data store
/// </summary>
/// <param name="person">Person object to add</param>
/// <returns>Returns the person object after adding it to the table</returns>
Task<Person?> AddPersonAsync(Person person);
/// <summary>
/// Returns all persons in the data store
/// </summary>
/// <returns>List of person objects from table</returns>
Task<IEnumerable<Person>?> GetAllPersonsAsync();
/// <summary>
/// Returns a person object based on the given person id
/// </summary>
/// <param name="personId">PersonId(guid) to search</param>
/// <returns>A person object or null</returns>
Task<Person?> GetPersonByPersonIdAsync(Guid personId);
/// <summary>
/// Returns all person objects based on the given expression
/// </summary>
/// <param name="predicate">LINQ expression to check</param>
/// <returns>All matching persons with given condition</returns>
Task<IEnumerable<Person>?> GetFilteredPersonsAsync(Expression<Func<Person,bool>> predicate);
/// <summary>
/// Deletes a person object based on the person id
/// </summary>
/// <param name="PersonId">Person Id(guid) to search</param>
/// <returns>Returns true, if the deletion is successful, otherwise false</returns>
Task<bool> DeletePersonByPersonId(Guid PersonId);
/// <summary>
/// Updates a person object(person name and other details) based on the given person id
/// </summary>
/// <param name="person">Person object to update</param>
/// <returns>Returns the updated person object</returns>
Task<Person?> UpdatePerson(Person person);
}
}
Gitee获取源码: