Introducing Cake Bridge Dependency Injection

Utilize Cake abstractions and addins using Microsoft dependency injection

The Need

Introducing Cake.Bridge.DependencyInjection

Usage

Obtain

dotnet add package Cake.Bridge.DependencyInjection
<PackageReference Include="Cake.Bridge.DependencyInjection" Version="0.1.0" />

Register

using Cake.Bridge.DependencyInjection; ... serviceCollection .AddCakeCore();

Resolve

using Cake.Core; ... var cakeContext = serviceProvider.GetRequiredService<ICakeContext>();

Use

Example ICakeContext

using Cake.Bridge.DependencyInjection;
using Cake.Core;
using Cake.Core.Diagnostics;
using Microsoft.Extensions.DependencyInjection;

var serviceCollection = new ServiceCollection()
.AddCakeCore();

var serviceProvider = serviceCollection.BuildServiceProvider();

var cakeContext = serviceProvider.GetRequiredService<ICakeContext>();

cakeContext.Log.Write(
Verbosity.Normal,
LogLevel.Information,
"Hello World!");
Hello World!

Example IScriptHost and Cake.Common

dotnet add package Cake.Common --version 1.0.0-rc0002
<PackageReference Include="Cake.Common" Version="1.0.0-rc0002" />
using Cake.Bridge.DependencyInjection;
using Cake.Core;
using Cake.Common.Diagnostics;
using Cake.Core.Scripting;
using Microsoft.Extensions.DependencyInjection;

var serviceCollection = new ServiceCollection()
.AddCakeCore();

var serviceProvider = serviceCollection.BuildServiceProvider();

var scriptHost = serviceProvider.GetRequiredService<IScriptHost>();

scriptHost.Task("Hello")
.Does(ctx => ctx.Information("Hello"));

scriptHost.Task("World")
.IsDependentOn("Hello")
.Does(ctx => ctx.Information("World"));

await scriptHost.RunTargetAsync("World");
========================================
Hello
========================================
Hello

========================================
World
========================================
World

Task Duration
--------------------------------------------------
Hello 00:00:00.0226275
World 00:00:00.0002682
--------------------------------------------------
Total: 00:00:00.0228957

Complete example project

Resources

--

--

Partner & Technical fellow at WCOM AB. Microsoft Azure & Developer Technologies MVP. Been coding since I 80’s (C128 & Amiga). Father of 2, husband of 1.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mattias Karlsson

Partner & Technical fellow at WCOM AB. Microsoft Azure & Developer Technologies MVP. Been coding since I 80’s (C128 & Amiga). Father of 2, husband of 1.