Skip to content

Commit 3727ed4

Browse files
Merge pull request #602 from johelvisguzman/GH-601
GH-601 Allow IOC extension projects to scan for additional types
2 parents 9175003 + 9deb7b8 commit 3727ed4

File tree

7 files changed

+83
-90
lines changed

7 files changed

+83
-90
lines changed

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
using Configuration.Options.Internal;
77
using global::Microsoft.Extensions.DependencyInjection;
88
using JetBrains.Annotations;
9-
using Services;
109
using System;
11-
using System.Collections.Generic;
1210
using System.Linq;
1311
using System.Reflection;
14-
using Transactions;
1512
using Utility;
1613

1714
/// <summary>
@@ -75,6 +72,7 @@ public static IServiceCollection AddRepositories([NotNull] this IServiceCollecti
7572

7673
var scanResults = AssemblyScanner.FindRepositoriesFromAssemblies(assembliesToScan);
7774

75+
// Register scanned types
7876
scanResults.ForEach(scanResult =>
7977
{
8078
foreach (var implementationType in scanResult.ImplementationTypes)
@@ -93,27 +91,7 @@ public static IServiceCollection AddRepositories([NotNull] this IServiceCollecti
9391
}
9492
});
9593

96-
// Register other services
97-
services.Add(new ServiceDescriptor(
98-
typeof(IRepositoryFactory),
99-
sp => new RepositoryFactory(sp.GetRequiredService<IRepositoryOptions>()),
100-
serviceLifetime));
101-
102-
services.Add(new ServiceDescriptor(
103-
typeof(IUnitOfWork),
104-
sp => new UnitOfWork(sp.GetRequiredService<IRepositoryOptions>()),
105-
serviceLifetime));
106-
107-
services.Add(new ServiceDescriptor(
108-
typeof(IUnitOfWorkFactory),
109-
sp => new UnitOfWorkFactory(sp.GetRequiredService<IRepositoryOptions>()),
110-
serviceLifetime));
111-
112-
services.Add(new ServiceDescriptor(
113-
typeof(IServiceFactory),
114-
sp => new ServiceFactory(sp.GetRequiredService<IRepositoryOptions>()),
115-
serviceLifetime));
116-
94+
// Register options services
11795
services.Add(new ServiceDescriptor(
11896
typeof(IRepositoryOptions),
11997
sp =>

src/DotNetToolkit.Repository.Extensions.Ninject/KernelExtensions.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,17 @@ public static void BindRepositories([NotNull] this IKernel kernel, [NotNull] Act
6868

6969
var scanResults = AssemblyScanner.FindRepositoriesFromAssemblies(assembliesToScan);
7070

71+
// Binds scanned types
7172
scanResults.ForEach(scanResult =>
7273
{
7374
foreach (var implementationType in scanResult.ImplementationTypes)
7475
{
75-
if (scanResult.InterfaceType == typeof(IRepositoryInterceptor))
76-
{
77-
kernel.Bind(implementationType).ToSelf();
78-
kernel.Bind(scanResult.InterfaceType).To(implementationType);
79-
}
80-
else
81-
{
82-
kernel.Bind(scanResult.InterfaceType).To(implementationType);
83-
}
76+
kernel.Bind(implementationType).ToSelf();
77+
kernel.Bind(scanResult.InterfaceType).To(implementationType);
8478
}
8579
});
8680

87-
// Binds other services
88-
kernel.Bind<IRepositoryFactory>().ToMethod(c => new RepositoryFactory(c.Kernel.Get<IRepositoryOptions>()));
89-
kernel.Bind<IUnitOfWork>().ToMethod(c => new UnitOfWork(c.Kernel.Get<IRepositoryOptions>()));
90-
kernel.Bind<IUnitOfWorkFactory>().ToMethod(c => new UnitOfWorkFactory(c.Kernel.Get<IRepositoryOptions>()));
91-
kernel.Bind<IServiceFactory>().ToMethod(c => new ServiceFactory(c.Kernel.Get<IUnitOfWorkFactory>()));
81+
// Binds options services
9282
kernel.Bind<IRepositoryOptions>().ToMethod(c =>
9383
{
9484
var options = new RepositoryOptions(optionsBuilder.Options);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ public static void RegisterRepositories([NotNull] this IUnityContainer container
7575

7676
var scanResults = AssemblyScanner.FindRepositoriesFromAssemblies(assembliesToScan);
7777

78+
// Register scanned types
7879
scanResults.ForEach(scanResult =>
7980
{
8081
foreach (var implementationType in scanResult.ImplementationTypes)
8182
{
83+
container.RegisterType(implementationType);
84+
8285
if (scanResult.InterfaceType == typeof(IRepositoryInterceptor))
8386
{
84-
container.RegisterType(implementationType, implementationType);
8587
container.RegisterType(scanResult.InterfaceType, implementationType, implementationType.FullName);
8688
}
8789
else
@@ -91,11 +93,7 @@ public static void RegisterRepositories([NotNull] this IUnityContainer container
9193
}
9294
});
9395

94-
// Register other services
95-
container.RegisterFactory<IRepositoryFactory>(c => new RepositoryFactory(c.Resolve<IRepositoryOptions>()), factorylifetimeManager);
96-
container.RegisterFactory<IUnitOfWork>(c => new UnitOfWork(c.Resolve<IRepositoryOptions>()), factorylifetimeManager);
97-
container.RegisterFactory<IUnitOfWorkFactory>(c => new UnitOfWorkFactory(c.Resolve<IRepositoryOptions>()), factorylifetimeManager);
98-
container.RegisterFactory<IServiceFactory>(c => new ServiceFactory(c.Resolve<IUnitOfWorkFactory>()), factorylifetimeManager);
96+
// Register options services
9997
container.RegisterFactory<IRepositoryOptions>(c =>
10098
{
10199
var options = new RepositoryOptions(optionsBuilder.Options);

src/DotNetToolkit.Repository/Services/ServiceFactory.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace DotNetToolkit.Repository.Services
22
{
3-
using Configuration.Options;
43
using JetBrains.Annotations;
54
using System;
65
using Transactions;
@@ -20,18 +19,6 @@ public class ServiceFactory : IServiceFactory
2019

2120
#region Constructors
2221

23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="ServiceFactory" /> class.
25-
/// </summary>
26-
/// <param name="optionsAction">A builder action used to create or modify options for this service factory.</param>
27-
public ServiceFactory([NotNull] Action<RepositoryOptionsBuilder> optionsAction) : this(new UnitOfWorkFactory(Guard.NotNull(optionsAction, nameof(optionsAction)))) { }
28-
29-
/// <summary>
30-
/// Initializes a new instance of the <see cref="ServiceFactory" /> class.
31-
/// </summary>
32-
/// <param name="options">The service options.</param>
33-
public ServiceFactory([NotNull] IRepositoryOptions options) : this(new UnitOfWorkFactory(Guard.NotNull(options, nameof(options)))) { }
34-
3522
/// <summary>
3623
/// Initializes a new instance of the <see cref="ServiceFactory"/> class.
3724
/// </summary>

src/DotNetToolkit.Repository/Transactions/IUnitOfWork.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,49 @@
66
/// Maintains business objects in-memory which have been changed (inserted, updated, or deleted) in a single transaction.
77
/// Once the transaction is completed, the changes will be sent to the database in one single unit of work.
88
/// </summary>
9-
/// <seealso cref="IRepositoryFactory" />
109
/// <seealso cref="System.IDisposable" />
11-
public interface IUnitOfWork : IRepositoryFactory, IDisposable
10+
public interface IUnitOfWork : IDisposable
1211
{
12+
/// <summary>
13+
/// Creates a new repository for the specified entity type.
14+
/// </summary>
15+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
16+
/// <returns>The new repository.</returns>
17+
IRepository<TEntity> Create<TEntity>() where TEntity : class;
18+
19+
/// <summary>
20+
/// Creates a new repository for the specified entity and primary key type.
21+
/// </summary>
22+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
23+
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
24+
/// <returns>The new repository.</returns>
25+
IRepository<TEntity, TKey> Create<TEntity, TKey>() where TEntity : class;
26+
27+
/// <summary>
28+
/// Creates a new repository for the specified entity and a composite primary key type.
29+
/// </summary>
30+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
31+
/// <typeparam name="TKey1">The type of the first part of the composite primary key.</typeparam>
32+
/// <typeparam name="TKey2">The type of the second part of the composite primary key.</typeparam>
33+
/// <returns>The new repository.</returns>
34+
IRepository<TEntity, TKey1, TKey2> Create<TEntity, TKey1, TKey2>() where TEntity : class;
35+
36+
/// <summary>
37+
/// Creates a new repository for the specified entity and a composite primary key type.
38+
/// </summary>
39+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
40+
/// <typeparam name="TKey1">The type of the first part of the composite primary key.</typeparam>
41+
/// <typeparam name="TKey2">The type of the second part of the composite primary key.</typeparam>
42+
/// <typeparam name="TKey3">The type of the third part of the composite primary key.</typeparam>
43+
/// <returns>The new repository.</returns>
44+
IRepository<TEntity, TKey1, TKey2, TKey3> Create<TEntity, TKey1, TKey2, TKey3>() where TEntity : class;
45+
46+
/// <summary>
47+
/// Creates a new repository for the specified type.
48+
/// </summary>
49+
/// <returns>The new repository.</returns>
50+
T CreateInstance<T>() where T : class;
51+
1352
/// <summary>
1453
/// Commits all changes made in this unit of work.
1554
/// </summary>

src/DotNetToolkit.Repository/Transactions/UnitOfWork.cs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,6 @@ private void Initialize([NotNull] IRepositoryOptions options)
9494

9595
#region Implementation of IUnitOfWork
9696

97-
/// <summary>
98-
/// Commits all changes made in this unit of work.
99-
/// </summary>
100-
public virtual void Commit()
101-
{
102-
ThrowIfDisposed();
103-
104-
if (_transactionManager == null)
105-
throw new InvalidOperationException("The transaction has already been committed.");
106-
107-
_transactionManager.Commit();
108-
_transactionManager = null;
109-
}
110-
111-
#endregion
112-
113-
#region Implementation of IDisposable
114-
115-
/// <summary>
116-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
117-
/// </summary>
118-
public void Dispose()
119-
{
120-
Dispose(true);
121-
GC.SuppressFinalize(this);
122-
}
123-
124-
#endregion
125-
126-
#region Implementation of IRepositoryFactory
127-
12897
/// <summary>
12998
/// Creates a new repository for the specified entity type.
13099
/// </summary>
@@ -191,6 +160,33 @@ public T CreateInstance<T>() where T : class
191160
return (T)Activator.CreateInstance(typeof(T), new object[] { _options });
192161
}
193162

163+
/// <summary>
164+
/// Commits all changes made in this unit of work.
165+
/// </summary>
166+
public virtual void Commit()
167+
{
168+
ThrowIfDisposed();
169+
170+
if (_transactionManager == null)
171+
throw new InvalidOperationException("The transaction has already been committed.");
172+
173+
_transactionManager.Commit();
174+
_transactionManager = null;
175+
}
176+
177+
#endregion
178+
179+
#region Implementation of IDisposable
180+
181+
/// <summary>
182+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
183+
/// </summary>
184+
public void Dispose()
185+
{
186+
Dispose(true);
187+
GC.SuppressFinalize(this);
188+
}
189+
194190
#endregion
195191

196192
#region Nested Type: RepositoryContextFactory

src/DotNetToolkit.Repository/Utility/AssemblyScanner.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Linq;
1212
using System.Reflection;
13+
using Transactions;
1314

1415
/// <summary>
1516
/// A helper class for scanning all repositories and interceptors from a specified assembly collection.
@@ -51,6 +52,10 @@ private AssemblyScanner([NotNull] IEnumerable<Type> types)
5152
typeof(IReadOnlyRepository<,,,>),
5253
typeof(IRepositoryInterceptor),
5354
typeof(ILoggerProvider),
55+
typeof(IRepositoryFactory),
56+
typeof(IServiceFactory),
57+
typeof(IUnitOfWork),
58+
typeof(IUnitOfWorkFactory),
5459
};
5560
}
5661

0 commit comments

Comments
 (0)