Problems with a Dependency Injection Container - Simple Injector

Asked

Viewed 236 times

1

I am mounting my ID container using Simple Injector, but it is giving a compilation error (Underlined in red) in config. from the Ireadonlyrepository interface with the Clientedapperrepository class. I think I’m doing something wrong but I don’t know what rsrs are.

Someone knows how to help me?

//Codes

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

        container.Register<IReadOnlyRepository, ClienteDapperRepository>(Lifestyle.Scoped);

        return container;
    }



     public interface IReadOnlyRepository<TEntity> where TEntity : class
     {
         TEntity Get(int id);
         IEnumerable<TEntity> All();
         IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
     }



    public class ClienteDapperRepository : Common.Repository, IClienteReadOnlyRepository
    {
        public IEnumerable<Cliente> All()
        {
            using (var cn = SistemaComercialConnection)
            {
                var cliente = cn.Query<Cliente>("Select * from Cliente");
                return cliente;
            }
        }

    }
  • The name is not wrong?

  • The names are right, but he doesn’t accept this structure... I don’t know if there’s any other way...

  • can send the error code?

  • If I leave it as it is, it just stands underlined in red. If I change to container.Register<Ireadonlyrepository<Client>, Clientedapperrepository>(Lifestyle.Scoped); It generates this error: "+ $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 "

  • Please post the code instead of images. And if you look at the build output, you will see the error, post it too

  • I think the way I’m doing it is wrong...

  • your second Register wouldn’t have to look like the first one? Getting something like container.Register(typeof(IReadOnlyRepository<>), typeof(ClienteDapperRepository<>, Lifestyle.Scope)

  • I posted the Personal Code.

  • I tried it like this: container. Register(typeof(Ireadonlyrepository<>), typeof(Clientedapperrepository<>), Lifestyle.Scoped); but it underlines in red the Clientedapperrepository.

  • Suddenly the error pq tua classs Clientedapperrepository is not generic, so it has to be as follows container.Register(typeof(IReadOnlyRepository<>), typeof(ClienteDapperRepository), Lifestyle.Scoped);

  • If I were to use Ninject, just use: Bind<Ireadonlyrepository<Genre>>(). To<Genredapperrepository>(); that would work.... Only there must be some difference in the Simpleinjector...

  • Pablo, it worked bro!!!!!!! Thank you very much dude!!!!!!!!!

  • Answer there on the topic of the question so I can mark as answer.. :)

  • I just think you could improve your question by taking the pictures and putting the error message you had. But leaving the code.

  • I just edited it! Thank you!

Show 10 more comments

1 answer

0


What happens is that your call to container.Register is wrong, you are trying to do dependency injection using as if the Restarter was Generic, where the correct is that it waits 3 parameters, having to stay as follows your dependency injection.

container.Register(typeof(IReadOnlyRepository<>), typeof(ClienteDapperRepository), Lifestyle.Scoped);

Browser other questions tagged

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