Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8.  
  9. #nullable enable
  10. namespace ConsoleApp3
  11. {
  12. class Program
  13. {
  14. static async Task Main(string[] args)
  15. {
  16.  
  17. var projector = new PersonProjector(new DatabaseProjectionStore(new DbContextOptions<DatabaseProjectionStore>()));
  18. projector.Start();
  19.  
  20. var p = new Person
  21. {
  22. FirstName = "Test",
  23. LastName = "User"
  24. };
  25.  
  26. Console.WriteLine(p.GetLength());
  27. await foreach (var item in p.PeopleStream(end: 1))
  28. {
  29. Console.WriteLine(item);
  30. }
  31. }
  32. }
  33.  
  34. public class User123 : EntityProjection
  35. {
  36. public string Owner { get; set; }
  37. }
  38.  
  39. public class Person : EntityProjection
  40. {
  41. public string FirstName { get; set; }
  42. public string? MiddleName { get; set; }
  43. public string LastName { get; set; }
  44. public int Age { get; set; }
  45.  
  46. public static Person PersonFactory(string key) =>
  47. key switch
  48. {
  49. _ => new Person(),
  50. };
  51.  
  52. public Person()
  53. {
  54. FirstName = "";
  55. LastName = "";
  56. Age = 0;
  57. }
  58.  
  59.  
  60. public int GetLength()
  61. {
  62. if (this is { MiddleName: { Length: var length } }) return length;
  63. return 0;
  64. }
  65.  
  66. public async IAsyncEnumerable<int> PeopleStream(int start=0, int end = 100)
  67. {
  68. foreach (var item in Enumerable.Range(start,end))
  69. {
  70. var i = await GetNumberAsync(item);
  71. yield return i;
  72. }
  73. }
  74.  
  75. private async Task<int> GetNumberAsync(int item)
  76. {
  77. await Task.Delay(1000);
  78. return item;
  79. }
  80. }
  81.  
  82. public class DatabaseProjectionStore : DbContext, IProjectionStore
  83. {
  84. public DatabaseProjectionStore(DbContextOptions<DatabaseProjectionStore> options)
  85. : base(options)
  86. {
  87. //Database.EnsureDeleted();
  88. Database.EnsureCreated();
  89. this.Store(new Person() { FirstName = "Test", LastName = "User", MiddleName = "T", Age = 23 });
  90. this.Store(new User123() { Owner = "Owner user" });
  91. }
  92.  
  93. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  94. {
  95. base.OnConfiguring(optionsBuilder);
  96. optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Projections;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
  97. }
  98.  
  99. protected override void OnModelCreating(ModelBuilder modelBuilder)
  100. {
  101. var assemblies = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
  102. .Where(x => typeof(EntityProjection).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
  103. .ToList();
  104. foreach (var type in assemblies)
  105. {
  106. modelBuilder
  107. .Entity(type, bt =>
  108. {
  109. var props = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly);
  110. foreach (var prop in props)
  111. {
  112. bt.Ignore(prop.Name);
  113. }
  114. bt.HasKey(new string[] { "AggregateId" });
  115. bt.Property<string>("JSON");
  116. }
  117. );
  118. }
  119.  
  120. base.OnModelCreating(modelBuilder);
  121. }
  122.  
  123. public IEntitySet<TEntity> GetEntitySet<TEntity>() where TEntity : EntityProjection
  124. {
  125. return new EntitySet<TEntity>(Set<TEntity>());
  126. }
  127.  
  128. public Task<TEntity> Get<TKey, TEntity>(TKey key) where TEntity : EntityProjection
  129. {
  130. throw new NotImplementedException();
  131. }
  132.  
  133. public async Task<TEntity> Get<TEntity>(Guid key) where TEntity : EntityProjection
  134. {
  135. return await FindAsync<TEntity>(new object[] { key });
  136. }
  137.  
  138. public TEntity Store<TEntity>(TEntity value) where TEntity : EntityProjection
  139. {
  140. var dbset = this.Set<TEntity>();//this.Find(typeof(), new object[] { });
  141. var entry = Entry<TEntity>(value);
  142. entry.Property("JSON").CurrentValue = JObject.FromObject(value).ToString();
  143. //value.JSON = JObject.FromObject(value).ToString();
  144. //dbset.Add((TEntity)new EntityProjection() { AggregateId = value.AggregateId, JSON = , LastUpdated = DateTime.UtcNow });
  145. //dbset.Add(DatabaseEntityProjection.FromEntityProjection<TEntity>(value));
  146. dbset.Add(value);
  147. SaveChanges();
  148. return value;
  149. }
  150. }
  151.  
  152. public interface IEntitySet<T>
  153. {
  154.  
  155. List<T> ToList();
  156. }
  157.  
  158. public class EntitySet<T> : IEntitySet<T> where T: EntityProjection
  159. {
  160. private DbSet<T> _entity;
  161.  
  162. public EntitySet(DbSet<T> entity)
  163. {
  164. _entity = entity;
  165. }
  166.  
  167. public List<T> ToList()
  168. {
  169. return _entity.Select(p => JObject.Parse(EF.Property<string>(p, "JSON")).ToObject<T>()).ToList();
  170. }
  171. }
  172.  
  173. public class EntityProjection
  174. {
  175. public Guid AggregateId { get; set; } = Guid.NewGuid();
  176. public DateTime LastUpdated { get; set; }
  177. }
  178.  
  179. public interface IProjectionStore
  180. {
  181. public IEntitySet<TEntity> GetEntitySet<TEntity>() where TEntity: EntityProjection;
  182. public Task<TEntity> Get<TKey, TEntity>(TKey key) where TEntity : EntityProjection;
  183. public Task<TEntity> Get<TEntity>(Guid key) where TEntity : EntityProjection;
  184. public TEntity Store<TEntity>(TEntity value) where TEntity : EntityProjection;
  185. }
  186.  
  187. public abstract class Projector<T> where T: EntityProjection
  188. {
  189. private IProjectionStore _store;
  190. private T? Value = null;
  191.  
  192. public Projector(IProjectionStore store)
  193. {
  194. _store = store;
  195. }
  196.  
  197. public virtual void Start()
  198. {
  199. var users = _store.GetEntitySet<User123>().ToList();
  200. var people = _store.GetEntitySet<Person>().ToList();
  201. foreach (var user in users)
  202. {
  203. Console.WriteLine(user.AggregateId);
  204. }
  205. foreach (var user in people)
  206. {
  207. Console.WriteLine(user.AggregateId);
  208. }
  209. }
  210.  
  211. }
  212.  
  213. public class PersonProjector : Projector<Person>
  214. {
  215. public PersonProjector(IProjectionStore store) : base(store)
  216. {
  217. }
  218. }
  219. public interface ILifebookContainer { }
  220.  
  221. public class ProjectorHosting
  222. {
  223. public static void Run(ILifebookContainer container)
  224. {
  225.  
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement