Пример #1
0
        private static void AddDbQueryForEntitiesInDomainAssembly(this IServiceCollection services)
        {
            var domainAssembly = typeof(Entity).Assembly;

            // Inject SoftDeleteDbQuery for entities that implement softdelete interface
            ClassFinder
            .SearchInAssembly(domainAssembly)
            .ClassesThatImplementInterface(typeof(IQueryableEntity))
            .ClassesThatImplementInterface(typeof(ISoftDeleteEntity))
            .ForEach(type =>
            {
                var interfaceType      = typeof(IDbQuery <>).MakeGenericType(new[] { type });
                var implementationType = typeof(SoftDeleteDbQuery <>).MakeGenericType(new[] { type });
                services.AddTransient(interfaceType, implementationType);
            });

            // Inject DbQuery for entities that do not implement softdelete interface
            ClassFinder
            .SearchInAssembly(domainAssembly)
            .ClassesThatImplementInterface(typeof(IQueryableEntity))
            .ClassesThatDoNotImplementInterface(typeof(ISoftDeleteEntity))
            .ForEach(type =>
            {
                var interfaceType      = typeof(IDbQuery <>).MakeGenericType(new[] { type });
                var implementationType = typeof(Persistence.DbQuery <>).MakeGenericType(new[] { type });
                services.AddTransient(interfaceType, implementationType);
            });
        }
 private static void RegisterDomainEntities(this IServiceCollection services)
 {
     ClassFinder
     .SearchInAssembly(Assembly.GetExecutingAssembly())
     .ClassesThatImplementInterface(typeof(IDomainEntity <>))
     .ForEach(type =>
     {
         var nonGenericInterfaceType = type.GetInterfaces()
                                       .Where(i => !i.GetTypeInfo().IsGenericType)
                                       .Where(i => i.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDomainEntity <>)))
                                       .FirstOrDefault();
         services.AddTransient(nonGenericInterfaceType, type);
     });
 }
        private static void RegisterDomainEntityHooks(this IServiceCollection services)
        {
            var interfaces = new[]
            {
                typeof(IBeforeCreate <>),
                typeof(IBeforeUpdate <>),
                typeof(IBeforeDelete <>),
                typeof(IAfterCreate <>),
                typeof(IAfterUpdate <>),
                typeof(IAfterDelete <>),
                typeof(IValidator <>),
                typeof(IDefaultValuesSetter <>)
            };

            foreach (var @interface in interfaces)
            {
                ClassFinder
                .SearchInAssembly(Assembly.GetExecutingAssembly())
                .ClassesThatImplementInterface(@interface)
                .ForEach(type =>
                {
                    if (type.IsGenericTypeDefinition)     // like LoggingHook<T>
                    {
                        services.AddTransient(@interface, type);
                    }
                    else
                    {
                        var interfaceType = type.GetInterfaces()
                                            .Where(i => i.GetTypeInfo().IsGenericType)
                                            .Where(i => i.GetGenericTypeDefinition() == @interface)
                                            .FirstOrDefault();
                        services.AddTransient(interfaceType, type);
                    }
                });
            }
        }