-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
94 lines (72 loc) · 3.07 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Core;
using Core.Domain;
using Core.ImportExport;
using Core.Redis;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Service.Cach;
using Service.Domain;
using Service.ImportExport;
namespace KaveNegarTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Application Started");
var config = ConfigurationBiulder();
var serviceProdvider = BuildDependencyInjection(config);
Console.WriteLine("App is Ready");
var stream = File.Open("Files/TempExcel.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Console.WriteLine("Excel File Streamed");
var importManagerService = serviceProdvider.GetService<IImportManager>();
var setRedisKeys = importManagerService?.ImportProductsToRedisFromXlsx(stream);
if (setRedisKeys == null || !setRedisKeys.Any())
{
Console.WriteLine("No Data Inserted To Redis");
Console.ReadLine();
}
Console.WriteLine("Excel Imported to Redis");
var distributedCachManager = serviceProdvider.GetService<IDistributedCachManager>();
var products = new List<Product>();
foreach (var redisKey in setRedisKeys)
{
var product = distributedCachManager?.GetKey<Product>(redisKey);
products.Add(product);
}
Console.WriteLine("All products Loaded From Redis");
var productService = serviceProdvider.GetService<IProductService>();
productService?.InsertProducts(products);
Console.WriteLine("All Products Inserted To DataBase");
Console.WriteLine("Work Done");
Console.ReadKey();
}
private static IConfiguration ConfigurationBiulder()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
var config = builder.Build();
return config;
}
private static IServiceProvider BuildDependencyInjection(IConfiguration config)
{
var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(config)
.AddTransient<IImportManager, ImportManager>()
.AddTransient<IProductService, ProductService>()
.AddTransient<IRedisConnectionWrapper, RedisConnectionWrapper>()
.AddTransient<IDistributedCachManager, RedisCachManager>()
.AddTransient(typeof(IRepository<>), typeof(EntityRepository<>))
.AddDbContext<BaseDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("ConnectionStrings")))
.BuildServiceProvider();
return serviceProvider;
}
}
}