Created
August 30, 2011 19:03
-
-
Save jonezy/1181723 to your computer and use it in GitHub Desktop.
Base class for subsonic data access files, just inherit this class (rename the db type to match your own), provide the subsonic entity type. Has deps on subsonic and richmondday.helpers
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using CollectBruceLee; | |
| using CollectBruceLee.Data; | |
| using RichmondDay.Helpers; | |
| using SubSonic.Query; | |
| using SubSonic.Repository; | |
| public abstract class ServiceBase<T> where T :class, new() { | |
| protected virtual double CacheExpiry { | |
| get { return 60;} | |
| } // time in seconds the cache should live for | |
| protected abstract string CacheKey { | |
| get; | |
| } // a unique way to identify the item in the cache | |
| protected CacheHelper CacheHelper; | |
| protected IQuerySurface db; | |
| public ServiceBase() { | |
| if(db == null) db = new ServiceLocator().GetService<IQuerySurface>(); | |
| if (App.CachingEnabled) { | |
| if (CacheHelper == null) CacheHelper = new CacheHelper(HttpContext.Current.Cache); | |
| } // a good place to put App.CachingEnabled is in your Global.asax.cs | |
| } | |
| public ServiceBase(IQuerySurface _db) { | |
| // should do something serious if _db is null. | |
| if (db == null) db = _db; // change this to match your concrete implementation | |
| if (App.CachingEnabled) { | |
| if (CacheHelper == null) CacheHelper = new CacheHelper(HttpContext.Current.Cache); | |
| } // a good place to put App.CachingEnabled is in your Global.asax.cs | |
| } | |
| protected SubSonicRepository<T> GetRepository<T>() where T : class, new() { | |
| return new SubSonicRepository<T>(db); | |
| } | |
| protected virtual List<T> GetData() { | |
| List<T> data = CacheHelper != null && CacheExpiry > 0 ? CacheHelper.Get(CacheKey) as List<T> : null; | |
| if (data == null) { | |
| data = GetRepository<T>().GetAll().ToList(); | |
| if (CacheHelper != null) | |
| CacheHelper.Add(CacheKey, data, DateTime.Now.AddSeconds(CacheExpiry)); | |
| } | |
| return data; | |
| } | |
| public virtual void Delete(int id) { | |
| GetRepository<T>().Delete(id); | |
| } | |
| } | |
| /// <summary> | |
| /// started doing this based on these posts | |
| /// http://stefanoricciardi.com/2009/10/29/a-singleton-service-locator-pattern/ | |
| /// http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/ | |
| /// </summary> | |
| interface IServiceLocator { | |
| T GetService<T>(); | |
| } | |
| class ServiceLocator : IServiceLocator { | |
| private IDictionary<object, object> services; | |
| internal ServiceLocator() { | |
| services = new Dictionary<object, object>(); | |
| this.services.Add(typeof(IQuerySurface), new CollectBruceLeeDB()); | |
| } | |
| public T GetService<T>() { | |
| try { | |
| return (T)services[typeof(T)]; | |
| } catch (KeyNotFoundException) { | |
| throw new ApplicationException("The requested service is not registered"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment