Created
May 1, 2013 18:05
-
-
Save yemrekeskin/5497019 to your computer and use it in GitHub Desktop.
Identity Map Design Pattern
This file contains hidden or 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
| public abstract class BaseEntity | |
| { | |
| // Default identity(key) for Each Object | |
| public int Id { get; set; } | |
| // Basic properties | |
| public DateTime CreateDate { get; set; } | |
| public DateTime UpdateDate{ get; set; } | |
| public bool IsActive { get; set; } | |
| } | |
| public class Account | |
| :BaseEntity | |
| { | |
| } | |
| public class IdentityMap<TObject> | |
| { | |
| Hashtable entities = new Hashtable(); | |
| // in here the key is identity properties (such as Id,AccountNumber etc.) | |
| public TObject GetById(int key) | |
| { | |
| if (entities.ContainsKey(key)) | |
| return (TObject)entities[key]; | |
| else | |
| return default(TObject); | |
| } | |
| public void Add(TObject entity,int key) | |
| { | |
| if (!entities.Contains(key)) | |
| entities.Add(key, entity); | |
| } | |
| } | |
| public interface IAccountRepository<TObject> | |
| { | |
| // Espeacially for find methods | |
| // Cause These methods just returns a single object | |
| //IQueryable<TObject> All(); | |
| //IQueryable<TObject> Filter(Expression<Func<TObject, bool>> predicate); | |
| //IQueryable<TObject> Filter<Key>(Expression<Func<TObject, bool>> filter, | |
| // out int total, int index = 0, int size = 50); | |
| TObject Find(int keys); | |
| TObject Find(Expression<Func<TObject, bool>> predicate); | |
| //bool Contains(Expression<Func<TObject, bool>> predicate); | |
| //TObject Create(TObject t); | |
| //int Delete(TObject t); | |
| //int Delete(Expression<Func<TObject, bool>> predicate); | |
| //int Update(TObject t); | |
| } | |
| public class AccountRepository | |
| :IAccountRepository<Account> | |
| { | |
| IdentityMap<Account> identityMap; | |
| public AccountRepository() | |
| { | |
| this.identityMap = new IdentityMap<Account>(); | |
| } | |
| // identity Control and identityMap add(like pool mechanism) | |
| public Account Find(int key) | |
| { | |
| Account account = identityMap.GetById(key); | |
| if (null == account) | |
| { | |
| account = GetById(key); | |
| if (null != account) | |
| identityMap.Add(account, account.Id); | |
| } | |
| return account; | |
| } | |
| // directly get data from database | |
| public Account GetById(int key) | |
| { | |
| Account account = default(Account); | |
| // operations to retrieval of data object from database | |
| return account; | |
| } | |
| public Account Find(Expression<Func<Account, bool>> predicate) | |
| { | |
| // Above mapping control process can apply for this method | |
| return default(Account); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment