dependency-injection

How to register interface with Generic type in Startup.cs

和自甴很熟 提交于 2019-12-20 03:26:06
问题 I am using WebAPI in ASP.NET Core. This works: services.AddScoped<IApiKeysService, APIKeysService>(); Now that the interface is returning a generic type T This does not services.AddScoped<IApiKeysService>(); Error Using the generic type blah requires 1 type Arguments Or this services.AddScoped<IApiKeysService<T>>(); I get the following compile error type or namespace T cannot be found How do I register it in Startup.cs ? NOTE: I cannot use a concrete type because the interface member is a

Jersey and HK2 - Injecting current user

戏子无情 提交于 2019-12-20 03:19:27
问题 I'm working with jersey 2.17 and HK2 to create a simple rest app. I have a ContainerRequestFilter that rejects any request that doesn't have the "currentuser" cookie. I have something like this: @Path("/users") public class UserResource { private UserService userService; @GET @Path("/orders") @Produces("application/json") public List<Order> findOrdersOfCurrentUser() { // some ugly code to access headers, extract cookies, and finally // extract username (a String) from a particular cookie

How do I avoid the service locator pattern? Should I?

倖福魔咒の 提交于 2019-12-20 03:12:52
问题 I'm currently working on a WinForms system (I know) where there's a lot of Constructor Injection when creating forms, but if those forms/views need to open another form, I find the DI container has been injected too so that we can locate the implementation of the desired view interface at runtime. e.g. public partial class MyView : Form, IMyView { private readonly IDIContainer _container; public MyView(IDIContainer container) { InitializeComponent(); _container = container; } public

Class diagram - dependency or association from class to interface?

喜夏-厌秋 提交于 2019-12-20 03:10:42
问题 I have a question regarding creating a class diagram where I have used dependency injection. I have following code example: public class ReservationController : ApiController { private readonly IGetReservationService _getReservationService; public ReservationController(IGetReservationService getReservationService) { _getReservationService = getReservationService; } // GET list of all reservations public List<ReservationViewModel> GetReservations() { return _getReservationService

Dagger 2 : error while getting a multiple instances of same object with @Named

一曲冷凌霜 提交于 2019-12-20 02:45:20
问题 How can i get multiple instances of same return type like cursor for example :- Module @CursorScope public class CursorModule { @Provides Cursor provideSongCursor( @Named("Song") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ BaseColumns._ID, MediaStore.Audio.AudioColumns.TITLE, MediaStore.Audio.AudioColumns.ARTIST, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.DURATION },

Autofac - dependency injection for MVC controller and Web Api controller

狂风中的少年 提交于 2019-12-20 02:35:45
问题 I have MVC controllers (in Controllers folder) and Web Api controllers (in Api folder) in the same project: Here is the folder structure: Controllers ProductController Api ProductController Here is my bootstrapper method: private static void SetAutofacContainer() { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); //builder.RegisterType<UnitOfWork>().As<IUnitOfWork>()

Registering decorators with primitive configuration dependencies in Simple Injector

南楼画角 提交于 2019-12-20 02:32:55
问题 I have an IAppSettingsLoader interface that abstracts away the file IO for loading my app.config file. public interface IAppSettingsLoader { IEnumerable<KeyValuePair<string, string>> LoadAppSettings(); } I have a class that loads the actual file: public class FileAppSettignsLoader : IAppSettingsLoader { public IEnumerable<KeyValuePair<string, string>> LoadAppSettings() { // Perform actual loading through ConfigurationManager.AppSettings } } Then I have a caching decorator that tries to

How to inject AutoMapper with Autofac?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:16:18
问题 What is the proper way to inject AutoMapper to other layers? I read this blog post , but this code cause exception below An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code when try mapping in service layer. List<StudentViewModel> list2 = _mapper.Map<List<StudentViewModel>>(list); My AutoFac configuration like below: public static class DependencyRegistration { public static void Config() { var builder = new ContainerBuilder

What is the correct place to add a database driven scheduler in ASP.NET Core?

Deadly 提交于 2019-12-20 01:58:26
问题 I have added a Timer to Startup class of an ASP.Net Core application. It fires on some time period and do operations like logging a sample text. I need it to be able to do database driven operations like adding a record to a table. So I try to get AppDbContext from DI but it is always null. Please see the code: public class Scheduler { static Timer _timer; static bool _isStarted; static ILogger<Scheduler> _logger; const int dueTimeMin = 1; const int periodMin = 1; public static void Start

Binding with @Autowired not working inside instances initiated with 'new'

你。 提交于 2019-12-20 01:45:46
问题 In my web spring application I create an instance with key word new as following. In my one of action class, following method exists. public void process() { MyBean b=new MyBean(); //initiated the instance with new b.process(); } Other MyBean class @Service public class MyBean { @Autowired MyService service; public void process() { service.execute(); // this service instance has not initialized by Spring DI :( .service object is null. } the MyService instance is not set by spring Dependency