Answer the question
In order to leave comments, you need to log in
Why doesn't AddTransient() work in ASP.NET CORE 2.0?
There is an interface
using System;
namespace WebEmpty.Models
{
public interface IProductRepository
{
IEnumerable<Product> Products { get; }
}
}
using System;
namespace WebEmpty.Models
{
public class FakeProductRepository : IProductRepository
{
public IEnumerable<Product> Products => new List<Product>
{
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
};
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IProductRepository, FakeProductRepository>();
services.AddMvc();
}
public class ProductController : Controller
{
IProductRepository repository;
public ProductController(IProductRepository repo)
{
repository = repo;
}
public ViewResult List() => View(repository.Products);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question