我有来自相同接口的服务
public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }
public class ServiceC : IService { }
Run Code Online (Sandbox Code Playgroud)
通常,其他IOC容器Unity允许您通过某些Key区分它们来注册具体实现.
在Asp.Net Core中,我如何注册这些服务并在运行时根据某些键解决它?
我没有看到任何通常用于区分具体实现的AddService方法key或name参数.
public void ConfigureServices(IServiceCollection services)
{
// How do I register services here of the same interface
}
public MyController:Controller
{
public void DoSomeThing(string key)
{
// How do get service based on key
}
}
Run Code Online (Sandbox Code Playgroud)
工厂模式是唯一的选择吗?
Update1
我已经阅读了这里 …
(与此问题相关,EF4:为什么在启用延迟加载时必须启用代理创建?).
我是DI的新手,所以请耐心等待.我知道容器负责实例化我所有已注册的类型,但为了做到这一点,它需要引用我的解决方案中的所有DLL及其引用.
如果我没有使用DI容器,我就不必在我的MVC3应用程序中引用EntityFramework库,只需引用我的业务层,它将引用我的DAL/Repo层.
我知道在一天结束时所有的DLL都包含在bin文件夹中,但我的问题是必须通过VS中的"添加引用"显式引用它,以便能够发布包含所有必需文件的WAP.
我正在探索依赖注入,并且在整个地方使用术语组合根.那是什么?
我熟悉这些模式,但仍然不知道如何处理以下情况:
public class CarFactory
{
public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6)
{
}
public ICar CreateCar(type)
{
switch(type)
{
case A:
return new Car1(Dep1,Dep2,Dep3);
break;
case B:
return new Car2(Dep4,Dep5,Dep6);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
通常,问题在于需要注入的引用量.当有更多的汽车时会更糟.
我想到的第一种方法是在工厂构造函数中注入Car1和Car2,但它违反工厂方法,因为工厂将始终返回相同的对象.第二种方法是注入servicelocator,但它的反模式到处都是.怎么解决?
替代方式1:
public class CarFactory
{
public CarFactory(IContainer container)
{
_container = container;
}
public ICar CreateCar(type)
{
switch(type)
{
case A:
return _container.Resolve<ICar1>();
break;
case B:
return _container.Resolve<ICar2>();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
替代方式2(由于树中的依赖性过多而难以使用):
public class CarFactory
{
public CarFactory()
{
}
public ICar CreateCar(type)
{
switch(type)
{ …Run Code Online (Sandbox Code Playgroud) c# dependency-injection inversion-of-control factory-pattern
我有两个示例类
class ClassToResolve
{
private List<CollectionItem> _coll;
public ClassToResolve(List<CollectionItem> coll)
{
_coll = coll;
}
}
class CollectionItem
{
//...
}
Run Code Online (Sandbox Code Playgroud)
我需要解决ClassToResolve
var classToResolve = new ClassToResolve(
new List<CollectionItem>()
{
new CollectionItem(),
new CollectionItem(),
new CollectionItem()
}
);
Run Code Online (Sandbox Code Playgroud)
现在我以某种方式解决它
var classToResolve = new ClassToResolve(
new List<CollectionItem>()
{
unity.Resolve<CollectionItem>(),
unity.Resolve<CollectionItem>(),
unity.Resolve<CollectionItem>()
}
);
Run Code Online (Sandbox Code Playgroud)
有没有办法使用动态注册解析ClassToResolve?
.net c# dependency-injection inversion-of-control unity-container
我期待这个例子来了解工厂模式的使用.
我真的很喜欢这个领域,所以原谅我的愚蠢问题.
我的问题是我没有看到工厂模式的使用,它返回了我们可以在需要使用它时直接注入它的接口.
在上面的例子中,我会做这样的事情:
public class Program
{
// register the interfaces with DI container in a separate config class (Unity in this case)
private readonly IShippingStrategy _shippingStrategy;
public Program(IShippingStrategy shippingStrategy)
{
_shippingStrategy= shippingStrategy;
}
public int DoTheWork(Order order)
{
// assign properties just as an example
order.ShippingMethod = "Fedex";
order.OrderTotal = 90;
order.OrderWeight = 12;
order.OrderZipCode = 98109;
int shippingCost = _shippingStrategy.CalculateShippingCost(order);
return shippingCost;
}
}
Run Code Online (Sandbox Code Playgroud)
而不是注射工厂:
public class Program
{
// register the interfaces with DI …Run Code Online (Sandbox Code Playgroud) 我正在为我的Web api项目使用简单的注入器。我有一个需要会话令牌才能实例化的服务。
public class CustomerService
{
public CustomerService(Auth auth, IRepositoryFactory repositoryFactory)
{
// make post call to another web api for validation
SomeWebApiCallToValidateAuth.vaildate(auth);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,对于此服务,它需要一个auth令牌和一个repositoryFactory。我希望它能够注入auth参数(来自http Web请求),并同时使用注册到容器的指定已实现多数民众赞成解决存储库工厂。
但是我不确定如何在简单的注射器上注册它,或者是否有解决方法。任何帮助都会很棒。谢谢。