Skip to content

Commit a3f8f05

Browse files
Merge pull request #462 from johelvisguzman/issue441
Added a dependency resolver
2 parents 240f953 + 34c0271 commit a3f8f05

File tree

21 files changed

+560
-93
lines changed

21 files changed

+560
-93
lines changed

src/DotNetToolkit.Repository.EntityFramework/Internal/EfRepositoryContextFactory.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,24 @@ public IRepositoryContext Create()
8686

8787
#endregion
8888
}
89+
90+
/// <summary>
91+
/// An implementation of <see cref="IRepositoryContextFactory" /> with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
92+
/// </summary>
93+
/// <seealso cref="IRepositoryContextFactory" />
94+
internal class EfRepositoryContextFactory : IRepositoryContextFactory
95+
{
96+
#region Implementation of IRepositoryContextFactory
97+
98+
/// <summary>
99+
/// Create a new repository context.
100+
/// </summary>
101+
/// <returns>The new repository context.</returns>
102+
public IRepositoryContext Create()
103+
{
104+
return new EfRepositoryContext(RepositoryDependencyResolver.Current.Resolve<DbContext>());
105+
}
106+
107+
#endregion
108+
}
89109
}

src/DotNetToolkit.Repository.EntityFramework/RepositoryOptionsBuilderExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,19 @@ public static RepositoryOptionsBuilder UseEntityFramework<TDbContext>([NotNull]
5757

5858
return source;
5959
}
60+
61+
/// <summary>
62+
/// Configures the context to use entity framework with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
63+
/// </summary>
64+
/// <param name="source">The repository options builder.</param>
65+
/// <returns>The same builder instance.</returns>
66+
public static RepositoryOptionsBuilder UseEntityFramework([NotNull] this RepositoryOptionsBuilder source)
67+
{
68+
Guard.NotNull(source, nameof(source));
69+
70+
source.UseInternalContextFactory(new EfRepositoryContextFactory());
71+
72+
return source;
73+
}
6074
}
6175
}

src/DotNetToolkit.Repository.EntityFrameworkCore/Internal/EfCoreRepositoryContextFactory.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@ public IRepositoryContext Create()
6464

6565
#endregion
6666
}
67+
68+
/// <summary>
69+
/// An implementation of <see cref="IRepositoryContextFactory" /> with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
70+
/// </summary>
71+
/// <seealso cref="IRepositoryContextFactory" />
72+
internal class EfRepositoryContextFactory : IRepositoryContextFactory
73+
{
74+
#region Implementation of IRepositoryContextFactory
75+
76+
/// <summary>
77+
/// Create a new repository context.
78+
/// </summary>
79+
/// <returns>The new repository context.</returns>
80+
public IRepositoryContext Create()
81+
{
82+
return new EfCoreRepositoryContext(RepositoryDependencyResolver.Current.Resolve<DbContext>());
83+
}
84+
85+
#endregion
86+
}
6787
}

src/DotNetToolkit.Repository.EntityFrameworkCore/RepositoryOptionsBuilderExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,19 @@ public static RepositoryOptionsBuilder UseEntityFrameworkCore<TDbContext>([NotNu
5959

6060
return source;
6161
}
62+
63+
/// <summary>
64+
/// Configures the context to use entity framework with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="DbContext"/>.
65+
/// </summary>
66+
/// <param name="source">The repository options builder.</param>
67+
/// <returns>The same builder instance.</returns>
68+
public static RepositoryOptionsBuilder UseEntityFramework([NotNull] this RepositoryOptionsBuilder source)
69+
{
70+
Guard.NotNull(source, nameof(source));
71+
72+
source.UseInternalContextFactory(new EfRepositoryContextFactory());
73+
74+
return source;
75+
}
6276
}
6377
}

src/DotNetToolkit.Repository.Extensions.Microsoft.DependencyInjection/DotNetToolkit.Repository.Extensions.Microsoft.DependencyInjection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

src/DotNetToolkit.Repository.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public static IServiceCollection AddRepositories([NotNull] this IServiceCollecti
9696
}
9797

