Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Core DI injection with user id

$
0
0

I would like to inject a user ID to all my services and utilities.

Here is my current setup:

    public class StructureUtilities : IStructureUtilities
    {
        /// <summary>
        /// The structure service
        /// </summary>
        private readonly IStructureService structureService;
        private readonly IMapper mapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="StructureUtilities" /> class.
        /// </summary>
        /// <param name="structureService">The structure service.</param>
        /// <param name="mapper">The mapper.</param>
        public StructureUtilities(IStructureService structureService, IMapper mapper)
        {
            this.structureService = structureService;
            this.mapper = mapper;
        }

        /// <summary>
        /// Gets the structure tree.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns>GetStructureTree</returns>
        public async Task<IEnumerable<StructureTreeViewModel>> GetStructureTree(int userId)
        {
            var test = await structureService.GetStructureTreeByUserId(161).ConfigureAwait(false);
            return mapper.Map<IEnumerable<StructureTreeViewModel>>(test);
        }
    }
}
        public void RegisterDependencyTypes(IServiceCollection services)
        {
            int loggedInuser = 161;

            // Context Model
            services.AddSingleton(x =>
            {
                return new ContextModel(loggedInuser);
            });

            services.AddSingleton<IMapper>(x => new Mapper(mapperConfiguration));

            services.AddSingleton<IStructureUtilities, StructureUtilities>();
            services.AddSingleton<IStructureService, StructureService>();
            services.AddSingleton<IStructureRepository, StructureRepository>();
        }

So how would set this up so I can pass a user id in of the logged-in user?

I would want to do this on the StructureService as well.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>