// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { TypeBroker.SetRepositoryType <AlternativeRepository>(); //services.AddTransient<IRepository, MemoryRepository>(); //using the scoped life cicle, using this the same MemoryRepository is used for any class that need a //implementation of it in the same scope services.AddScoped <IRepository, MemoryRepository>(); //using singleton life cycle, a single object is used to resolve all the dependencies for a given //service and it's reused for any subsquent need of dependecies. services.AddSingleton <IRepository, MemoryRepository>(); //Using a Factory function services.AddTransient <IRepository>(provider => { if (env.IsDevelopment()) { var x = provider.GetService <MemoryRepository>(); return(x); } else { return(new AlternativeRepository()); } }); //MemoryReposotry have dependencies from IModelStorage, so is required to add a Transient //to resolve any dependencias that it haves. services.AddTransient <MemoryRepository>(); services.AddTransient <IModelStorage, DictionaryStorage>(); services.AddTransient <ProductTotalizer>(); // Add framework services. services.AddMvc(); }
public void ControllerTest() { //Arrange var data = new[] { new Product { Name = "Test", Price = 100 } }; var mock = new Mock <IRepository>(); mock.SetupGet(m => m.Products).Returns(data); TypeBroker.SetTestObjet(mock.Object); HomeController controller = new HomeController(); //Act ViewResult result = controller.Index(); //Assert Assert.Equal(data, result.ViewData.Model); }
public void ConfigureServices(IServiceCollection services) { TypeBroker.SetRepositoryType <AlternateRepository>(); services.AddMvc(); }