IGet 1.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package IGet --version 1.0.9                
NuGet\Install-Package IGet -Version 1.0.9                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IGet" Version="1.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IGet --version 1.0.9                
#r "nuget: IGet, 1.0.9"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IGet as a Cake Addin
#addin nuget:?package=IGet&version=1.0.9

// Install IGet as a Cake Tool
#tool nuget:?package=IGet&version=1.0.9                

IGet

Say goodbye to MediatR's IRequest-IRequestHandler combination and use IGet instead: less code, more freedom.

serviceCollection.AddIGet();

Why?

You're probably very happy with how MediatR solves injecting dependencies into your IRequestHandler implementations. You might however like IGet better as a simpler alternative for the IRequest-IRequestHandler combination:

  • you don't need to implement any interface for your handlers.
  • creating a request class is optional.
  • have compile-time checks that all handlers exist.
  • use editor shortcuts to jump to a handler method immediately.
  • have a shorter StackTrace in case of an error.
  • etc.

IGet can also be used for code that previously used the INotification-INotificationHandler combination or things like the IRequestPostProcessor. See the examples below. (Note that IGet has only one responsibility: "instantiating classes with their dependencies injected".)

But wait!

Question: How does IGet differ from IServiceProvider? It looks like a copy!

Answer: The main difference is that IGet provides you classes that are NOT in your service collection (and it injects the dependencies from your service collection). There is no need to register those classes.

Declaring a handler

Example 1
public class MyHandler
{
    private ILogger<MyHandler> _logger;

    public MyHandler(ILogger<MyHandler> logger)
    {
        _logger = logger;
    }

    public MyResult AnyRandomSignature(int id)
    {
        // do something
    }
}
Example 2
public class MyHandler
{
    private ILogger<MyHandler> _logger;

    public MyHandler(ILogger<MyHandler> logger)
    {
        _logger = logger;
    }

    public void Handle()
    {
        // do something
    }
}

Sending a request

In the examples below i is the IGet instance where you call Get on. (Like mediator might have been the name of the variable for a IMediator instance where you called Send on.)

Example 1
var result = i.Get<MyHandler>().AnyRandomSignature(1);
Example 2
var handler = i.Get<MyHandler>();
handler.Handle();
Example 3
var result = await i.Get<MyHandler>().HandleAsync(new MyRequest
{
    Id = 2
});

etc.

More complex scenarios

Example 1

Handlers may get other handlers to do stuff for them, like validation, pre-processing and post-processing.

public class MoreComplexHandler
{
    private ILogger<MoreComplexHandler> _logger;
    private IGet i;

    public MoreComplexHandler(IGet iget, ILogger<MoreComplexHandler> logger)
    {
        _logger = logger;
        i = iget;
    }

    public Result<WhatWasAskedFor> Handle(Request request)
    {
        try
        {
            var validationResult = i.Get<Validator>().Validate(request);
            if (!validationResult.IsValid)
            {
                return Result.Fail<WhatWasAskedFor>(validationResult.Message);
            }

            i.Get<PreProcessor>().Prepare(request);
            var whatWasAskedFor = i.Get<MainProcessor>().Handle(request);

            try
            {
                i.Get<PostProcessor>().DoLessImportantStuffWith(request, whatWasAskedFor);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "PostProcessor failed for request {requestId}.", request.Id);
            }

            return Result.Success(whatWasAskedFor);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected error for request {requestId}.", request.Id);
            return Result.Fail<WhatWasAskedFor>("Something went wrong. Try again later.");
        }
    }
}
Example 2

When you think of notifications, you might think of subscribers. MediatR does not support this concept of subscribing at runtime. A notification publisher that allows subscribing handlers at runtime is also out of the problem scope for this package - not because it is too hard to create, but because that problem deserves a package of its own.

The functionality of MediatR's INotification-INotificationHandler combination can be created via something like this:

public class NotificationPublisher
{
    private IGet i;

    public NotificationPublisher(IGet iget)
    {
        i = iget;
    }

    public async Task PublishAsync(Notification notification)
    {
        try
        {
            await i.Get<FirstHandler>().HandleAsync(notification);
        }
        catch { }
        try
        {
            await i.Get<SecondHandler>().HandleAsync(notification);
        }
        catch { }
        try
        {
            i.Get<ThirdHandler>().Handle(notification);
        }
        catch { }
    }
}

which can be called via

await i.Get<NotificationPublisher>().PublishAsync(notification);
Example 3

The examples above give an idea of how you can be creative with IGet. Share your own examples online to spread the word about IGet.

Try it out

What problem would you like to solve today? Give it a try!

One more question: Why does this package not have a repository?

Answer: This package does not have a repository because then the discussion page would explode with questions. If you're interested in the source code, then download the package via NuGet, use it in Visual Studio and use F12 to navigate to the definition.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on IGet:

Package Downloads
IGet.GetAll

Extends IGet. Get an IEnumerable of class instances (with their dependencies injected) via i.GetAll<IMyInterface>() or i.GetAll<MyBaseClass>(). Additional setup: serviceCollection.AddIGetAll(new [] { typeof(Startup).Assembly, ... }). See the readme for many examples.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.8 17,394 12/13/2023
1.1.7 133 12/13/2023
1.1.6 5,161 4/12/2023
1.1.5 970 3/29/2023
1.1.4 297 3/21/2023
1.1.3 297 3/20/2023
1.1.2 190 3/19/2023
1.1.1 326 3/16/2023
1.1.0 200 3/15/2023
1.0.14 198 3/15/2023
1.0.13 209 3/14/2023
1.0.12 207 3/14/2023
1.0.11 212 3/14/2023
1.0.10 208 3/14/2023
1.0.9 212 3/13/2023
1.0.8 215 3/13/2023
1.0.7 205 3/13/2023
1.0.6 194 3/13/2023
1.0.5 205 3/12/2023
1.0.4 222 3/12/2023
1.0.3 195 3/11/2023
1.0.2 211 3/10/2023
1.0.1 227 3/10/2023
1.0.0 227 3/10/2023
1.0.0-alpha 147 3/10/2023