constructor-injection

Can't get value of Ninject ConstructorArgument (passed in as parameter to kernel.Get)

旧巷老猫 提交于 2019-12-20 07:19:40
问题 Having trouble getting the value of a ConstructorArgument parameter passed to kernel.Get(). I want to use the parameter's value to determine which of two string values will be passed into the constructor. The parameter is in fact there when expected, I just can't get at its value. I end up with a null ref exception after calling the parameter's GetValue method: namespace MyNS.DBProviders { public abstract class DBProviderBase { private ObjectContext _db; public DBProviderBase(ObjectContext db

Failure to pass generic arguments with Castle Windsor

允我心安 提交于 2019-12-20 04:11:37
问题 There seems to be an issue with passing generic arguments when attempting to create a parametrized instance with Castle Windsor Demo of Failure to Pass Generic Arguments private static void Main(string[] args) { PassGenericParamAtResolutionTime(); Console.ReadLine(); } private static void PassGenericParamAtResolutionTime() { Console.WriteLine("Passing generic argument fails"); var container = new WindsorContainer(); container.Register(Component.For<ISandCoordinator<Simpleton>>()

StructureMap: Choose concrete type of nested dependency

試著忘記壹切 提交于 2019-12-19 19:51:31
问题 Calculators: public interface ICalculator { int Calculate(int a, int b); } public class Calculator : ICalculator { private readonly ICalculatorStrategy _calculatorStrategy; public Calculator(ICalculatorStrategy calculatorStrategy) { _calculatorStrategy = calculatorStrategy; } public int Calculate(int a, int b) { return _calculatorStrategy.Calculate(a, b); } } Calculator stragies: public interface ICalculatorStrategy { int Calculate(int a, int b); } public class AdditionCalculator :

Multi-tenancy web application with filtered dbContext

≡放荡痞女 提交于 2019-12-18 12:04:29
问题 I am new to ASP.Net MVC and multi-tenancy web application. I have done lots of reading, but being a beginner I just follow what I understand. So I managed to built a sample scenario web application and need to solve the ending part of it. Hope this scenario will be useful for some other beginners as well, but would welcome any other approach. Thanks in advance 1) Database in SQLServer 2008. 2) Data layer: C# class library project called MyApplication.Data public class AppUser { [Key] public

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,

Can I pass constructor parameters to Unity's Resolve() method?

末鹿安然 提交于 2019-12-17 03:05:20
问题 I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2);

Having AutoMapper to inject dependencies using an IoC Container when needed

陌路散爱 提交于 2019-12-14 04:07:17
问题 I have tried almost everything, but I cannot get AutoMapper to map A => B when B doesn't have a parameterless constructor . I'm using Unity and all the dependencies are registered conveniently but, how do I say to AutoMapper "hey, if the target instance needs some dependency in the constructor, ask Unity to build it, and do the mapping later. I've tried with Mapper.Initialize(configuration => { configuration.ConstructServicesUsing(container.Resolve); configuration.CreateMap<Person,

Can Ninject use an anonymous delegate (func) as a ConstructorArgument?

时间秒杀一切 提交于 2019-12-13 10:17:05
问题 I have a repository abstract class that encapsulates pretty much all of the CRUD functionality: public abstract class DataRepository<T> : IRepository<T> where T : class { public DataContext Context { get; private set; } public TransactionScope Transaction { get; private set; } /// <summary> /// A <see cref="bool"/> function that compares the keys for fetching a single item, for example: /// return item1.Id == item2.Id (as an anonymous delegate). /// </summary> public Func<T, T, bool>

Resolution-time arguments of same type in Castle Windsor

≡放荡痞女 提交于 2019-12-11 19:07:18
问题 When I try to pass two parameters that are of the same type like so: public IPercentage CreatePercentage(int part, int total) { return _container.Resolve<T>(new Arguments(part, total)); } To a constructor like so: public Percentage(int part, int total) { // ... } Then I get a System.ArgumentException: An item with the same key has already been added. How can I pass arguments of same type? The key thing is I would like to avoid using literal string names of the parameters to identify which

Is it necessary to check null values with constructor injection?

浪子不回头ぞ 提交于 2019-12-11 13:05:42
问题 I'm using .NET Core constructor injection. In a code review from a colleague, he raised the question if I should check for null values on injected dependencies in controllers. Since the framework is responsible for creating an instance of the service, it seems to me that it would take care of any errors and never have a null value dependency passed to a constructor. I don't have any factual evidence for this though, so I'd like to know if it's possible that a null check may be necessary. For