public EventStore(IEventPublisher publisher, CQRSCode.ReadModel.Repository.MongoOptions mongoOptions, IList <EventType> events) { _publisher = publisher; if (!BsonClassMap.IsClassMapRegistered(@events[0].Type)) { // Insuffisant car quand les entités sont enregistrées en tant que IEvent, seuls les champs de l'interface sont persistés // BsonClassMap.RegisterClassMap<ProductCreated>(cm => { cm.AutoMap(); cm.unmap } ); // BsonClassMap.RegisterClassMap<OfferCreated>(); // BsonClassMap.RegisterClassMap<OfferStockSet>(); // BsonClassMap.RegisterClassMap<EventBase>(cm => { // cm.MapIdProperty(eb => eb._id).SetIdGenerator(MongoDB.Bson.Serialization.IdGenerators.BsonObjectIdGenerator.Instance); // }); // Impossible d'utiliser automap et de définir un autre champ ID que .Id // BsonClassMap.RegisterClassMap<OfferCreated>(cm => // { // cm.MapProperty(o => o.Id); // Nécessaire de mapper en tant que simple propriété afin que ce champ ne soit pas défini comme l'ID du document (_id) dans Mongo // cm.AutoMap(); // //cm.GetMemberMapForElement("_id").SetIdGenerator(MongoDB.Bson.Serialization.IdGenerators.BsonObjectIdGenerator.Instance); // }); foreach (var @event in events) { BsonClassMap m = new BsonClassMap(@event.Type); @event.Type.GetProperties().ToList().ForEach(p => m.MapProperty(p.Name)); BsonClassMap.RegisterClassMap(m); } } _client = new MongoClient(mongoOptions.ConnectionString); _database = _client.GetDatabase(mongoOptions.Database); _collection = _database.GetCollection <IEvent>("events"); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); services.AddMemoryCache(); //Add Cqrs services services.AddSingleton <InProcessBus>(new InProcessBus()); services.AddSingleton <ICommandSender>(y => y.GetService <InProcessBus>()); services.AddSingleton <IEventPublisher>(y => y.GetService <InProcessBus>()); services.AddSingleton <IHandlerRegistrar>(y => y.GetService <InProcessBus>()); services.AddScoped <ISession, Session>(); //Factory to turn scanned IEvents into Type services.AddSingleton <IEventStore, EventStore> (sProvider => new EventStore( sProvider.GetRequiredService <IEventPublisher>(), sProvider.GetRequiredService <CQRSCode.ReadModel.Repository.MongoOptions>(), sProvider.GetServices <EventType>().ToList())); services.AddScoped <ICache, CQRSlite.Cache.MemoryCache>(); services.AddScoped <IRepository>(y => new CacheRepository(new Repository(y.GetService <IEventStore>()), y.GetService <IEventStore>(), y.GetService <ICache>())); // REFACTORING REQUIRED: DIDN'T MANAGE TO PARSE JSON SETTINGS USING Configuration.GetSection("mongo").Get // var config = Configuration.GetSection("configuredClients"). //services.Configure<CQRSCode.ReadModel.Repository.MongoOptions>(Configuration.GetSection("mongo")); services.AddTransient <CQRSCode.ReadModel.Repository.MongoOptions>(sp => { var mOpt = new CQRSCode.ReadModel.Repository.MongoOptions(); mOpt.ConnectionString = Configuration["mongo:connectionString"]; mOpt.Database = Configuration["mongo:database"]; return(mOpt); }); // services.Configure<CQRSCode.ReadModel.Repository.MongoOptions>(options => Configuration.GetSection("mongo").Get<CQRSCode.ReadModel.Repository.MongoOptions>()); services.AddTransient( typeof(CQRSCode.ReadModel.Repository.IRepository <>), typeof(CQRSCode.ReadModel.Repository.Repository <>) ); //Scan for commandhandlers and eventhandlers services.Scan(scan => scan .FromAssemblies(typeof(CategoryCommandHandlers).GetTypeInfo().Assembly) .AddClasses(classes => classes.Where(x => { var allInterfaces = x.GetInterfaces(); return (allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IHandler <>)) || allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICancellableHandler <>))); })) .AsSelf() .WithTransientLifetime() ); //Register tous les IEvent // services.Scan(scan => scan // .FromAssemblies(typeof(CQRSCode.ReadModel.Events.ProductCreated).GetTypeInfo().Assembly) // .AddClasses(classes => classes.Where(x => { // var allInterfaces = x.GetInterfaces(); // return // allInterfaces.Any(y => y == typeof(IEvent)); // })) // .As(type => EventType<type>) // //.AsImplementedInterfaces() // .WithTransientLifetime() // ); typeof(CQRSCode.ReadModel.Events.ProductCreated) .GetTypeInfo().Assembly.ExportedTypes .ToList().ForEach(type => { if (type.GetInterfaces().Any(iType => iType == typeof(IEvent))) { services.AddTransient <EventType, EventType>(sProvider => new EventType(type)); } }); // foreach (var service in services) // { // System.Console.WriteLine(service.ServiceType.Name + " " + service.ServiceType.AssemblyQualifiedName); // } //Register bus var serviceProvider = services.BuildServiceProvider(); var registrar = new BusRegistrar(new DependencyResolver(serviceProvider)); registrar.Register(typeof(CategoryCommandHandlers)); // Add framework services. services.AddMvc(); // services.AddAutoMapper(typeof(Startup)); var config = new AutoMapper.MapperConfiguration(cfg => { cfg.AddProfile <MappingProfile>(); }); var mapper = config.CreateMapper(); services.AddSingleton <IMapper>(mapper); }