Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-scope usage scenario #276

Open
SuperJMN opened this issue Apr 22, 2021 · 0 comments
Open

Multi-scope usage scenario #276

SuperJMN opened this issue Apr 22, 2021 · 0 comments

Comments

@SuperJMN
Copy link

SuperJMN commented Apr 22, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant