Advertisement
MarMar_IV

Untitled

Nov 5th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. //Typ Gateway
  2.     public enum GatewayType
  3.     {
  4.         gtSQL, gtXML
  5.     }
  6.  
  7. //BaseEntity - vlastnosti společné pro všechny entity
  8.     public class BaseEntity
  9.     {
  10.         public int Id { get; set; }
  11.     }
  12.  
  13. //Interface pro všechny Gateway
  14.     public interface IGateway<T> where T : BaseEntity
  15.     {
  16.         IEnumerable<T> List { get; }
  17.         bool Add(T entity);
  18.         bool Delete(T entity);
  19.         bool Update(T entity);
  20.         T FindById(int Id);
  21.         IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
  22.     }
  23.  
  24.  
  25. //Příklad User SQL gateway
  26.     public class UserGateway_SQL : IGateway<User>
  27.     {
  28.         public IEnumerable<User> List
  29.         {
  30.             get
  31.             {
  32.                 throw new NotImplementedException();
  33.             }
  34.         }
  35.  
  36.         public bool Add(User entity)
  37.         {
  38.             throw new NotImplementedException();
  39.         }
  40.  
  41.         public bool Delete(User entity)
  42.         {
  43.             throw new NotImplementedException();
  44.         }
  45.  
  46.         public User FindById(int Id)
  47.         {
  48.             throw new NotImplementedException();
  49.         }
  50.  
  51.         public IQueryable<User> SearchFor(Expression<Func<User, bool>> predicate)
  52.         {
  53.             throw new NotImplementedException();
  54.         }
  55.  
  56.         public bool Update(User entity)
  57.         {
  58.             throw new NotImplementedException();
  59.         }
  60.     }
  61.  
  62. //Mapper pro Usera
  63.     public class UserMapper
  64.     {
  65.         private IGateway<User> gateway;
  66.  
  67.         public UserMapper(GatewayType type)
  68.         {
  69.             if (type == GatewayType.gtXML)
  70.             {
  71.                 this.gateway = new UserGateway_XML();
  72.             }
  73.             else if (type == GatewayType.gtSQL)
  74.             {
  75.                 this.gateway = new UserGateway_SQL();
  76.             }
  77.         }
  78.  
  79.         public List<User> getUsers() {
  80.             List<User> users = new List<User>();
  81.             users.AddRange(gateway.List);
  82.  
  83.             return users;
  84.         }
  85.  
  86.         public bool insertUser(User user) {
  87.             return gateway.Add(user);
  88.         }
  89.     }
  90.  
  91. //User model
  92.     public class User : BaseEntity
  93.     {
  94.         public string Name { get; set; }
  95.  
  96.         public User(int id, string name) {
  97.             this.Id = id;
  98.             this.Name = name;
  99.         }
  100.  
  101.         public static User newIntance(string name) {
  102.             return new User(-1, name);
  103.         }
  104.     }
  105.  
  106.  
  107. //Example
  108.             User user = User.newIntance("Uživatel 1");
  109.             UserMapper userMapper = new UserMapper(GatewayType.gtSQL);  //Předělám na globální proměnnou
  110.  
  111.             List<User> userList = userMapper.getUsers();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement