Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Threading.Tasks;
- #nullable enable
- namespace ConsoleApp3
- {
- class Program
- {
- static async Task Main(string[] args)
- {
- var projector = new PersonProjector(new DatabaseProjectionStore(new DbContextOptions<DatabaseProjectionStore>()));
- projector.Start();
- var p = new Person
- {
- FirstName = "Test",
- LastName = "User"
- };
- Console.WriteLine(p.GetLength());
- await foreach (var item in p.PeopleStream(end: 1))
- {
- Console.WriteLine(item);
- }
- }
- }
- public class User123 : EntityProjection
- {
- public string Owner { get; set; }
- }
- public class Person : EntityProjection
- {
- public string FirstName { get; set; }
- public string? MiddleName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- public static Person PersonFactory(string key) =>
- key switch
- {
- _ => new Person(),
- };
- public Person()
- {
- FirstName = "";
- LastName = "";
- Age = 0;
- }
- public int GetLength()
- {
- if (this is { MiddleName: { Length: var length } }) return length;
- return 0;
- }
- public async IAsyncEnumerable<int> PeopleStream(int start=0, int end = 100)
- {
- foreach (var item in Enumerable.Range(start,end))
- {
- var i = await GetNumberAsync(item);
- yield return i;
- }
- }
- private async Task<int> GetNumberAsync(int item)
- {
- await Task.Delay(1000);
- return item;
- }
- }
- public class DatabaseProjectionStore : DbContext, IProjectionStore
- {
- public DatabaseProjectionStore(DbContextOptions<DatabaseProjectionStore> options)
- : base(options)
- {
- //Database.EnsureDeleted();
- Database.EnsureCreated();
- this.Store(new Person() { FirstName = "Test", LastName = "User", MiddleName = "T", Age = 23 });
- this.Store(new User123() { Owner = "Owner user" });
- }
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- base.OnConfiguring(optionsBuilder);
- optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Projections;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- var assemblies = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
- .Where(x => typeof(EntityProjection).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
- .ToList();
- foreach (var type in assemblies)
- {
- modelBuilder
- .Entity(type, bt =>
- {
- var props = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly);
- foreach (var prop in props)
- {
- bt.Ignore(prop.Name);
- }
- bt.HasKey(new string[] { "AggregateId" });
- bt.Property<string>("JSON");
- }
- );
- }
- base.OnModelCreating(modelBuilder);
- }
- public IEntitySet<TEntity> GetEntitySet<TEntity>() where TEntity : EntityProjection
- {
- return new EntitySet<TEntity>(Set<TEntity>());
- }
- public Task<TEntity> Get<TKey, TEntity>(TKey key) where TEntity : EntityProjection
- {
- throw new NotImplementedException();
- }
- public async Task<TEntity> Get<TEntity>(Guid key) where TEntity : EntityProjection
- {
- return await FindAsync<TEntity>(new object[] { key });
- }
- public TEntity Store<TEntity>(TEntity value) where TEntity : EntityProjection
- {
- var dbset = this.Set<TEntity>();//this.Find(typeof(), new object[] { });
- var entry = Entry<TEntity>(value);
- entry.Property("JSON").CurrentValue = JObject.FromObject(value).ToString();
- //value.JSON = JObject.FromObject(value).ToString();
- //dbset.Add((TEntity)new EntityProjection() { AggregateId = value.AggregateId, JSON = , LastUpdated = DateTime.UtcNow });
- //dbset.Add(DatabaseEntityProjection.FromEntityProjection<TEntity>(value));
- dbset.Add(value);
- SaveChanges();
- return value;
- }
- }
- public interface IEntitySet<T>
- {
- List<T> ToList();
- }
- public class EntitySet<T> : IEntitySet<T> where T: EntityProjection
- {
- private DbSet<T> _entity;
- public EntitySet(DbSet<T> entity)
- {
- _entity = entity;
- }
- public List<T> ToList()
- {
- return _entity.Select(p => JObject.Parse(EF.Property<string>(p, "JSON")).ToObject<T>()).ToList();
- }
- }
- public class EntityProjection
- {
- public Guid AggregateId { get; set; } = Guid.NewGuid();
- public DateTime LastUpdated { get; set; }
- }
- public interface IProjectionStore
- {
- public IEntitySet<TEntity> GetEntitySet<TEntity>() where TEntity: EntityProjection;
- public Task<TEntity> Get<TKey, TEntity>(TKey key) where TEntity : EntityProjection;
- public Task<TEntity> Get<TEntity>(Guid key) where TEntity : EntityProjection;
- public TEntity Store<TEntity>(TEntity value) where TEntity : EntityProjection;
- }
- public abstract class Projector<T> where T: EntityProjection
- {
- private IProjectionStore _store;
- private T? Value = null;
- public Projector(IProjectionStore store)
- {
- _store = store;
- }
- public virtual void Start()
- {
- var users = _store.GetEntitySet<User123>().ToList();
- var people = _store.GetEntitySet<Person>().ToList();
- foreach (var user in users)
- {
- Console.WriteLine(user.AggregateId);
- }
- foreach (var user in people)
- {
- Console.WriteLine(user.AggregateId);
- }
- }
- }
- public class PersonProjector : Projector<Person>
- {
- public PersonProjector(IProjectionStore store) : base(store)
- {
- }
- }
- public interface ILifebookContainer { }
- public class ProjectorHosting
- {
- public static void Run(ILifebookContainer container)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement