Skip to content

Commit

Permalink
Merge pull request #10 from servicetitan/readme-imrovements
Browse files Browse the repository at this point in the history
Readme improvements
  • Loading branch information
hypercodeplace authored Dec 2, 2020
2 parents 59b5cbd + b97a637 commit faf3b97
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions LazyProxy.Unity.Sample/LazyProxy.Unity.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>8</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Unity.Container" Version="5.10.2" />
Expand Down
13 changes: 8 additions & 5 deletions LazyProxy.Unity.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public static void Main(string[] args)

private static void UnityExtensionExample1()
{
var container = new UnityContainer()
.RegisterLazy<IMyService, MyService>();
// Creating a container
using var container = new UnityContainer();

// Adding a lazy registration
container.RegisterLazy<IMyService, MyService>();

Console.WriteLine("Resolving service...");
Console.WriteLine("Resolving the service...");
var service = container.Resolve<IMyService>();

Console.WriteLine("Foo execution...");
Console.WriteLine("Executing the 'Foo' method...");
service.Foo();
}

Expand Down Expand Up @@ -56,4 +59,4 @@ private static void UnityExtensionExample2()
}
}
}
}
}
6 changes: 3 additions & 3 deletions LazyProxy.Unity.Sample/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public interface IMyService

public class MyService : IMyService
{
public MyService() => Console.WriteLine("Hello from ctor");
public void Foo() => Console.WriteLine("Hello from Foo");
public MyService() => Console.WriteLine("Ctor");
public void Foo() => Console.WriteLine("Foo");
}

public abstract class Warrior
Expand Down Expand Up @@ -111,4 +111,4 @@ public Weapon CreateShuriken()
return new Weapon(damage);
}
}
}
}
63 changes: 47 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
# Lazy injection for Unity container
# Lazy Dependency Injection for Unity Container

A [LazyProxy](https://github.com/servicetitan/lazy-proxy) can be used for IoC containers to change the resolving behaviour.
A [LazyProxy](https://github.com/servicetitan/lazy-proxy) can be used for IoC containers to improve performance by changing the resolve behavior.

Dependencies registered as lazy are created as dynamic proxy objects built in real time, but the real classes are resolved only after the first execution of proxy method or property.
More info can be found in the article about [Lazy Dependency Injection for .NET](https://dev.to/hypercodeplace/lazy-dependency-injection-37en).

Also dynamic lazy proxy allows injection of circular dependencies.
## Get Packages

```C#
var container = new UnityContainer().RegisterLazy<IMyService, MyService>();
The library provides in NuGet.

Console.WriteLine("Resolving service...");
```
Install-Package LazyProxy.Unity
```

## Get Started

Consider the following service:

```CSharp
public interface IMyService
{
void Foo();
}

public class MyService : IMyService
{
public MyService() => Console.WriteLine("Ctor");
public void Foo() => Console.WriteLine("Foo");
}
```

A lazy registration for this service can be added like this:

```CSharp
// Creating a container
using var container = new UnityContainer();

// Adding a lazy registration
container.RegisterLazy<IMyService, MyService>();

Console.WriteLine("Resolving the service...");
var service = container.Resolve<IMyService>();

Console.WriteLine("Foo execution...");
Console.WriteLine("Executing the 'Foo' method...");
service.Foo();
```

// Resolving service...
// Foo execution...
// Hello from ctor
// Hello from Foo
The output for this example:

```
Resolving the service...
Executing the 'Foo' method...
Ctor
Foo
```

## Features

The following is supported:
Currently, `LazyProxy.Unity` supports the following:
- Registration of types by interfaces;
- Passing lifetime managers;
- Passing injection members;
- Resolving by child containers.

**Not supported yet:**
- Registration of instances.

## Performance

Here is a result of the [Benchmark test](https://github.com/servicetitan/lazy-proxy-unity/blob/master/LazyProxy.Unity.Benchmarks/UnityExtensionBenchmark.cs)
Expand Down

0 comments on commit faf3b97

Please sign in to comment.