1+ // Copyright (C) 2021 Xtensive LLC.
2+ // This code is distributed under MIT license terms.
3+ // See the License.txt file in the project root for more information.
4+
5+ using System ;
6+ using Microsoft . AspNetCore . Builder ;
7+ using Microsoft . Extensions . DependencyInjection ;
8+ using Microsoft . AspNetCore . Mvc . Filters ;
9+ using Xtensive . Orm . Web . Filters ;
10+ using Xtensive . Orm . Web . Middleware ;
11+
12+ namespace Xtensive . Orm . Web
13+ {
14+ /// <summary>
15+ /// Contains extensions which are used in Startup.cs to configure application
16+ /// </summary>
17+ public static class StartupConfigurationExtensions
18+ {
19+ /// <summary>
20+ /// Adds <see cref="OpenSessionMiddleware"/> to ASP.NET Core middleware pipeline.
21+ /// </summary>
22+ /// <param name="builder"><see cref="IApplicationBuilder"/> instance.</param>
23+ /// <returns><paramref name="builder"/> with <see cref="OpenSessionMiddleware"/>.</returns>
24+ public static IApplicationBuilder UseDataObjectsSessionOpener ( this IApplicationBuilder builder ) =>
25+ builder . UseMiddleware < OpenSessionMiddleware > ( ) ;
26+
27+ /// <summary>
28+ /// Registers a <see cref="SessionAccessor"/> as scoped service to be able to
29+ /// have access to session/transaction as parameter in either MVC controllers' actions or
30+ /// constuctors, and in Razor Pages.
31+ /// </summary>
32+ /// <param name="services">A service collection to add <see cref="SessionAccessor"/>.</param>
33+ /// <returns>The same collection instance with registered accessor.</returns>
34+ public static IServiceCollection AddDataObjectsSessionAccessor ( this IServiceCollection services ) =>
35+ services . AddScoped < SessionAccessor > ( ) ;
36+
37+ /// <summary>
38+ /// Adds <see cref="SessionActionFilter"/> to action filters.
39+ /// </summary>
40+ /// <param name="filters">The filter collection to add the filter.</param>
41+ /// <returns>A <see cref="IFilterMetadata"/> representing added filter.</returns>
42+ public static IFilterMetadata AddDataObjectsSessionActionFilter ( this FilterCollection filters ) =>
43+ AddDataObjectsSessionActionFilter ( filters , 0 ) ;
44+
45+ /// <summary>
46+ /// Adds <see cref="SessionActionFilter"/> to action filters.
47+ /// </summary>
48+ /// <param name="filters">The filter collection to add the filter.</param>
49+ /// <param name="order">The order of the added filter.</param>
50+ /// <returns>A <see cref="IFilterMetadata"/> representing added filter.</returns>
51+ public static IFilterMetadata AddDataObjectsSessionActionFilter ( this FilterCollection filters , int order ) =>
52+ filters . Add ( typeof ( SessionActionFilter ) , order ) ;
53+ }
54+ }
0 commit comments