using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ejemplo1.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Ejemplo1
{
public class Startup
{
private IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// services.AddSingleton<IAmigoAlmacen, MockAmigoRepositorio>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env )
{
if (env.IsDevelopment())
{
DeveloperExceptionPageOptions d = new DeveloperExceptionPageOptions
{
SourceCodeLineCount = 2
};
app.UseDeveloperExceptionPage(d);
}
else if (env.IsProduction() || env.IsStaging())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
}
}
using Ejemplo1.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ejemplo1.Controllers
{
public class HomeController:Controller
{
private IAmigoAlmacen amigoAlmacen;
public HomeController(IAmigoAlmacen AmigoAlmacen)
{
amigoAlmacen = AmigoAlmacen;
}
public string Index()
{
return amigoAlmacen.dameDatosAmigo(1).Email;
}
}
}
- Clase MockAmigoRepositorio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ejemplo1.Models
{
public class MockAmigoRepositorio:IAmigoAlmacen
{
private List<Amigo> amigosLista;
public MockAmigoRepositorio()
{
amigosLista = new List<Amigo>();
amigosLista.Add(new Amigo() { Id = 1, Nombre = "Pedro", Ciudad = "Madrid", Email = "Pedro@mail.com" });
amigosLista.Add(new Amigo() { Id = 2, Nombre = "Juan", Ciudad = "Toledo", Email = "Juan@mail.com" });
amigosLista.Add(new Amigo() { Id = 3, Nombre = "Sara", Ciudad = "Cuenca", Email = "Sara@mail.com" });
}
public Amigo dameDatosAmigo(int Id)
{
return this.amigosLista.FirstOrDefault(e => e.Id == Id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ejemplo1.Models
{
public interface IAmigoAlmacen
{
Amigo dameDatosAmigo(int Id);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ejemplo1.Models
{
public class Amigo
{
public int Id { get; set; }
public string Nombre { get; set; }
public string Email { get; set; }
public string Ciudad { get; set; }
}
}