Repository pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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