Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Typ Gateway
- public enum GatewayType
- {
- gtSQL, gtXML
- }
- //BaseEntity - vlastnosti společné pro všechny entity
- public class BaseEntity
- {
- public int Id { get; set; }
- }
- //Interface pro všechny Gateway
- public interface IGateway<T> where T : BaseEntity
- {
- IEnumerable<T> List { get; }
- bool Add(T entity);
- bool Delete(T entity);
- bool Update(T entity);
- T FindById(int Id);
- IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
- }
- //Příklad User SQL gateway
- public class UserGateway_SQL : IGateway<User>
- {
- public IEnumerable<User> List
- {
- get
- {
- throw new NotImplementedException();
- }
- }
- public bool Add(User entity)
- {
- throw new NotImplementedException();
- }
- public bool Delete(User entity)
- {
- throw new NotImplementedException();
- }
- public User FindById(int Id)
- {
- throw new NotImplementedException();
- }
- public IQueryable<User> SearchFor(Expression<Func<User, bool>> predicate)
- {
- throw new NotImplementedException();
- }
- public bool Update(User entity)
- {
- throw new NotImplementedException();
- }
- }
- //Mapper pro Usera
- public class UserMapper
- {
- private IGateway<User> gateway;
- public UserMapper(GatewayType type)
- {
- if (type == GatewayType.gtXML)
- {
- this.gateway = new UserGateway_XML();
- }
- else if (type == GatewayType.gtSQL)
- {
- this.gateway = new UserGateway_SQL();
- }
- }
- public List<User> getUsers() {
- List<User> users = new List<User>();
- users.AddRange(gateway.List);
- return users;
- }
- public bool insertUser(User user) {
- return gateway.Add(user);
- }
- }
- //User model
- public class User : BaseEntity
- {
- public string Name { get; set; }
- public User(int id, string name) {
- this.Id = id;
- this.Name = name;
- }
- public static User newIntance(string name) {
- return new User(-1, name);
- }
- }
- //Example
- User user = User.newIntance("Uživatel 1");
- UserMapper userMapper = new UserMapper(GatewayType.gtSQL); //Předělám na globální proměnnou
- List<User> userList = userMapper.getUsers();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement