dependency-injection

How to use Dependency Injection and not Service Locator

瘦欲@ 提交于 2019-12-18 07:02:32
问题 I am hearing people say you should not use Service Locator for your Dependency Injection. So how exactly do you inject the dependencies without relying on a service locator? I want to try out IoC containers, but don't want to land into an anti-pattern. Should you just set everything up so there is one place where all classes always have a dependency chain to the deepest classes? (if I/that makes sense at all) I isn't right to have all your code littered with dependencies on the IoC container

How to inject Predicate and Func in Spring.net

元气小坏坏 提交于 2019-12-18 06:59:49
问题 I want to create an object with a constructor containing predicate and func objects in the xml config using spring. The Predicate and the Func arguments should point to a method of another configured object. How is this possible using Spring.net? I was not able to find a solution or hint in the documentation... A sample constructor would be: MyClass(Predicate<TInput> condition, Func<TInput, TOutput> result) 回答1: It is also possible to use the DelegateFactoryObject within Spring.net to create

Carrying per-request context using the HttpRequestMessage.Properties

寵の児 提交于 2019-12-18 06:14:22
问题 In a Web API application, I use Castle Windsor to supply services configured with PerWebRequest lifetime and everything works fine on IIS. However, when I use the ASP.NET Web API Self Host (Beta) package I need to create a custom lifetime in order to scope those services per HTTP request. How can I carry per-request context using the HttpRequestMessage.Properties? 回答1: I'd suggest you using a message handler to set some your object into HttpRequestMessage.Property: public class MyApplication

Strategy within Spring boot

≡放荡痞女 提交于 2019-12-18 06:04:18
问题 Hi I have a strategy pattern in a spring boot application. All my strategies have autowired constructors. I am new to spring boot. I do not have a simplest of idea how am I going to write my factory for strategy classes as autowired constructors have injected dependencies. I appreciate any help I get with this. NOTE: I am leaving out out Intefaces and base classes to not to clutter sample. public class StrategyA implement Strategy { private DependencyA depA; private DependencyB depB;

Strategy within Spring boot

萝らか妹 提交于 2019-12-18 06:04:09
问题 Hi I have a strategy pattern in a spring boot application. All my strategies have autowired constructors. I am new to spring boot. I do not have a simplest of idea how am I going to write my factory for strategy classes as autowired constructors have injected dependencies. I appreciate any help I get with this. NOTE: I am leaving out out Intefaces and base classes to not to clutter sample. public class StrategyA implement Strategy { private DependencyA depA; private DependencyB depB;

Unity: Change default lifetime manager for implicit registrations and/or disable them

喜欢而已 提交于 2019-12-18 05:45:06
问题 The Unity container will automatically resolve any type that it can figure out on its own without the need for manual registration. That's good in some ways, but the problem I have is that it uses a TransientLifetimeManager for this type of resolution, while I almost always want a ContainerControlledLifetimeManager . I can still register my types as singletons manually, of course, but if I forget, instead of getting an unhandled exception at startup, the app will launch successfully and

Cannot create an instance of custom ViewModel

孤人 提交于 2019-12-18 05:42:17
问题 I am using dagger2 library. whenever I am trying to run my project is says not able to create instance of view model class. main activity where I am trying to create an instance ((MovieApplication) getApplication()).getAppComponent().inject(this); mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class); My factory class public class ViewModelFactory implements ViewModelProvider.Factory { private MoviesDataViewModel mViewModel; @Inject public ViewModelFactory

How to use Ninject Conventions extension without referencing Assembly (or Types within it)

痞子三分冷 提交于 2019-12-18 05:36:30
问题 Sorry in advance for the long question, it's long because I've been digging at this all day. The general problem: I have an ASP.Net MVC2 application with the following projects: MyApp.Web, MyApp.Services, MyApp.Data. We code to interfaces and utilize Ninject 2 for DI/IoC. However, I'm getting awfully tired of typing (and forgetting to type): Bind<ISomeService>.To<SomeService>; So, knowing about Ninject.Extensions.Convensions, I have attempted to use it to automatically scan and register

Strange behaviour with StructureMap / ASP.MVC / Visual Studio / LinqToSql

☆樱花仙子☆ 提交于 2019-12-18 05:07:05
问题 I have been using the new MVC framework with StructureMap recently and have had good results overall, however, I keep running into a very strange error that I cannot understand or work out how to resolve. This is my architecture: DBContext - linqToSql data context. IRepository - contract defining data methods. IService - contract defining service methods. Controllers - two in this example. I therefore have: public class Repo : IRepository { public Repo(DBContext db) { ..... } } public class

Constructor injection of a View Model instance used as an Action method parameter

百般思念 提交于 2019-12-18 04:52:47
问题 When a view model is created you can populate the options (e.g. used in a dropdown list) into a setter property of the view model. The problem is that when that view model is later passed as a parameter (by the framework!) into an action method, those property values has not become automagically repopulated, so if you need to redisplay the form because of validation errors, you need to repopulate those options again. One potential solution, which I am asking for specifically in this question,