9898
// Register other services
99-
services.AddScoped<IRepositoryFactory, RepositoryFactory>(sp => new RepositoryFactory(sp.GetRequiredService<IRepositoryOptions>()));
100-
services.AddScoped<IUnitOfWork, UnitOfWork>(sp => new UnitOfWork(sp.GetRequiredService<IRepositoryOptions>()));
101-
services.AddScoped<IUnitOfWorkFactory, UnitOfWorkFactory>(sp => new UnitOfWorkFactory(sp.GetRequiredService<IRepositoryOptions>()));
99+
services.AddScoped<IRepositoryFactory, RepositoryFactory>(sp => new RepositoryFactory());
100+
services.AddScoped<IUnitOfWork, UnitOfWork>(sp => new UnitOfWork());
101+
services.AddScoped<IUnitOfWorkFactory, UnitOfWorkFactory>(sp => new UnitOfWorkFactory());
102102
services.AddScoped<IRepositoryOptions>(sp =>
103103
{
104104
var options = new RepositoryOptions(optionsBuilder.Options);
@@ -111,6 +111,14 @@ public static IServiceCollection AddRepositories([NotNull] this IServiceCollecti
111111
return options;
112112
});
113113

114+
// Register resolver
115+
RepositoryDependencyResolver.SetResolver(type =>
116+
{
117+
return services
118+
.BuildServiceProvider()
119+
.GetService(type);
120+
});
121+
114122
return services;
115123
}
116124

src/DotNetToolkit.Repository.Extensions.Unity/UnityContainerExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Configuration.Options.Internal;
66
using Factories;
77
using global::Unity;
8-
using global::Unity.Injection;
98
using JetBrains.Annotations;
109
using System;
1110
using System.Collections.Generic;
@@ -96,10 +95,10 @@ public static void RegisterRepositories([NotNull] this IUnityContainer container
9695
}
9796

9897
// Register other services
99-
container.RegisterType<IRepositoryFactory>(new InjectionFactory((c, t, n) => new RepositoryFactory(c.Resolve<IRepositoryOptions>())));
100-
container.RegisterType<IUnitOfWork>(new InjectionFactory((c, t, n) => new UnitOfWork(c.Resolve<IRepositoryOptions>())));
101-
container.RegisterType<IUnitOfWorkFactory>(new InjectionFactory((c, t, n) => new UnitOfWorkFactory(c.Resolve<IRepositoryOptions>())));
102-
container.RegisterType<IRepositoryOptions>(new InjectionFactory((c, t, n) =>
98+
container.RegisterFactory<IRepositoryFactory>(c => new RepositoryFactory());
99+
container.RegisterFactory<IUnitOfWork>(c => new UnitOfWork());
100+
container.RegisterFactory<IUnitOfWorkFactory>(c => new UnitOfWorkFactory());
101+
container.RegisterFactory<IRepositoryOptions>(c =>
103102
{
104103
var options = new RepositoryOptions(optionsBuilder.Options);
105104

@@ -109,7 +108,10 @@ public static void RegisterRepositories([NotNull] this IUnityContainer container
109108
}
110109

111110
return options;
112-
}));
111+
});
112+
113+
// Register resolver
114+
RepositoryDependencyResolver.SetResolver(type => container.Resolve(type));
113115
}
114116

115117
/// <summary>

