示例#1
0
        /// <summary>
        /// Injects the default logger into the fabric construction.
        /// </summary>
        /// <param name="construction">The construction</param>
        /// <returns>The construction for chaining</returns>
        public static FabricConstruction AddLocalLogger(this FabricConstruction construction)
        {
            // Add logging as default
            construction.ServiceCollection.AddLogging(options =>
            {
                // Default to debug level
                options.SetMinimumLevel(LogLevel.Debug);

                // Setup loggers from configuration
                options.AddConfiguration(construction.Configuration.GetSection("Logging"));

                // Add console logger
                options.AddConsole();

                // Add debug logger
                options.AddDebug();
            });

            // Adds a default logger so that we can get a non-generic ILogger
            // that will have the category name of "Fabric"
            construction.ServiceCollection.AddTransient(provider => provider?.GetService <ILoggerFactory>()?.CreateLogger("Fabric"));

            // Chain the construction
            return(construction);
        }
        /// <summary>
        /// Bind implementations of all data service into the fabric construction
        /// </summary>
        /// <param name="construction"></param>
        /// <returns></returns>
        public static FabricConstruction AddBotDataService(this FabricConstruction construction)
        {
            // Inject our SQLite EF data store
            construction.ServiceCollection
            .AddDbContext <BotDbContext>
            (
                options => options.UseSqlite(construction.Configuration.GetConnectionString("BotConnection"))
            );

            // Add client data store for easy access/use of the backing data store
            construction.ServiceCollection
            .AddScoped <IBotDataService>
            (
                provider => new BotDataService(provider.GetService <BotDbContext>())
            );

            // Bind Read Data Service
            construction.ServiceCollection
            .AddScoped <IReadDataService>
            (
                provider => new ReadDataService(provider.GetService <BotDbContext>())
            );

            // Bind Write Data Service
            construction.ServiceCollection
            .AddScoped <IWriteDataService>
            (
                provider => new WriteDataService(provider.GetService <BotDbContext>())
            );

            // Return construction for chaining.
            return(construction);
        }
示例#3
0
        /// <summary>
        /// Injects an xml logger into the fabric construction
        /// </summary>
        /// <param name="construction">The calling fabric construction to be chained</param>
        /// <param name="configurator">The configuration setting for the file logger, null to use default configuration</param>
        /// <returns>The fabric construction for chaining</returns>
        public static FabricConstruction AddXmlLogger(this FabricConstruction construction, Configurator configurator = null)
        {
            // Make use of default AddLogging extension that comes with .NET Core's dependency injection
            construction.ServiceCollection.AddLogging(builder => builder?.AddXml(configurator));

            // Chain the construction
            return(construction);
        }
示例#4
0
        /// <summary>
        /// The initial call to setting up and using Fabric
        /// </summary>
        /// <typeparam name="T">The type of the construction to use</typeparam>
        /// <returns>Construction for chaining</returns>
        public static FabricConstruction Construct <T>()
            where T : FabricConstruction, new()
        {
            Construction = new T();

            // Return construction for chaining
            return(Construction);
        }
示例#5
0
        /// <summary>
        /// Inject the default exception handler into the fabric construction
        /// </summary>
        /// <param name="construction">The fabric construction</param>
        /// <returns>The Fabric construction for chaining</returns>
        public static FabricConstruction AddLocalExceptionHandler(this FabricConstruction construction)
        {
            // Bind a static instance of the ExceptionHandler
            construction.ServiceCollection.AddSingleton <IExceptionHandler>(new ExceptionHandler());

            // Chain the construction
            return(construction);
        }
示例#6
0
        /// <summary>
        /// The initial call to setting up and using Fabric from a hosted environment
        /// </summary>
        /// <typeparam name="T">The type of the construction to use</typeparam>
        /// <param name="constructionInstance">The instance of the construction to use</param>
        /// <returns>Construction for chaining</returns>
        public static FabricConstruction Construct <T>(T constructionInstance)
            where T : FabricConstruction
        {
            // Set construction
            Construction = constructionInstance;

            // Return construction for chaining
            return(Construction);
        }
示例#7
0
        /// <summary>
        /// Should be called once a Fabric Construction is finished and we want to build
        /// it and start the application.
        /// </summary>
        /// <param name="construction"><inheritdoc cref="FabricConstruction"/></param>
        /// <param name="shouldLog">Indicates whether the fabric started message should be logged</param>
        public static void Build(this FabricConstruction construction, bool shouldLog = true)
        {
            // Build the service provider
            construction.Build();

            // Log the startup complete
            if (shouldLog)
            {
                Logger.LogCriticalSource($"Fabric has started in{FabricEnvironment.Configuration}...");
            }
        }
