public IEnumerable <Order> GetOrders() { Console.WriteLine("Get Orders"); var user = Thread.CurrentPrincipal.Identity.Name; var orders = cache.Get(user); if (orders == null) { Console.WriteLine("From DB"); using (var dbContext = new Northwind()) { dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false; orders = dbContext.Orders.ToList(); cache.Set(user, orders, policy); } } return(orders); }
/// <summary> /// Get all mapped properties of type T. (Properties with the DbField attribute) /// Returns a list of tuples with the following: /// ( Database Field Name, Reflected Property, If the property is a nullable type ) /// </summary> /// <typeparam name="T">Data Type</typeparam> /// <returns>List of DbMappings</returns> private static List <DbMapping> GetDatabaseProperties <T>() { var type = typeof(T); var cachedObject = cache.Get(type.FullName); if (cachedObject != null) { return(cachedObject); } var properties = type.GetProperties(); var result = new List <DbMapping>(); foreach (var property in properties) { var attrib = property.GetCustomAttribute <DbFieldAttribute>(true); if (attrib != null) { var isNullable = Nullable.GetUnderlyingType(property.PropertyType) != null || !property.PropertyType.IsValueType; var mapping = new DbMapping() { DatabaseField = attrib.Field ?? property.Name, ObjectProperty = property, IsNullable = isNullable }; result.Add(mapping); } } cache.Set(type.FullName, result); return(result); }