-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathDummyDataExtensions.cs
More file actions
42 lines (35 loc) · 1.37 KB
/
Copy pathDummyDataExtensions.cs
File metadata and controls
42 lines (35 loc) · 1.37 KB
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
using LinkDotNet.Blog.Infrastructure.Persistence;
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace LinkDotNet.Blog.Web.Features.DummyData;
public static class DummyDataExtensions
{
/// <summary>
/// This will seed some blog post data and replace the connection to the real database with an in-memory database with dummy data.
/// Use this for testing or development purposes only.
/// </summary>
public static void UseDummyData(this IServiceCollection services, DummyDataOptions? options = null)
{
ArgumentNullException.ThrowIfNull(services);
var descriptors = services.Where(d => d.ServiceType == typeof(IRepository<>)).ToList();
foreach (var descriptor in descriptors)
{
services.Remove(descriptor);
}
var dummyDataOptions = options ?? new DummyDataOptions();
services.AddPooledDbContextFactory<BlogDbContext>(builder =>
{
builder.UseSqlite("DataSource=file::memory:?cache=shared")
#if DEBUG
.EnableDetailedErrors()
#endif
;
});
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddSingleton(dummyDataOptions);
services.AddHostedService<DummyDataSeeder>();
}
}