Doubt on how to Map an Interface with a Class - Simpleinjector

Asked

Viewed 52 times

1

I’m having trouble mapping the Interface IClienteRepository with class ClienteRepository. When compiling the application, this error appears:

"Exception {"To be Able to use the Lifestyle.Scoped Property, Please ensure that the container is configured with a default scoped lifestyle by Setting the Container.Options.Defaultscopedlifestyle Property with the required scoped lifestyle for your type of application. See: https://simpleinjector.org/lifestyles#scoped"} System.Invalidoperationexception"

How to solve?

public static Container RegisterServices(Container container)
    {
        //Domain to Repository

        container.Register<IClienteRepository, ClienteRepository>(Lifestyle.Scoped);

        return container;
    }

public interface IClienteRepository : IRepository<Cliente>
    {
    }

public class ClienteRepository : Repository<Cliente>, IClienteRepository
    {
        public ClienteRepository(SistemaComercialContext context)
            :base(context)
        {

        }
    }
  • That’s not the same one I answered you yesterday

  • similar problem, but now it is another situation involving Interface and class...

  • From what I’m reading in the documentation, after registering all the dependencies in the container, they call a container.Verify() and do not pass the Lifestyle, getting something like the Register container.Register<IClienteRepository, ClienteRepository>();

  • I believe that to use the scope you set, you have to make the following call after instantiating the container container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

1 answer

0


As I observed in the documentation of simpleinjector, for you to use the Lifestyle.Scoped, you need first of all to make sure that the option continer.Options.DefaultScopedLifestyle is properly configured.

Getting something like

public static Container RegisterServices(Container container)
{
    // Set the scoped lifestyle one directly after creating the container
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();          
    //Domain to Repository

    container.Register<IClienteRepository, ClienteRepository>(Lifestyle.Scoped);

    return container;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.