Lifestyle.Scope - Object Lifetime Management in Simple Injector RRS feed

  • Question

  • User-1350042179 posted


    I use simpleinjector to depency inyection ,.....

    In my case I use Lifestyle.Scope(https://simpleinjector.readthedocs.io/en/latest/lifetimes.html).

    What is the meaning of scope?

    It is a web api method.

            public async Task<IHttpActionResult> ListaProyectoPorIdCiudadPaginado(ProyectoRequest request)
                var listaRespuesta = new List<RespuestaDto<Guid>>();
                    var resultado = await proyectoAppService.ListarProyectoxIdCiudadPaginadoAsync(request);
                    return Ok(new ProyectosResponseDto
                        Lista = resultado.Item1,
                        paginaSiguiente = resultado.Item2
                catch (Exception ex)
                    listaRespuesta.Add(FactoryDtos.CrearRespuestaError(new Guid(), ex));
                    return Ok(listaRespuesta);

    This calls other methods in the repository..

      public async Task<List<ProyectosResponse>> ListarProyectoxIdCiudadPaginadoAsync(ProyectoRequest filtros)
                var IdCiudadParam = new SqlParameter("@IdCiudad", SqlDbType.Char,8) { Direction = ParameterDirection.Input, Value = (object)filtros.IdCiudad ?? DBNull.Value };       
                var UrlBaseArchivosParam = new SqlParameter("@UrlBaseArchivo", SqlDbType.VarChar) { Direction = ParameterDirection.Input, Value = filtros.UrlBaseArchivos };
                var nroPaginaParam = new SqlParameter("@nroPagina", SqlDbType.Int) { Direction = ParameterDirection.Input, Value = filtros.NumeroPagina };
                var tamanioPaginaParam = new SqlParameter("@tamanioPagina", SqlDbType.Int) { Direction = ParameterDirection.Input, Value = filtros.TamanioPagina };
                var sqlQuery = "[Adm].[ProyectosPorIdCiudadRestPaginadoLista] @IdCiudad, @UrlBaseArchivo, @nroPagina, @tamanioPagina";
                var queryResult = ((IQueryableUnitOfWork)UnitOfWork).ExecuteQuery<ProyectosResponse>(sqlQuery, IdCiudadParam, UrlBaseArchivosParam, nroPaginaParam, tamanioPaginaParam);
                return await Task.FromResult(queryResult.ToList());

    Is the first method the scope?

    Monday, November 4, 2019 4:11 AM

All replies

  • User-719153870 posted

    Hi neoaguil17,

    What is the meaning of scope?

    According to the document you provided, there are three common lifestyles when using Simple Injector.

    1. Transient - A new instance of the component will be created each time the service is requested from the container and the instance will never be disposed.
    2. Scoped - For every request within an implicitly or explicitly defined scope. Instances will be disposed when their scope ends.
    3. Singleton - There will be at most one instance of the registered service type and instances will be disposed when the container is disposed.

    Check Scoped Lifestyle and you will see how to register the scope, like below:

    var container = new Container();
    // Set the scoped lifestyle one directly after creating the container
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
    // Use the Lifestyle.Scoped everywhere in your configuration.
    container.Register<IUserContext, AspNetUserContext>(Lifestyle.Scoped);
    container.Register<MyAppUnitOfWork>(() => new MyAppUnitOfWork("constr"),

    Is the first method the scope?

    Not sure, you need to find where you register the scope, if want to know more, you can find more useful information in the document.

    Best Regard,

    Yang Shen

    Tuesday, November 5, 2019 2:36 AM