src/DotNetToolkit.Repository/Factories/RepositoryFactory.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,62 @@ public RepositoryFactory([NotNull] Action<RepositoryOptionsBuilder> optionsActio
4040
/// <param name="options">The repository options.</param>
4141
public RepositoryFactory([NotNull] IRepositoryOptions options)
4242
{
43-
Guard.NotNull(options, nameof(options));
44-
45-
_options = options;
43+
_options = Guard.NotNull(options, nameof(options));
4644
}
4745

46+
/// <summary>
47+
/// Initializes a new instance of the <see cref="RepositoryFactory"/> class with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="IRepositoryOptions"/>.
48+
/// </summary>
49+
public RepositoryFactory() : this(RepositoryDependencyResolver.Current.Resolve<IRepositoryOptions>()) { }
50+
4851
#endregion
4952

5053
#region Implementation of IRepositoryFactory
5154

5255
/// <summary>
53-
/// Creates a new asynchronous repository for the specified entity type.
56+
/// Creates a new repository for the specified entity type.
5457
/// </summary>
5558
/// <typeparam name="TEntity">The type of the entity.</typeparam>
56-
/// <returns>The new asynchronous repository.</returns>
59+
/// <returns>The new repository.</returns>
5760
public IRepository<TEntity> Create<TEntity>() where TEntity : class
5861
{
59-
return CreateInstance<Repository<TEntity>>();
62+
return new Repository<TEntity>(_options);
6063
}
6164

6265
/// <summary>
63-
/// Creates a new asynchronous repository for the specified entity and primary key type.
66+
/// Creates a new repository for the specified entity and primary key type.
6467
/// </summary>
6568
/// <typeparam name="TEntity">The type of the entity.</typeparam>
6669
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
67-
/// <returns>The new asynchronous repository.</returns>
70+
/// <returns>The new repository.</returns>
6871
public IRepository<TEntity, TKey> Create<TEntity, TKey>() where TEntity : class
6972
{
70-
return CreateInstance<Repository<TEntity, TKey>>();
73+
return new Repository<TEntity, TKey>(_options);
7174
}
7275

7376
/// <summary>
74-
/// Creates a new asynchronous repository for the specified entity and a composite primary key type.
77+
/// Creates a new repository for the specified entity and a composite primary key type.
7578
/// </summary>
7679
/// <typeparam name="TEntity">The type of the entity.</typeparam>
7780
/// <typeparam name="TKey1">The type of the first part of the composite primary key.</typeparam>
7881
/// <typeparam name="TKey2">The type of the second part of the composite primary key.</typeparam>
79-
/// <returns>The new asynchronous repository.</returns>
82+
/// <returns>The new repository.</returns>
8083
public IRepository<TEntity, TKey1, TKey2> Create<TEntity, TKey1, TKey2>() where TEntity : class
8184
{
82-
return CreateInstance<Repository<TEntity, TKey1, TKey2>>();
85+
return new Repository<TEntity, TKey1, TKey2>(_options);
8386
}
8487

8588
/// <summary>
86-
/// Creates a new asynchronous repository for the specified entity and a composite primary key type.
89+
/// Creates a new repository for the specified entity and a composite primary key type.
8790
/// </summary>
8891
/// <typeparam name="TEntity">The type of the entity.</typeparam>
8992
/// <typeparam name="TKey1">The type of the first part of the composite primary key.</typeparam>
9093
/// <typeparam name="TKey2">The type of the second part of the composite primary key.</typeparam>
9194
/// <typeparam name="TKey3">The type of the third part of the composite primary key.</typeparam>
92-
/// <returns>The new asynchronous repository.</returns>
95+
/// <returns>The new repository.</returns>
9396
public IRepository<TEntity, TKey1, TKey2, TKey3> Create<TEntity, TKey1, TKey2, TKey3>() where TEntity : class
9497
{
95-
return CreateInstance<Repository<TEntity, TKey1, TKey2, TKey3>>();
98+
return new Repository<TEntity, TKey1, TKey2, TKey3>(_options);
9699
}
97100

98101
/// <summary>

src/DotNetToolkit.Repository/Factories/UnitOfWorkFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public UnitOfWorkFactory([NotNull] IRepositoryOptions options)
4444
_options = Guard.NotNull(options, nameof(options));
4545
}
4646

47+
/// <summary>
48+
/// Initializes a new instance of the <see cref="UnitOfWorkFactory"/> class with the <see cref="RepositoryDependencyResolver"/> using an IOC container to resolve the <see cref="IRepositoryOptions"/>.
49+
/// </summary>
50+
public UnitOfWorkFactory() : this(RepositoryDependencyResolver.Current.Resolve<IRepositoryOptions>()) { }
51+
4752
#endregion
4853

4954
#region Implementation of IUnitOfWorkFactory
@@ -54,7 +59,7 @@ public UnitOfWorkFactory([NotNull] IRepositoryOptions options)
5459
/// <returns>The new unit of work.</returns>
5560
public IUnitOfWork Create()
5661
{
57-
return CreateInstance<UnitOfWork>();
62+
return new UnitOfWork(_options);
5863
}
5964

6065
/// <summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace DotNetToolkit.Repository
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Represents a dependency resolver.
7+
/// </summary>
8+
public interface IRepositoryDependencyResolver
9+
{
10+
/// <summary>
11+
/// Gets an instance of the requested type with the given name from the container.
12+
/// </summary>
13+
/// <typeparam name="T">The type of the object to resolve.</typeparam>
14+
/// <returns>The resolved object.</returns>
15+
T Resolve<T>();
16+
17+
/// <summary>
18+
/// Gets an instance of the requested type with the given name from the container.
19+
/// </summary>
20+
/// <param name="type">The type of the object to resolve.</param>
21+
/// <returns>The resolved object.</returns>
22+
object Resolve(Type type);
23+
}
24+
}

0 commit comments

Comments
 (0)