示例#8
0
        /// <summary>
        /// Injects all of the default services used by Fabric for a quicker and
        /// cleaner setup
        /// </summary>
        /// <param name="construction">The fabric construction</param>
        /// <returns>The fabric construction for further chaining</returns>
        public static FabricConstruction AddLocalServices(this FabricConstruction construction)
        {
            // Add exception handler
            construction.AddLocalExceptionHandler();

            // Add default logger
            construction.AddLocalLogger();

            // Chain the construction
            return(construction);
        }
示例#9
0
        /// <summary>
        /// Configures a fabric construction using the provided configuration
        /// </summary>
        /// <param name="construction">The fabric construction</param>
        /// <param name="configuration">The configuration from dependency injection</param>
        /// <returns>The fabric construction</returns>
        public static FabricConstruction AddConfiguration(this FabricConstruction construction,
                                                          IConfiguration configuration)
        {
            // Add specific configuration
            construction.UseConfiguration(configuration);

            // Add configuration to services
            construction.ServiceCollection.AddSingleton(configuration);

            // Chain the construction
            return(construction);
        }
示例#10
0
        public static FabricConstruction AddBotServices(this FabricConstruction construction)
        {
            // Bind user manager
            construction.ServiceCollection.AddTransient <IUserManager, UserManager>();

            // Bind group manager
            construction.ServiceCollection.AddTransient <IGroupManager, GroupManager>();

            // Bind message handler
            construction.ServiceCollection.AddTransient <IMessageHandler, MessageHandler>();

            // Bind update service
            construction.ServiceCollection.AddTransient <IUpdateService, UpdateService>();

            // Bind bot service
            construction.ServiceCollection.AddSingleton <IBotService, BotService>();

            return(construction);
        }
示例#11
0
        /// <summary>
        /// Bind implementations of all data service into the fabric construction
        /// </summary>
        /// <param name="construction"></param>
        /// <returns></returns>
        public static FabricConstruction AddDataService(this FabricConstruction construction)
        {
            // Inject our SQLite EF data store
            construction.ServiceCollection
            .AddDbContext <ClassContext>
            (
                options => options.UseSqlite(construction.Configuration.GetConnectionString("ClassConnection"))
            );

            // Add client data store for easy access/use of the backing data store
            construction.ServiceCollection
            .AddScoped <IDataService>
            (
                provider => new DataServices(provider.GetService <ClassContext>())
            );

            // Bind Read Person data Service
            construction.ServiceCollection
            .AddScoped <IReadService <Person> >
            (
                provider => new PersonReadService(provider.GetService <ClassContext>())
            );

            // Bind Read Group data Service
            construction.ServiceCollection
            .AddScoped <IReadService <Group> >
            (
                provider => new GroupReadService(provider.GetService <ClassContext>())
            );

            // Bind Write Data Service
            construction.ServiceCollection
            .AddScoped <IWriteService>
            (
                provider => new WriteService(provider.GetService <ClassContext>())
            );

            // Return construction for chaining.
            return(construction);
        }
示例#12
0
        /// <summary>
        /// Configures the fabric construction in the default way
        /// </summary>
        /// <param name="construction">The construction to configure</param>
        /// <param name="configure">The custom configuration action</param>
        /// <returns>Fabric construction for chaining</returns>
        public static FabricConstruction AddLocalConfiguration(this FabricConstruction construction,
                                                               Action <IConfigurationBuilder> configure = null)
        {
            // Create configuration source
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()

                                                         // Add environment variables
                                                         .AddEnvironmentVariables();

            // If we are not on a mobile platform...
            if (!construction.Environment.IsMobile)
            {
                // Add file based configuration.

                // Set base path for JSON files as the startup location of the application
                configurationBuilder.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                // Add application settings json files
                configurationBuilder.AddJsonFile("appsettings.json", true, true);
                configurationBuilder.AddJsonFile($"appsettings.{construction.Environment.Configuration}.json", true, true);
            }

            // Let custom configuration happen
            configure?.Invoke(configurationBuilder);

            // Inject configuration into services
            IConfiguration configuration = configurationBuilder.Build();

            construction.ServiceCollection.AddSingleton(configuration);

            // Set the construction configuration
            construction.UseConfiguration(configuration);

            // Chain the construction
            return(construction);
        }