Skip to content

Instantly share code, notes, and snippets.

@amcoder
Created September 29, 2017 21:31
Show Gist options
  • Save amcoder/9c82914a4fbfd3deb3b485f85e5d2d6f to your computer and use it in GitHub Desktop.
Save amcoder/9c82914a4fbfd3deb3b485f85e5d2d6f to your computer and use it in GitHub Desktop.
.net core DI
public interface IWidgetCreator
{
Widget Create(Widget widget);
}
public interface IWidgetRepository
{
IEnumerable<Widget> GetAll();
Widget Get(string id);
Widget Create(Widget widget);
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IWidgetRepository, WidgetRepository>();
services.AddScoped<IWidgetCreator, WidgetCreator>();
services.AddScoped<IDocumentClient, DocumentClient>(s =>
new DocumentClient(new Uri("https://am-widget.documents.azure.com:443/"), "REDACTED")
);
}
// 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())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
[Route("api/widgets")]
public class WidgetsController : Controller
{
private IWidgetRepository Repository { get; set; }
private IWidgetCreator Creator { get; set; }
public WidgetsController(IWidgetRepository widgetRepository,
IWidgetCreator widgetCreator)
{
Repository = widgetRepository;
Creator = widgetCreator;
}
[HttpGet]
public IEnumerable<Domain.Widget> Get()
{
return Repository.GetAll();
}
[HttpGet("{id}")]
public IActionResult Get(string id)
{
var widget = Repository.Get(id);
if (widget == null)
return NotFound();
return Ok(widget);
}
[HttpPost]
public IActionResult Create([FromBody]Domain.Widget widget)
{
var newWidget = Creator.Create(widget);
return CreatedAtAction("Get", new { id = newWidget.Id }, newWidget);
}
[HttpPut("{id}")]
public void Update(string id, [FromBody]Domain.Widget widget)
{
}
[HttpDelete("{id}")]
public void Delete(string id)
{
}
}
public class WidgetCreator: IWidgetCreator
{
private IWidgetRepository Repository { get; set; }
public WidgetCreator(IWidgetRepository repository)
{
Repository = repository;
}
public Widget Create(Widget widget)
{
return Repository.Create(widget);
}
}
public class WidgetRepository: IWidgetRepository
{
private IDocumentClient Client { get; set; }
public WidgetRepository(IDocumentClient client)
{
Client = client;
}
public Domain.Widget Get(string id)
{
var response = Client.ReadDocumentAsync(
UriFactory.CreateDocumentUri("widgetDatabase", "widgets", id)
);
var result = response.Result;
return (dynamic)result.Resource;
}
public IEnumerable<Domain.Widget> GetAll()
{
return Client.CreateDocumentQuery<Domain.Widget>(
UriFactory.CreateDocumentCollectionUri("widgetDatabase", "widgets")
);
}
public Domain.Widget Create(Domain.Widget widget)
{
var response = Client.CreateDocumentAsync(
UriFactory.CreateDocumentCollectionUri("widgetDatabase", "widgets"),
widget
);
var result = response.Result;
return (dynamic)result.Resource;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment