/// <summary> /// Add repository /// </summary> /// <param name="register"></param> /// <typeparam name="TService"></typeparam> /// <returns></returns> public static void RegisterRepository <TService>(this DependencyProxyRegister register) where TService : class, IRepository { var reflector = RepositoryReflector.Create <TService>(); RepositoryManager.Instance.Register(reflector); register.AddScoped <TService>(); }
/// <summary> /// Add repository /// </summary> /// <param name="register"></param> /// <typeparam name="TRegister"></typeparam> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImplement"></typeparam> /// <returns></returns> public static TRegister RegisterRepository <TRegister, TService, TImplement>(this TRegister register) where TRegister : DependencyProxyRegister where TService : IRepository where TImplement : class, TService { var reflector = RepositoryReflector.Create <TService, TImplement>(); RepositoryManager.Instance.Register(reflector); register.AddScoped <TService, TImplement>(); return(register); }
static void Main(string[] args) { var services = new ServiceCollection(); services.AddScoped <ITraceIdMaker, DefaultTraceIdMaker>(); services.AddScoped <TraceIdAccessor>(); services.AddSingleton(RepositoryManager.Instance); services.AddScoped <ScopedRepositoryManager>(); services.AddScoped <ITransactionManager, ScopedTransactionManager>(); services.AddScoped <UnitOfWorkManager>(); services.AddScoped <DbContext>(); var reflector = RepositoryReflector.Create <IDbRepository>(); RepositoryManager.Instance.Register(reflector); services.AddScoped(reflector.ServiceType, reflector.ImplementType); //services.ConfigureDynamicProxy(c=>c.) var provider = services.BuildDynamicProxyProvider(); using (var scope = provider.CreateScope()) { var scopedProvider = scope.ServiceProvider; Console.WriteLine("Start to load repository"); // get repository var repository = scopedProvider.Resolve <IDbRepository>(); Console.WriteLine("Load repository finished."); Console.WriteLine("Start to get trace id."); var id = repository.CurrentTraceId; Console.WriteLine("Got trace id."); Console.WriteLine(id); Console.WriteLine("Start to load RawTypedContext"); Console.WriteLine(repository.RawTypedContext222 is null ? "CTX NULL" : "CTX NOT NULL"); Console.WriteLine("Load RawTypedContext finished."); Console.WriteLine(repository.UnitOfWork is null ? "UOW NULL" : "UOW NOT NULL"); // returns ok } Console.WriteLine("Hello World!"); }