Rezolver Dev Guide
Welcome to the Rezolver developer guide - the best resource to learn about all the features of Rezolver, and tips and tricks to get the most out of it.
Getting started
As with many open source .Net projects, there are two primary ways to get Rezolver integrated into your project.
- The easiest is via the Nuget packages
- By using the Nuget packages, you ensure that you're using the latest (hopefully stable!) releases. Pre-release packages will also be made available for in-development features.
- Fork or download the Rezolver source from Github
- Use this if you can't integrate nuget into your build pipeline, or if you'd like to customise or contribute to the project.
- If you're looking to customise, though, then there should be enough extensibility points in the framework to allow you to do so without having to change core types. If that's not the case, post an issue on the Github project so we can get it added in and make it better for everyone!
Tests as Examples
Most of the example code shown here is drawn directly from the project Rezolver.Tests.Examples
, which can be found
in the test
folder under the root of the repo. If there's a filename shown at the top of a code example, then that
should equal the filename where you can find that code in the examples project.
Please note that any example code containing type declarations will be found in the Types
folder of that project, with all the
individual test files in the root.
We're using xunit for all our tests, hence all the examples are written for it too.
Matching an example to a test
We do omit the test function declaration in these examples - but if you look down a tests file you'll see comments inside
each test method with an XML tag. Here's the body of the ObjectExamples.cs
file, which contains all the examples
for the 'Objects as Services' documentation:
// Copyright (c) Zolution Software Ltd. All rights reserved.
// Licensed under the MIT License, see LICENSE.txt in the solution root for license information
using Rezolver.Tests.Examples.Types;
using Xunit;
namespace Rezolver.Tests.Examples
{
public class ObjectExamples
{
[Fact]
public void ShouldRegisterAndRetrieveByItsType()
{
// <example1>
var container = new Container();
var service = new MyService();
container.RegisterObject(service);
Assert.Same(service, container.Resolve<MyService>());
// </example1>
}
[Fact]
public void ShouldRegisterAndRetrieveByInterface()
{
// <example2>
var container = new Container();
var service = new MyService();
container.RegisterObject<IMyService>(service);
// NOTE: Could also use:
// container.RegisterObject(service, typeof(IMyService)
Assert.Same(service, container.Resolve<IMyService>());
// </example2>
}
[Fact]
public void ShouldNotDisposeByDefault()
{
// <example10>
var disposableObj = new DisposableType();
using (var container = new ScopedContainer())
{
container.RegisterObject(disposableObj);
var result = container.Resolve<DisposableType>();
Assert.Same(disposableObj, result);
}
// Should NOT be disposed
Assert.False(disposableObj.Disposed);
// </example10>
}
[Fact]
public void ShouldDispose()
{
// <example11>
var disposableObj = new DisposableType();
using (var container = new ScopedContainer())
{
container.RegisterObject(disposableObj, scopeBehaviour: ScopeBehaviour.Explicit);
var result = container.Resolve<DisposableType>();
Assert.Same(disposableObj, result);
}
// Should be disposed
Assert.True(disposableObj.Disposed);
// </example11>
}
[Fact]
public void OnlyRootScopeShouldDispose()
{
// <example12>
var disposableObj = new DisposableType();
using (var container = new ScopedContainer())
{
container.RegisterObject(disposableObj, scopeBehaviour: ScopeBehaviour.Explicit);
using (var scope = container.CreateScope())
{
var result = container.Resolve<DisposableType>();
Assert.Same(disposableObj, result);
}
// Should not be disposed here...
Assert.False(disposableObj.Disposed);
}
// ... but should be disposed here
Assert.True(disposableObj.Disposed);
// </example12>
}
}
}
You'll notice that the body of each test method looks like this:
//<examplen>
... (code) ...
//</examplen>
Note
This is a facet of the code snippet feature of docfx
Each example is numbered sequentially (the n
), so it shouldn't be too hard to marry up the example in the documentation to the
test in the tests file.
Something missing?
This site is always under development, and we're doing everything we can to get all high-level documentation in place so that even novice users of DI containers can get up and running with Rezolver.
If you have a question about something that's not covered here, please feel free to open an issue over on Github.
Next Steps
- Asp.Net Core developers should read how to integrate Rezolver into the Asp.Net Core hosting pipeline.
- Learn how to create and use a container.