Skip to content

Commit

Permalink
Dependency Injection
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovav committed Oct 13, 2020
1 parent f8253d1 commit 21287ba
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ Call PrepareTrackInfo() method before SaveChanges()

}
```

5. Register in Dependency Injection.
Below is sample code how You can use DI.

```csharp
using EfCoreDataChange;

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(
RuntimeDBContextExtention<MyDBContext>.RuntimeContextType,
options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"));
}
```
2 changes: 1 addition & 1 deletion src/EfCoreDataChange/EfCoreDataChange.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>EfCoreDataChange</PackageId>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Description>Entity Framework Core Extention for save and retrive information about adding, updating and deleting data changes with last modified timestamp.</Description>
<Authors>Aleksandr Kolesnikov</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down
60 changes: 60 additions & 0 deletions src/EfCoreDataChange/Extentions/IServiceCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace EfCoreDataChange
{
/// <summary>
/// Extentions of IServiceCollection
/// </summary>
public static class ServiceExtentions
{
/// <summary>
/// Adds DbContext to IServiceCollection instance
/// <param name="services">The <see cref="IServiceCollection"/> Instance of you IServiceCollection to add DbContext.</param>
/// <param name="dbContextType">Type of DbContext</param>
/// <param name="options">DbContext options</param>
/// <param name="serviceLifetime">Lifetime of DbContext Default = Scoped</param>
/// <param name="optionsLifeTime">Lifetime of DbContext options Default = Scoped</param>
/// </summary>
public static void AddDbContext(this IServiceCollection services,
Type dbContextType, Action<DbContextOptionsBuilder> options,
ServiceLifetime serviceLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifeTime = ServiceLifetime.Scoped)
{
var actType = typeof(Action<DbContextOptionsBuilder>);
typeof(EntityFrameworkServiceCollectionExtensions).GetMethods(BindingFlags.Static| BindingFlags.Public)
.Where(v => v.Name == "AddDbContext" && v.IsGenericMethod && v.GetGenericArguments().Count() == 1)
.Where(v => v.GetParameters().Count() == 4)
.Where(v => v.GetParameters().Where(p => p.ParameterType == actType).Count() == 1)
.First()
.MakeGenericMethod(new Type[] {dbContextType})
.Invoke(null, new object[] {
services, options, serviceLifetime, optionsLifeTime
});
}
/// <summary>
/// Adds DbContext to IServiceCollection instance
/// <param name="services">The <see cref="IServiceCollection"/> Instance of you IServiceCollection to add DbContext.</param>
/// <param name="dbContextType">Type of DbContext</param>
/// <param name="serviceLifetime">Lifetime of DbContext Default = Scoped</param>
/// <param name="optionsLifeTime">Lifetime of DbContext options Default = Scoped</param>
/// </summary>
public static void AddDbContext(this IServiceCollection services,
Type dbContextType,
ServiceLifetime serviceLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifeTime = ServiceLifetime.Scoped)
{
typeof(EntityFrameworkServiceCollectionExtensions).GetMethods(BindingFlags.Static| BindingFlags.Public)
.Where(v => v.Name == "AddDbContext" && v.IsGenericMethod && v.GetGenericArguments().Count() == 1)
.Where(v => v.GetParameters().Count() == 3)
.First()
.MakeGenericMethod(new Type[] {dbContextType})
.Invoke(null, new object[] {
services, serviceLifetime, optionsLifeTime
});
}
}
}
7 changes: 7 additions & 0 deletions src/EfCoreDataChange/RuntimeDBContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public static class RuntimeDBContextExtention<T> where T : DbContext, IDisposabl
private static readonly Dictionary<Type, EntityPropsForTransfer> _dTrackKeys = new Dictionary<Type, EntityPropsForTransfer>();

private static Type _dbContextType;
/// <summary>
/// Runtime DbContext Type with track entities
/// </summary>
public static Type RuntimeContextType
{
get => RuntimeContext.GetType();
}

/// <summary>
/// Runtime DbContext Type instance with track entities
Expand Down
24 changes: 24 additions & 0 deletions test/DataChangeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;
using EfCoreDataChange;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace test
Expand Down Expand Up @@ -35,7 +36,30 @@ public void DataChangeTest1()
// db.SaveChangesWithTrackInfo();
// Assert.Equal(1,1);
}
}

[Fact]
public void DITest()
{
IServiceCollection services = new ServiceCollection();
services.AddDbContext(typeof(TestDBContext), options => {

});
var servContext = services.First(v => v.ServiceType == typeof(TestDBContext));
var servContextOption = services.First(v => v.ServiceType == typeof(DbContextOptions<TestDBContext>));
Assert.NotNull(servContext);
Assert.NotNull(servContextOption);

IServiceCollection services2 = new ServiceCollection();
services2.AddDbContext(typeof(TestDBContext));
var servContext2 = services2.First(v => v.ServiceType == typeof(TestDBContext));
Assert.NotNull(servContext2);

IServiceCollection services3 = new ServiceCollection();
var t = RuntimeDBContextExtention<TestDBContext>.RuntimeContextType;
services3.AddDbContext(t);
var servContext3 = services3.First(v => v.ServiceType == t);
Assert.NotNull(servContext3);
}
}
}
5 changes: 5 additions & 0 deletions test/TestDBContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Dog>().HasKey(v => new {v.Name, v.Age});
modelBuilder.CreateDataChangeTracking(this.GetType());
}
public TestDBContext(DbContextOptions<TestDBContext> options)
:base(options)
{}
public TestDBContext()
{}
}

public class Cat
Expand Down

0 comments on commit 21287ba

Please sign in to comment.