Skip to content

Commit 86c44ae

Browse files
Merge pull request #7 from johelvisguzman/feature/repo-factory
Introduced a repository factory
2 parents c37d79f + 21255a7 commit 86c44ae

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace DotNetToolkit.Repository.EntityFramework
2+
{
3+
using System;
4+
using System.Data.Entity;
5+
6+
/// <summary>
7+
/// An implementation of <see cref="IRepositoryFactory" />.
8+
/// </summary>
9+
public class EfRepositoryFactory : IRepositoryFactory
10+
{
11+
#region Private Methods
12+
13+
private static DbContext GetDbContext(IRepositoryOptions options)
14+
{
15+
if (options.DbContextType == null)
16+
throw new InvalidOperationException($"The repository options must provide a {nameof(options.DbContextType)}.");
17+
18+
DbContext context;
19+
20+
if (string.IsNullOrEmpty(options.ConnectionString))
21+
context = (DbContext)Activator.CreateInstance(options.DbContextType);
22+
else
23+
context = (DbContext)Activator.CreateInstance(options.DbContextType, options.ConnectionString);
24+
25+
return context;
26+
}
27+
28+
#endregion
29+
30+
#region Implementation of IRepositoryFactory
31+
32+
/// <summary>
33+
/// Creates a new repository for the specified entity type.
34+
/// </summary>
35+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
36+
/// <param name="options">The options.</param>
37+
/// <returns>The new repository.</returns>
38+
public IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TEntity : class
39+
{
40+
return new EfRepository<TEntity>(GetDbContext(options));
41+
}
42+
43+
/// <summary>
44+
/// Creates a new repository for the specified entity and primary key type.
45+
/// </summary>
46+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
47+
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
48+
/// <param name="options">The options.</param>
49+
/// <returns>The new repository.</returns>
50+
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryOptions options) where TEntity : class
51+
{
52+
return new EfRepository<TEntity, TKey>(GetDbContext(options));
53+
}
54+
55+
#endregion
56+
}
57+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace DotNetToolkit.Repository
2+
{
3+
/// <summary>
4+
/// Represents a repository factory.
5+
/// </summary>
6+
public interface IRepositoryFactory
7+
{
8+
/// <summary>
9+
/// Creates a new repository for the specified entity type.
10+
/// </summary>
11+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
12+
/// <param name="options">The options.</param>
13+
/// <returns>The new repository.</returns>
14+
IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TEntity : class;
15+
16+
/// <summary>
17+
/// Creates a new repository for the specified entity and primary key type.
18+
/// </summary>
19+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
20+
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
21+
/// <param name="options">The options.</param>
22+
/// <returns>The new repository.</returns>
23+
IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryOptions options) where TEntity : class;
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace DotNetToolkit.Repository
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Represents a repository option for creating new repositories.
7+
/// </summary>
8+
public interface IRepositoryOptions
9+
{
10+
/// <summary>
11+
/// Gets or sets the connection string.
12+
/// </summary>
13+
string ConnectionString { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the type of the database context.
17+
/// </summary>
18+
Type DbContextType { get; set; }
19+
}
20+
}

version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>1.0.1</VersionPrefix>
4-
<VersionSuffix>alpha</VersionSuffix>
3+
<VersionPrefix>1.1.0</VersionPrefix>
4+
<VersionSuffix></VersionSuffix>
55
</PropertyGroup>
66
</Project>

0 commit comments

Comments
 (0)