-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added restful work related to conventions and so forth
- Loading branch information
1 parent
9ed8b38
commit 2e53cfb
Showing
91 changed files
with
2,312 additions
and
772 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"age": "0", | ||
"exclude": "^(Microsoft.AspNetCore.*|Microsoft.Extensions.*)" | ||
"exclude": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Sample.Command | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Bogus.Extensions; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Rocket.Surgery.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Rocket.Surgery.Conventions; | ||
using Rocket.Surgery.Conventions.DependencyInjection; | ||
using Sample.Core; | ||
using Sample.Core.Domain; | ||
|
||
[assembly: Convention(typeof(DataGenerationConvention))] | ||
[assembly: Convention(typeof(DataConvention))] | ||
|
||
namespace Sample.Core | ||
{ | ||
public class DataGenerationConvention : IServiceConvention | ||
{ | ||
|
||
public void Register(IServiceConventionContext context) | ||
{ | ||
context.Services.AddHostedService<HostedService>(); | ||
} | ||
|
||
class HostedService : IHostedService | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public HostedService(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await _serviceProvider.WithScoped<RocketDbContext>().Invoke( | ||
async dbContext => | ||
{ | ||
await dbContext.Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false); | ||
// await dbContext.Database.MigrateAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
).ConfigureAwait(false); | ||
await _serviceProvider.WithScoped<RocketDbContext>().Invoke( | ||
async dbContext => | ||
{ | ||
var rocketFaker = new RocketFaker(); | ||
var rockets = rocketFaker.GenerateBetween(10, 100); | ||
var launchFaker = new LaunchRecordFaker(rockets); | ||
|
||
var launches = launchFaker.GenerateBetween(100, 1000); | ||
|
||
await dbContext.Rockets.AddRangeAsync(rockets, cancellationToken).ConfigureAwait(false); | ||
await dbContext.LaunchRecords.AddRangeAsync(launches, cancellationToken).ConfigureAwait(false); | ||
await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
).ConfigureAwait(false); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} | ||
|
||
class DataConvention : IServiceConvention | ||
{ | ||
public void Register(IServiceConventionContext context) | ||
{ | ||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
connection.Open(); | ||
context.Services | ||
.AddDbContext<RocketDbContext>(x => x | ||
.EnableDetailedErrors() | ||
.EnableSensitiveDataLogging() | ||
.EnableServiceProviderCaching() | ||
.UseSqlite(connection) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Microsoft.EntityFrameworkCore.ValueGeneration; | ||
using NodaTime; | ||
using Rocket.Surgery.LaunchPad.EntityFramework; | ||
|
||
namespace Sample.Core.Domain | ||
{ | ||
public class LaunchRecord | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public string Partner { get; set; } = null!; | ||
public string Payload { get; set; } = null!; | ||
public long PayloadWeightKg { get; set; } | ||
public DateTimeOffset? ActualLaunchDate { get; set; } | ||
public DateTimeOffset ScheduledLaunchDate { get; set; } | ||
|
||
public Guid RocketId { get; set; } | ||
public ReadyRocket ReadyRocket { get; set; } = null!; | ||
|
||
class Configure : ConfigureEntityType<LaunchRecord> | ||
{ | ||
protected override void OnEntityCreating(DbContext context, ModelBuilder modelBuilder, EntityTypeBuilder<LaunchRecord> builder) | ||
{ | ||
builder.HasKey(x => x.Id); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Rocket.Surgery.LaunchPad.EntityFramework; | ||
|
||
namespace Sample.Core.Domain | ||
{ | ||
public class ReadyRocket | ||
{ | ||
public Guid Id { get; set; } | ||
public string SerialNumber { get; set; } = null!; | ||
public RocketType Type { get; set; } | ||
|
||
public IEnumerable<LaunchRecord> LaunchRecords { get; set; } | ||
|
||
class Configure : ConfigureEntityType<ReadyRocket> | ||
{ | ||
protected override void OnEntityCreating(DbContext context, ModelBuilder modelBuilder, EntityTypeBuilder<ReadyRocket> builder) | ||
{ | ||
builder.HasKey(x => x.Id); | ||
builder.ToTable("Rockets"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
using Rocket.Surgery.LaunchPad.EntityFramework; | ||
|
||
namespace Sample.Core.Domain | ||
{ | ||
public class RocketDbContext : LpContext<RocketDbContext> | ||
{ | ||
public RocketDbContext( | ||
DbContextOptions<RocketDbContext> options, | ||
[NotNull] [ItemNotNull] IEnumerable<IOnConfiguringDbContext> configurationHandlers, | ||
[NotNull] [ItemNotNull] IEnumerable<IOnModelCreating> modelCreationHandlers | ||
) : base(options, configurationHandlers, modelCreationHandlers) { } | ||
|
||
public DbSet<ReadyRocket> Rockets { get; set; } = null!; | ||
public DbSet<LaunchRecord> LaunchRecords { get; set; } = null!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Sample.Core.Domain | ||
{ | ||
public enum RocketType | ||
{ | ||
Falcon9, | ||
FalconHeavy, | ||
AtlasV | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Bogus; | ||
using Bogus.Extensions; | ||
using Sample.Core.Domain; | ||
|
||
namespace Sample.Core | ||
{ | ||
class LaunchRecordFaker : Faker<LaunchRecord> | ||
{ | ||
public LaunchRecordFaker(List<Sample.Core.Domain.ReadyRocket> rockets) | ||
{ | ||
RuleFor(x => x.Id, x => x.Random.Guid()); | ||
RuleFor(x => x.Partner, x => x.Company.CompanyName()); | ||
RuleFor(x => x.ReadyRocket, x => x.PickRandom(rockets.AsEnumerable())); | ||
RuleFor(x => x.RocketId, (f, v) => v.ReadyRocket.Id); | ||
RuleFor(x => x.ActualLaunchDate, f => f.Date.PastOffset().OrNull(f, 0.2f)); | ||
RuleFor(x => x.ScheduledLaunchDate, (f, v) => f.Date.PastOffset(refDate: v.ActualLaunchDate)); | ||
RuleFor(x => x.PayloadWeightKg, f => f.Random.Number(100, 1000000)); | ||
RuleFor(x => x.Payload, f => f.Lorem.Paragraphs(3)); | ||
} | ||
} | ||
} |
Oops, something went wrong.