/
Module.cs
126 lines (112 loc) · 6.1 KB
/
Module.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VirtoCommerce.CartModule.Core;
using VirtoCommerce.CartModule.Core.Events;
using VirtoCommerce.CartModule.Core.Model;
using VirtoCommerce.CartModule.Core.Services;
using VirtoCommerce.CartModule.Data.BackgroundJobs;
using VirtoCommerce.CartModule.Data.Handlers;
using VirtoCommerce.CartModule.Data.MySql;
using VirtoCommerce.CartModule.Data.PostgreSql;
using VirtoCommerce.CartModule.Data.Repositories;
using VirtoCommerce.CartModule.Data.Services;
using VirtoCommerce.CartModule.Data.SqlServer;
using VirtoCommerce.Platform.Core.Bus;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.DynamicProperties;
using VirtoCommerce.Platform.Core.Modularity;
using VirtoCommerce.Platform.Core.Security;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.Platform.Data.Extensions;
using VirtoCommerce.Platform.Hangfire;
using VirtoCommerce.Platform.Hangfire.Extensions;
namespace VirtoCommerce.CartModule.Web
{
public class Module : IModule, IHasConfiguration
{
public ManifestModuleInfo ModuleInfo { get; set; }
public IConfiguration Configuration { get; set; }
public void Initialize(IServiceCollection serviceCollection)
{
var databaseProvider = Configuration.GetValue("DatabaseProvider", "SqlServer");
serviceCollection.AddDbContext<CartDbContext>(options =>
{
var connectionString = Configuration.GetConnectionString(ModuleInfo.Id) ?? Configuration.GetConnectionString("VirtoCommerce");
switch (databaseProvider)
{
case "MySql":
options.UseMySqlDatabase(connectionString);
break;
case "PostgreSql":
options.UsePostgreSqlDatabase(connectionString);
break;
default:
options.UseSqlServerDatabase(connectionString);
break;
}
});
switch (databaseProvider)
{
case "MySql":
serviceCollection.AddTransient<ICartRawDatabaseCommand, MySqlCartRawDatabaseCommand>();
break;
case "PostgreSql":
serviceCollection.AddTransient<ICartRawDatabaseCommand, PostgreSqlCartRawDatabaseCommand>();
break;
default:
serviceCollection.AddTransient<ICartRawDatabaseCommand, SqlServerCartRawDatabaseCommand>();
break;
}
serviceCollection.AddTransient<ICartRepository, CartRepository>();
serviceCollection.AddTransient<Func<ICartRepository>>(provider => () => provider.CreateScope().ServiceProvider.GetRequiredService<ICartRepository>());
serviceCollection.AddTransient<IShoppingCartService, ShoppingCartService>();
serviceCollection.AddTransient<IShoppingCartSearchService, ShoppingCartSearchService>();
serviceCollection.AddTransient<IShoppingCartTotalsCalculator, DefaultShoppingCartTotalsCalculator>();
serviceCollection.AddTransient<IShoppingCartBuilder, ShoppingCartBuilder>();
serviceCollection.AddTransient<IWishlistService, WishlistService>();
serviceCollection.AddTransient<IDeleteObsoleteCartsHandler, DeleteObsoleteCartsHandler>();
serviceCollection.AddTransient<CartChangedEventHandler>();
}
public void PostInitialize(IApplicationBuilder appBuilder)
{
var serviceProvider = appBuilder.ApplicationServices;
var permissionsRegistrar = serviceProvider.GetRequiredService<IPermissionsRegistrar>();
permissionsRegistrar.RegisterPermissions(ModuleInfo.Id, "Cart", ModuleConstants.Security.Permissions.AllPermissions);
var dynamicPropertyRegistrar = serviceProvider.GetRequiredService<IDynamicPropertyRegistrar>();
dynamicPropertyRegistrar.RegisterType<LineItem>();
dynamicPropertyRegistrar.RegisterType<Payment>();
dynamicPropertyRegistrar.RegisterType<Shipment>();
dynamicPropertyRegistrar.RegisterType<ShoppingCart>();
var settingsRegistrar = serviceProvider.GetRequiredService<ISettingsRegistrar>();
settingsRegistrar.RegisterSettings(ModuleConstants.Settings.General.AllSettings, ModuleInfo.Id);
var recurringJobManager = serviceProvider.GetService<IRecurringJobManager>();
var settingsManager = serviceProvider.GetService<ISettingsManager>();
recurringJobManager.WatchJobSetting(
settingsManager,
new SettingCronJobBuilder()
.SetEnablerSetting(ModuleConstants.Settings.General.EnableDeleteObsoleteCarts)
.SetCronSetting(ModuleConstants.Settings.General.CronDeleteObsoleteCarts)
.ToJob<DeleteObsoleteCartsJob>(x => x.Process())
.Build());
var handlerRegistrar = serviceProvider.GetService<IHandlerRegistrar>();
handlerRegistrar.RegisterHandler<CartChangedEvent>(async (message, _) => await serviceProvider.GetService<CartChangedEventHandler>().Handle(message));
handlerRegistrar.RegisterHandler<CartChangeEvent>(async (message, _) => await serviceProvider.GetService<CartChangedEventHandler>().Handle(message));
using var serviceScope = serviceProvider.CreateScope();
using var dbContext = serviceScope.ServiceProvider.GetRequiredService<CartDbContext>();
var databaseProvider = Configuration.GetValue("DatabaseProvider", "SqlServer");
if (databaseProvider == "SqlServer")
{
dbContext.Database.MigrateIfNotApplied(MigrationName.GetUpdateV2MigrationName(ModuleInfo.Id));
}
dbContext.Database.Migrate();
}
public void Uninstall()
{
// Method intentionally left empty.
}
}
}