Skip to content

Instantly share code, notes, and snippets.

@deepumi
Last active March 20, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepumi/08b95d586d4b8360051d11765cb2f2a8 to your computer and use it in GitHub Desktop.
Save deepumi/08b95d586d4b8360051d11765cb2f2a8 to your computer and use it in GitHub Desktop.
Repository pattern
//Insert new record to database
public interface ICreatableRepository<T>
{
int Create(T value);
}
//List records
public interface IListableRepository<T>
{
IList<T> List(DataParameterDictionary dataParameters);
}
//Database Pagination list
public interface IPageableRepository<T>
{
IPageableResult<T> PageList(DataParameterDictionary dataParameters);
}
//Get single record from database
public interface IRetrievableRepository<T>
{
T Retrieve(DataParameterDictionary dataParameters);
}
//Update a record
public interface IUpdatableRepository<T>
{
int Update(T value);
}
//Delete a record
public interface IDeletableRepository
{
int Delete(DataParameterDictionary dataParameters);
}
public interface IPageableResult<T>
{
IList<T> Result { get; }
int TotalRecords { get; }
}
public sealed class DataParameterDictionary : Dictionary<string, object>
{
public DataParameterDictionary() : base(StringComparer.OrdinalIgnoreCase)
{
}
}
//Sample for using pagination interface.
public class UserModel
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
public sealed class UserListRepository : IPageableRepository<UserModel>
{
public IPageableResult<UserModel> PageList(DataParameterDictionary dataParameters)
{
var items = new List<UserModel>
{
new UserModel {FirstName = "Deepu", LastName = "Madhusoodanan"}
};
return new UserPageList(items, 1);
}
}
public sealed class UserPageList : IPageableResult<UserModel>
{
public IList<UserModel> Result { get; }
public int TotalRecords { get; }
public UserPageList(IList<UserModel> users, int totalRecords)
{
Result = users;
TotalRecords = totalRecords;
}
}
public class UserRepositoryTest
{
public static void TestListUser()
{
var userListRepository = new UserListRepository();
var result = userListRepository.PageList(default);
var tota = userListRepository.TotalRecords;
var users = userListRepository.Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment