diff --git a/SharpRepository.Samples.CoreMvc/Controllers/ContactsController.cs b/SharpRepository.Samples.CoreMvc/Controllers/ContactsController.cs index 3870294d..d4223ba1 100644 --- a/SharpRepository.Samples.CoreMvc/Controllers/ContactsController.cs +++ b/SharpRepository.Samples.CoreMvc/Controllers/ContactsController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using SharpRepository.Repository; using SharpRepository.CoreMvc.Models; +using SharpRepository.Samples.CoreMvc; namespace SharpRepository.CoreMvc.Controllers { @@ -13,9 +14,9 @@ public class ContactsController : Controller { protected IRepository repository; - public ContactsController(IRepository repository) + public ContactsController(IUserServiceCustom service) { - this.repository = repository; + this.repository = service.GetRepository(); } // GET: Contacts diff --git a/SharpRepository.Samples.CoreMvc/Startup.cs b/SharpRepository.Samples.CoreMvc/Startup.cs index e2c4da2d..c0200e01 100644 --- a/SharpRepository.Samples.CoreMvc/Startup.cs +++ b/SharpRepository.Samples.CoreMvc/Startup.cs @@ -9,6 +9,7 @@ using System; using MongoDB.Bson.Serialization; using SharpRepository.CoreMvc.Models; +using SharpRepository.Samples.CoreMvc; namespace SharpRepository.CoreMvc { @@ -32,6 +33,12 @@ public IServiceProvider ConfigureServices(IServiceCollection services) services.AddTransient(r => new EmailRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore")); + services.AddTransient(); + services.AddTransient(); + services.AddTransient( + r => new UserRepository( + RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore" + )); // return services.UseSharpRepository(Configuration.GetSection("sharpRepository")); //default InMemory // return services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "mongoDb"); // for Mongo Db return services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "efCore"); // for Ef Core diff --git a/SharpRepository.Samples.CoreMvc/UserService.cs b/SharpRepository.Samples.CoreMvc/UserService.cs new file mode 100644 index 00000000..25f583fa --- /dev/null +++ b/SharpRepository.Samples.CoreMvc/UserService.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.Logging; +using SharpRepository.CoreMvc.Models; +using SharpRepository.Repository; +using SharpRepository.Repository.Configuration; + +namespace SharpRepository.Samples.CoreMvc +{ + public class UserRepository : ConfigurationBasedRepository + { + public UserRepository(ISharpRepositoryConfiguration configuration, string repositoryName = null) : base(configuration, repositoryName) { } + } + + public class UserService : IUserService + { + protected IRepository _userRepository; + + public UserService(IRepository userRepository) + { + _userRepository = userRepository; + } + + public IRepository GetRepository() + { + return _userRepository; + } + } + + public interface IUserService + { + IRepository GetRepository(); + } + + public class UserServiceCustom : IUserServiceCustom + { + public UserRepository _userRepository; + + public UserServiceCustom(UserRepository userRepository) + { + _userRepository = userRepository; + } + + public IRepository GetRepository() + { + return _userRepository; + } + } + + public interface IUserServiceCustom + { + IRepository GetRepository(); + } +}