unity-container

Dependency Injection and ModelStateWrapper

瘦欲@ 提交于 2020-01-02 08:12:32
问题 in tutorial Validating with a service layer constructor for Product Service looks like this: ProductService(IValidationDictionary validationDictionary, IProductRepository repository) and its instance in default controller constructor is created like this: public ProductController() { _service = new ProductService(new ModelStateWrapper(this.ModelState), new roductRepository()); } If I want to use Unity for DI, second constructor should obviously be used. public ProductController

Dependency Injection and ModelStateWrapper

烂漫一生 提交于 2020-01-02 08:12:08
问题 in tutorial Validating with a service layer constructor for Product Service looks like this: ProductService(IValidationDictionary validationDictionary, IProductRepository repository) and its instance in default controller constructor is created like this: public ProductController() { _service = new ProductService(new ModelStateWrapper(this.ModelState), new roductRepository()); } If I want to use Unity for DI, second constructor should obviously be used. public ProductController

UnityContainer configured in web.config

狂风中的少年 提交于 2020-01-02 07:21:12
问题 I have the following code var container = new UnityContainer(); //LINE 1 container.RegisterType<ILogUtility,LogUtil>(); //LINE 2 var logger = container.Resolve<Logger>(); //LINE 3 logger.Log(LogType.Warn, "logging from container"); //LINE 4 How do I implement line 2 in web.config such that I will only have to code line 1, 3, and 4 in my code behind? I have searched every where for code example but they are not clear. Thanks 回答1: Take a look at my tutorial http://netpl.blogspot.com/2011/11

Register Generic Type in Unity Based On Concrete Type

爷,独闯天下 提交于 2020-01-02 06:29:23
问题 I'm trying to emulate a behavior that I can configure in Ninject, only using Unity instead. I am attempting to use the Cached Repository Pattern, given the following classes and interface: public interface IRepository<T> { T Get(); } public class SqlRepository<T> : IRepository<T> where T : new() { public T Get() { Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name); return new T(); } } public class CachedRepository<T> : IRepository<T> where T : class { private readonly

Dependency on the framework assembly “System.Runtime, Version=4.0.10.0,” which could not be resolved in the currently targeted framework

对着背影说爱祢 提交于 2020-01-02 02:40:28
问题 TFS 2013 - Build: ASP.Net 4.5.1 website I get this error: warning MSB3268: The primary reference "C:\Builds\2\MyProj\Web1_Main\bin\MyProj1.dll" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.5.1". To resolve this problem, either remove the reference "C:\Builds\2\MyProj\Web1_Main

Automapper and request specific resources

北慕城南 提交于 2020-01-01 19:42:11
问题 I'm considering automapper for an asp mvc intranet app I am writing. My controllers are currently created using Unity dependency injection, where each container gets dependencies unique to the request. I need to know if automapper can be made to use a request specific resource ICountryRepository to look up an object, like so.... domainObject.Country = CountryRepository.Load(viewModelObject.CountryCode); 回答1: Couple of options here. One is to do a custom resolver: .ForMember(dest => dest

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 {

Unity Container - Dependency Injection of Dynamic DbContext into Service

帅比萌擦擦* 提交于 2020-01-01 07:12:32
问题 I'm building a .Net Web API which uses a Service+Repository pattern w/ Entity Framework. Each controller's CRUD actions relay data retrieved by calls to a Service. I have a SomeContext that extends DbContext: public class SomeContext : DbContext { public SomeContext(string connString) : base(connString) { } // DbSets ... } The Service is initialized w/ a constructor that accepts an ISomeContext: public class Service : IService { public Service(ISomeContext ctx) : base(ctx) { _alpha = new

How do I implement a delegate factory?

ε祈祈猫儿з 提交于 2020-01-01 05:17:05
问题 The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories. It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? 回答1: Well I've never used Unity so far, so my answer is quite vague. The principal is simple

Unity Resolve Multiple Classes

时光总嘲笑我的痴心妄想 提交于 2020-01-01 04:56:27
问题 How do I get microsoft unity to 'construct' a list of classes for a given interface type. Very Simple example: List<IShippingCalculation> list = new List<IShippingCalculation>(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List<IShippingCalculation> list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity, must i do this