You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to wrap my head around the concept of scopes, lifetimes and other more complex features in Grace.
To do it, I've created simple use case scenario in which the same interface resolves to different implementations depending on its "scope".
In my scenario, I have 2 named configurations, one for each camera.
Camera 1 is a fake camera (mockup)
Camera 2 is a real device (that is disposable).
I would like to be able to choose (in run time) a scope by its name and using Locate should locate the appropriate camera, depending on that "scope".
Notice that I use the term "scope" in a general way, not tied to Grace, because I don't know if my concept of scope aligns with Grace completely.
However, I've created some code to illustrate the use case. The test fails, and I'm almost sure that I'm missing something important because I don't really know what I'm doing...
Here it is:
using System;
using System.Collections.Generic;
using Grace.DependencyInjection;
using Xunit;
namespace GraceTests
{
public class ContainerTests
{
[Fact]
public void Given_dependencies_for_each_scope_locating_inside_a_scope_should_work()
{
var config = new Config
{
CameraConfigs = new[]
{
new CameraConfig
{
Name = "Cam1",
IsFake = true,
},
new CameraConfig
{
Name = "Cam2",
IsFake = false,
}
},
};
var container = CreateContainer(config);
using (var scope = container.BeginLifetimeScope("Cam1"))
{
scope.Locate<ICamera>();
}
}
private DependencyInjectionContainer CreateContainer(Config config)
{
var container = new DependencyInjectionContainer();
container.Configure(block =>
{
foreach (var sc in config.CameraConfigs)
{
var scopeName = sc.Name;
if (sc.IsFake)
{
block.Export<FakeCamera>().As<ICamera>().Lifestyle.SingletonPerNamedScope(scopeName);
}
else
{
block.Export<RealCamera>().As<ICamera>().Lifestyle.SingletonPerNamedScope(scopeName);
}
}
});
return container;
}
}
public class CameraConfig
{
public bool IsFake { get; set; }
public string Name { get; set; }
}
public class Config
{
public IEnumerable<CameraConfig> CameraConfigs { get; set; }
}
public interface ICamera
{
}
public class FakeCamera : ICamera
{
}
public class RealCamera : ICamera, IDisposable
{
public void Dispose()
{
}
}
}
The test fails with this message:
Could not locate scope with name Cam2
It complaints about scope "Cam2". However, the BeginLifetimeScope method is invoked with "Cam1".
What am I missing?
Also, I created this code blindly, trying to figure out what scopes are and how they're used.
Any help will be greatly appreciated, as always!
Thanks in advance.
The text was updated successfully, but these errors were encountered:
I'm trying to wrap my head around the concept of scopes, lifetimes and other more complex features in Grace.
To do it, I've created simple use case scenario in which the same interface resolves to different implementations depending on its "scope".
In my scenario, I have 2 named configurations, one for each camera.
I would like to be able to choose (in run time) a scope by its name and using Locate should locate the appropriate camera, depending on that "scope".
Notice that I use the term "scope" in a general way, not tied to Grace, because I don't know if my concept of scope aligns with Grace completely.
However, I've created some code to illustrate the use case. The test fails, and I'm almost sure that I'm missing something important because I don't really know what I'm doing...
Here it is:
The test fails with this message:
It complaints about scope "Cam2". However, the BeginLifetimeScope method is invoked with "Cam1".
What am I missing?
Also, I created this code blindly, trying to figure out what scopes are and how they're used.
Any help will be greatly appreciated, as always!
Thanks in advance.
The text was updated successfully, but these errors were encountered: