Skip to content

Commit f8739ad

Browse files
committed
Samples for issue #235
1 parent e103467 commit f8739ad

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

SharpRepository.Samples.CoreMvc/Controllers/ContactsController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
using Microsoft.AspNetCore.Mvc;
77
using SharpRepository.Repository;
88
using SharpRepository.CoreMvc.Models;
9+
using SharpRepository.Samples.CoreMvc;
910

1011
namespace SharpRepository.CoreMvc.Controllers
1112
{
1213
public class ContactsController : Controller
1314
{
1415
protected IRepository<Contact, string> repository;
1516

16-
public ContactsController(IRepository<Contact, string> repository)
17+
public ContactsController(IUserServiceCustom service)
1718
{
18-
this.repository = repository;
19+
this.repository = service.GetRepository();
1920
}
2021

2122
// GET: Contacts

SharpRepository.Samples.CoreMvc/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using MongoDB.Bson.Serialization;
1111
using SharpRepository.CoreMvc.Models;
12+
using SharpRepository.Samples.CoreMvc;
1213

1314
namespace SharpRepository.CoreMvc
1415
{
@@ -32,6 +33,12 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
3233

3334
services.AddTransient<EmailRepository>(r => new EmailRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore"));
3435

36+
services.AddTransient<IUserService, UserService>();
37+
services.AddTransient<IUserServiceCustom, UserServiceCustom>();
38+
services.AddTransient<UserRepository>(
39+
r => new UserRepository(
40+
RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore"
41+
));
3542
// return services.UseSharpRepository(Configuration.GetSection("sharpRepository")); //default InMemory
3643
// return services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "mongoDb"); // for Mongo Db
3744
return services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "efCore"); // for Ef Core
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.Extensions.Logging;
2+
using SharpRepository.CoreMvc.Models;
3+
using SharpRepository.Repository;
4+
using SharpRepository.Repository.Configuration;
5+
6+
namespace SharpRepository.Samples.CoreMvc
7+
{
8+
public class UserRepository : ConfigurationBasedRepository<Contact, string>
9+
{
10+
public UserRepository(ISharpRepositoryConfiguration configuration, string repositoryName = null) : base(configuration, repositoryName) { }
11+
}
12+
13+
public class UserService : IUserService
14+
{
15+
protected IRepository<Contact, string> _userRepository;
16+
17+
public UserService(IRepository<Contact, string> userRepository)
18+
{
19+
_userRepository = userRepository;
20+
}
21+
22+
public IRepository<Contact, string> GetRepository()
23+
{
24+
return _userRepository;
25+
}
26+
}
27+
28+
public interface IUserService
29+
{
30+
IRepository<Contact, string> GetRepository();
31+
}
32+
33+
public class UserServiceCustom : IUserServiceCustom
34+
{
35+
public UserRepository _userRepository;
36+
37+
public UserServiceCustom(UserRepository userRepository)
38+
{
39+
_userRepository = userRepository;
40+
}
41+
42+
public IRepository<Contact, string> GetRepository()
43+
{
44+
return _userRepository;
45+
}
46+
}
47+
48+
public interface IUserServiceCustom
49+
{
50+
IRepository<Contact, string> GetRepository();
51+
}
52+
}

0 commit comments

Comments
 (0)