示例#1
0
        /// <summary>
        /// Configures the connection to use the <see cref="ILoggerFactory"/> passed in.
        /// </summary>
        /// <param name="builder">Builder to configure.</param>
        /// <param name="factory">Factory used to resolve loggers.</param>
        /// <returns>The builder instance so calls can be chained.</returns>
        public static LinqToDBConnectionOptionsBuilder UseLoggerFactory(this LinqToDBConnectionOptionsBuilder builder,
                                                                        ILoggerFactory factory)
        {
            var adapter = new LinqToDBLoggerFactoryAdapter(factory);

            return(builder.WithTraceLevel(TraceLevel.Verbose).WriteTraceWith(adapter.OnTrace));
        }
示例#2
0
        /// <summary>
        /// Configures the connection to use the <see cref="ILoggerFactory"/> resolved from the container.
        /// </summary>
        /// <param name="builder">Builder to configure.</param>
        /// <param name="provider">Container used to resolve the factory.</param>
        /// <returns>The builder instance so calls can be chained.</returns>
        public static LinqToDBConnectionOptionsBuilder UseDefaultLogging(this LinqToDBConnectionOptionsBuilder builder,
                                                                         IServiceProvider provider)
        {
            var factory = provider.GetRequiredService <ILoggerFactory>();

            return(UseLoggerFactory(builder, factory));
        }
示例#3
0
        /// <summary>
        ///     Registers <typeparamref name="TContext"/> as a service in the <see cref="IServiceCollection" />.
        ///     You use this method when using dependency injection in your application, such as with ASP.NET.
        ///     For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
        /// </summary>
        /// <example>
        ///     <code>
        ///           public void ConfigureServices(IServiceCollection services)
        ///           {
        ///               var connectionString = "connection string to database";
        ///
        ///               services.AddLinqToDBContext&lt;IMyContext, MyContext&gt;(options => {
        ///                   options.UseSqlServer(connectionString);
        ///               });
        ///           }
        ///       </code>
        /// </example>
        /// <typeparam name="TContext">
        ///     The class or interface that will be used to resolve the context from the container.
        /// </typeparam>
        /// <typeparam name="TContextImplementation">
        ///		The concrete implementation type used to fulfill requests for <typeparamref name="TContext"/> from the container.
        ///     Must inherit from <see cref="IDataContext"/> and <typeparamref name="TContext"/>
        ///     and expose a constructor that takes <see cref="LinqToDBConnectionOptions{TContext}" /> (where T is <typeparamref name="TContextImplementation"/>)
        ///     and passes it to the base constructor of <see cref="DataConnection" />.
        /// </typeparam>
        /// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
        /// <param name="configure">
        ///     <para>
        ///         An action to configure the <see cref="LinqToDBConnectionOptionsBuilder" /> for the context.
        ///     </para>
        ///     <para>
        ///         In order for the options to be passed into your context, you need to expose a constructor on your context that takes
        ///         <see cref="LinqToDBConnectionOptions{TContext}" /> and passes it to the base constructor of <see cref="DataConnection" />.
        ///     </para>
        /// </param>
        /// <param name="lifetime">
        ///     The lifetime with which to register the Context service in the container.
        ///     For one connection per request use <see cref="ServiceLifetime.Scoped"/> (the default).
        /// </param>
        /// <remarks>
        ///     This method should be used when a custom context is required or
        ///     when multiple contexts with different configurations are required.
        /// </remarks>
        /// <returns>
        ///     The same service collection so that multiple calls can be chained.
        /// </returns>
        public static IServiceCollection AddLinqToDBContext <TContext, TContextImplementation>(
            this IServiceCollection serviceCollection,
            Action <IServiceProvider, LinqToDBConnectionOptionsBuilder> configure,
            ServiceLifetime lifetime = ServiceLifetime.Scoped) where TContextImplementation : TContext, IDataContext
        {
            var hasTypedConstructor = HasTypedContextConstructor <TContextImplementation>();

            serviceCollection.TryAdd(new ServiceDescriptor(typeof(TContext), typeof(TContextImplementation), lifetime));
            serviceCollection.TryAdd(new ServiceDescriptor(typeof(LinqToDBConnectionOptions <TContextImplementation>),
                                                           provider =>
            {
                var builder = new LinqToDBConnectionOptionsBuilder();
                configure(provider, builder);
                return(builder.Build <TContextImplementation>());
            },
                                                           lifetime));

            if (!hasTypedConstructor)
            {
                serviceCollection.TryAdd(new ServiceDescriptor(typeof(LinqToDBConnectionOptions),
                                                               provider => provider.GetRequiredService(typeof(LinqToDBConnectionOptions <TContextImplementation>)), lifetime));
            }

            return(serviceCollection);
        }
示例#4
0
        public void TraceSwitchShouldUseFromBuilder()
        {
            var staticTraceLevel  = DataConnection.TraceSwitch.Level;
            var builderTraceLevel = staticTraceLevel + 1;
            var builder           = new LinqToDBConnectionOptionsBuilder().WithTraceLevel(builderTraceLevel);

            using (var db = new DataConnection(builder.Build()))
            {
                Assert.AreEqual(builderTraceLevel, db.TraceSwitchConnection.Level);
                Assert.AreNotEqual(staticTraceLevel, db.TraceSwitchConnection.Level);
            }
        }
示例#5
0
        public void WriteTraceInstanceShouldUseFromBuilder()
        {
            var staticWriteCalled = false;

            DataConnection.WriteTraceLine = (s, s1, arg3) => staticWriteCalled = true;

            var builderWriteCalled = false;
            var builder            = new LinqToDBConnectionOptionsBuilder()
                                     .WriteTraceWith((s, s1, a3) => builderWriteCalled = true);

            using (var db = new DataConnection(builder.Build()))
            {
                db.WriteTraceLineConnection(null, null, TraceLevel.Info);
            }

            Assert.True(builderWriteCalled, "because the data connection should have used the action from the builder");
            Assert.False(staticWriteCalled, "because the data connection should have used the action from the builder");
        }
示例#6
0
        public void OnTraceConnectionShouldUseFromBuilder()
        {
            bool defaultTraceCalled = false;

#pragma warning disable CS0618 // Type or member is obsolete
            DataConnection.OnTrace = info => defaultTraceCalled = true;
#pragma warning restore CS0618 // Type or member is obsolete

            bool builderTraceCalled = false;
            var  builder            = new LinqToDBConnectionOptionsBuilder().WithTracing(info => builderTraceCalled = true);

            using (var db = new DataConnection(builder.Build()))
            {
                db.OnTraceConnection(new TraceInfo(db, TraceInfoStep.BeforeExecute, TraceOperation.BuildMapping, false));
            }

            Assert.True(builderTraceCalled, "because the builder trace should have been called");
            Assert.False(defaultTraceCalled, "because the static trace should not have been called");
        }