dependency-injection

Can't use registered singetons in ConfigureServices without them being instantiated twice

China☆狼群 提交于 2020-01-01 14:34:13
问题 I have a .Net Core project that registers a number of Singletons as follows: public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); services.AddLogging(); services.AddSingleton<IConfiguration>(Configuration); services.AddSingleton<IDbFactory, DefaultDbFactory>(); services.AddSingleton<IUserRepository, UserRepository>(); services.AddSingleton<IEmailService, EmailService>(); services.AddSingleton<IHostedService, BackgroundService>(); services.AddSingleton

Can the Cake Pattern be used for non-singleton style dependencies?

对着背影说爱祢 提交于 2020-01-01 14:22:31
问题 Most of the examples of the Cake Pattern I've come across appear to consider dependencies as singleton type services; where there is only one instance of each type in the final assembly of components. Is it possible to write a configuration that has more than one instance of a particular type, perhaps configured in different ways, when using the Cake Pattern for dependency injection? Consider the following components. Generic HTTP service: trait HttpService { def get(query:String):String }

Can the Cake Pattern be used for non-singleton style dependencies?

旧巷老猫 提交于 2020-01-01 14:22:29
问题 Most of the examples of the Cake Pattern I've come across appear to consider dependencies as singleton type services; where there is only one instance of each type in the final assembly of components. Is it possible to write a configuration that has more than one instance of a particular type, perhaps configured in different ways, when using the Cake Pattern for dependency injection? Consider the following components. Generic HTTP service: trait HttpService { def get(query:String):String }

How to catch an exception and respond with a status code in .NET Core

倾然丶 夕夏残阳落幕 提交于 2020-01-01 12:18:07
问题 I am running a .Net Core Web API project. I have a startup file (below). In the Startup.ConfigureServices(...) method, I add a factory method that creates an instance of IFoo. I want to catch any exceptions that the IFooFactory throws and return a better error message with a status code. At the moment I getting a 500 Error with the exception Message. Can anyone help? public interface IFooFactory { IFoo Create(); } public class FooFactory : IFooFactory { IFoo Create() { throw new Exception(

Ninject Contextual Binding at RunTime

。_饼干妹妹 提交于 2020-01-01 11:48:17
问题 I am trying to understand Ninject Contextual Binding. I understand the scenarios where I know my context at design time. e.g. I understand that I can use Named Attributes to Bind the DB object to a mock DB when I want to use it in a test class and to a SQL DB when I use it from my actual code. However, I don't know how to handle contextual Binding at runtime. e.g. let's say I am writing software for a shopping center. The shopkeeper can use a keyboad for billing or a barcode scanner. I don't

Associating FXML and Controller in Guice's Module configuration

那年仲夏 提交于 2020-01-01 10:47:49
问题 In my Guice Module I want to associate FXML files and their controllers, currently it looks like this: public class GuiceModule extends AbstractModule { @Override protected void configure() { // associate controllers and fxml files bind(MainController.class).toInstance((MainController)loadController("/main.fxml")); bind(SubController.class).toInstance((SubController)loadController("/content.fxml")); } protected Object loadController(String url) { InputStream fxmlStream = null; try {

In Ninject, how can I run custom code on an object after it is created with Bind<..>.ToSelf()?

雨燕双飞 提交于 2020-01-01 10:01:17
问题 In Ninject's dependency injection, if you set up a binding of a class to itself like so: Bind<SomeClass>().ToSelf(); Ninject very nicely resolves any dependencies SomeClass has and gives you the object back. I want to be able to do something to the SomeClass it returns every time it creates a new one, so like a post-processing event. I could use the .ToMethod (or ToFactoryMethod) binding to explicitly new it up, but I would like all its dependencies resolved by Ninject beforehand. It wouldu

Creating new instances while still using Dependency Injection

落爺英雄遲暮 提交于 2020-01-01 09:55:19
问题 A quick description of the environment: I have a class that represents a chatroom and has a dependency on a logger. It's not the same as a system-wide logger with cross-cutting concerns, but a logger that's tied to that specific chatroom. It logs all activity in that chatroom to it's unique log file. When the chatroom is created I want to open the log file, and when it's destroyed I want to close the log file. The Problem Here's the relevant code I'm using. public interface IChatroomLogger {

Inject EJB in Jersey filter

扶醉桌前 提交于 2020-01-01 09:27:14
问题 I am currently developing an application with Jersey JAX-RS as backend and AngularJS as frontend ; I need a sort of authentication, and so with every request I send a token that should be verify by the backend. For this, I decided to create a Jersey filter that will look for that token, and then call my AuthenticateService to check if the user can be authenticated. Authorization is then managed by @RolesAllowed annotation. Here is my problem : I can't inject an EJB inside a Jersey filter,

Autofac named registration constructor injection

那年仲夏 提交于 2020-01-01 08:49:31
问题 Does Autofac support specifying the registration name in the components' constructors? Example: Ninject's NamedAttribute . 回答1: You need to use the Autofac.Extras.Attributed package on top to achieve this So let's say you have one interface and two classes: public interface IHello { string SayHello(); } public class EnglishHello : IHello { public string SayHello() { return "Hello"; } } public class FrenchHello : IHello { public string SayHello() { return "Bonjour"; } } Then you have a