Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. [Serializable]
  2. public class Module
  3. {
  4. [Key]
  5. public int Id { get; set; }
  6. public string ModuleName { get; set; }
  7. public string FontAwesomeClass { get; set; }
  8. }
  9.  
  10. [Serializable]
  11. public class ModulosPorUsuario
  12. {
  13. [Key]
  14. public int Id { get; set; }
  15. public string Email { get; set; }
  16. public virtual ICollection<Module> Modules{ get; set; }
  17. }
  18.  
  19. /// <summary>
  20. /// Gets the modules activated for a user
  21. /// </summary>
  22. /// <param name="email">Email address of the user</param>
  23. /// <returns>List of modules for the selected user</returns>
  24. public static List<Models.ModulosPorUsuario> GetModulesForUser(string identityname)
  25. {
  26. /// It needs to be cached for every user because every user can have different modules enabled.
  27. var cachekeyname = "ApplicationModulesPerUser|" + identityname;
  28.  
  29. IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
  30. List<Models.ModulosPorUsuario> listOfModulesPerUser = new List<Models.ModulosPorUsuario>();
  31. listOfModulesPerUser = (List<Models.ModulosPorUsuario>)cache.Get(cachekeyname);
  32. if (listOfModulesPerUser == null)
  33. {
  34. listOfModulesPerUser = dbApp.ModulosPorUsuario.Where(p => p.Email == identityname).ToList();
  35. cache.Set(cachekeyname, listOfModulesPerUser, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
  36. return listOfModulesPerUser;
  37. }
  38. else
  39. {
  40. return listOfModulesPerUser;
  41. }
  42. }
  43.  
  44. public static class SampleStackExchangeRedisExtensions
  45. {
  46. public static T Get<T>(this IDatabase cache, string key)
  47. {
  48. return Deserialize<T>(cache.StringGet(key));
  49. }
  50.  
  51. public static object Get(this IDatabase cache, string key)
  52. {
  53. return Deserialize<object>(cache.StringGet(key));
  54. }
  55.  
  56. public static void Set(this IDatabase cache, string key, object value, TimeSpan expiration)
  57. {
  58. cache.StringSet(key, Serialize(value), expiration);
  59. }
  60.  
  61. static byte[] Serialize(object o)
  62. {
  63. if (o == null)
  64. {
  65. return null;
  66. }
  67. BinaryFormatter binaryFormatter = new BinaryFormatter();
  68. using (MemoryStream memoryStream = new MemoryStream())
  69. {
  70. binaryFormatter.Serialize(memoryStream, o);
  71. byte[] objectDataAsStream = memoryStream.ToArray();
  72. return objectDataAsStream;
  73. }
  74. }
  75.  
  76. static T Deserialize<T>(byte[] stream)
  77. {
  78. BinaryFormatter binaryFormatter = new BinaryFormatter();
  79. if (stream == null)
  80. return default(T);
  81.  
  82. using (MemoryStream memoryStream = new MemoryStream(stream))
  83. {
  84. T result = (T)binaryFormatter.Deserialize(memoryStream);
  85. return result;
  86. }
  87. }
  88. }
  89.  
  90. #region Seed Modules
  91. var module1 = new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" };
  92. var module2 = new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" };
  93. var module3 = new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" };
  94. var module4 = new Module() { Id = 4, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
  95. var module5 = new Module() { Id = 5, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
  96. var module6 = new Module() { Id = 6, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" };
  97. var module7 = new Module() { Id = 7, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" };
  98. var module8 = new Module() { Id = 8, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" };
  99. context.Modulos.Add(module1);
  100. context.Modulos.Add(module2);
  101. context.Modulos.Add(module3);
  102. context.Modulos.Add(module4);
  103. context.Modulos.Add(module5);
  104. context.Modulos.Add(module6);
  105. context.Modulos.Add(module7);
  106. context.Modulos.Add(module8);
  107.  
  108. context.SaveChanges();
  109. #endregion
  110.  
  111. #region Seed ModulosPor Usuario
  112. context.ModulosPorUsuario.Add(new ModulosPorUsuario()
  113. {
  114. Id=1,
  115. Email = "companyadmin@mysaasapp.onmicrosoft.com",
  116. Modules = new List<Module>() { module1, module2 }
  117. });
  118.  
  119. context.ModulosPorUsuario.Add(new ModulosPorUsuario()
  120. {
  121. Id=2,
  122. Email = "accountingadmin@mysaasapp.onmicrosoft.com",
  123. Modules = new List<Module>() { module3, module5 }
  124. });
  125.  
  126. context.ModulosPorUsuario.Add(new ModulosPorUsuario()
  127. {
  128. Id=3,
  129. Email = "jayhamlin@mysaasapp.onmicrosoft.com",
  130. Modules = new List<Module>() { module4, module6 }
  131. });
  132.  
  133. context.ModulosPorUsuario.Add(new ModulosPorUsuario()
  134. {
  135. Id=4,
  136. Email = "usuario1@mysaasapp.onmicrosoft.com",
  137. Modules = new List<Module>() { module7, module7 }
  138. });
  139.  
  140.  
  141. context.SaveChanges();
  142. #endregion
  143.  
  144. Additional information: Unable to find assembly 'EntityFrameworkDynamicProxies-Inspinia_MVC5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
  145.  
  146. at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
  147. at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
  148. at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
  149. at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
  150. at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement