当使用Ninject和mvc3时,我们会做类似的事情,安装ninject,registerger modules-or-services.
我们写这样的控制
public class HomeController : Controller
{
private IHelloService _service;
public HomeController(IHelloService service)
{
_service = service;
}
public string Index()
{
return _service.GetGreeting();
}
}
Run Code Online (Sandbox Code Playgroud)
public class HomeController : Controller
{
private IHelloService _service;
/*
No default constructor
*/
public string Index()
{
_service= Ask_Ninject_to_provide_resource
return _service.GetGreeting();
}
}
Run Code Online (Sandbox Code Playgroud) 有哪些方案允许通过IoC创建的服务可以访问IoC控制器以访问其他服务?在我看来,简短的答案永远不会,但是在某些情况下它是否正常甚至是有意义的?
我正在使用Unity IoC框架,并Bootstrapper.cs在我的主机MVC层中有一个类来注册所有组件.然而,在我的建筑我有MVC层下方的"服务"层,即也使用DI并有库接口注入到它(库接口未在MVC层使用-它注入到其控制器的服务层接口) .
所以我的问题如下:我是否仍然可以将存储库接口注册到整个应用程序的MVC/UI层中的具体类型,或者我是否添加对Unity的另一个引用并Bootstrapper.cs在我的"服务"层中创建另一个类来定义接口特定图层使用的类型?
即使答案是我可以在UI层注册界面,我仍然想知道常见的做法.我不喜欢在MVC/UI层中注册该类型的事情是我必须添加对Repository层的引用才能进行注册,甚至知道它不在该层中使用.它用在服务层中.
谢谢!
如何使以下代码有效?它抛出一个错误,说有两个相同名称的元数据属性,但我不明白为什么.
错误消息如下:
System.ComponentModel.Composition.dll中发生未处理的"System.InvalidOperationException"类型异常
附加信息:成员或类型'ConsoleApplication2.DoSomeMagic'包含多个名为'PluginName'的元数据条目.元数据条目可以来自ExportMetadataAttribute,也可以来自自定义元数据属性的属性.删除重复的条目或启用名称为"PluginName"的元数据条目,以允许通过自定义元数据属性上的ExportMetadataAttribute或AttributeUsage.AllowMultiple上的IsMultiple属性进行多个条目.
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.Test();
}
private void Test()
{
//Export
var catalog = new AssemblyCatalog(this.GetType().Assembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
//Import Meta Data
var import1 = container.GetExports<IMagic1, IPluginAttributeView>().Select(e => new PluginAttribute(e.Metadata));
}
}
public interface IPluginAttributeView
{
string PluginName { get; set; }
string PluginConfigurationName { get; set; }
string PluginCategory { get; set; }
Type PluginType { get; set; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple …Run Code Online (Sandbox Code Playgroud) 也许这是一个愚蠢的问题,但我坚持下去.
我试图在整个应用程序中使用SimpleContainer作为IoC,因此在我的数据访问层中,我以这种方式定义了一个引导程序:
public class AppBootstrapper : BootstrapperBase
{
SimpleContainer container;
public AppBootstrapper()
{
Start();
}
protected override void Configure()
{
container = new SimpleContainer();
container.PerRequest<IMyClass, MyClass>();
}
protected override object GetInstance(Type service, string key)
{
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
Run Code Online (Sandbox Code Playgroud)
但是我怎么能用呢?
我只想获得一个实现并尝试编写:
IMyClass mc = new IoC.GetInstance(IMyClass );
Run Code Online (Sandbox Code Playgroud)
但我没有找到怎么样
我试过了:
SimpleContainer container = new SimpleContainer();
IMyClass mc = new container.GetInstance(IMyClass,null);
Run Code Online (Sandbox Code Playgroud)
和:
IMyClass mc = new IoC.GetInstance(IMyClass, …Run Code Online (Sandbox Code Playgroud) 在Github上查看一些源代码时,我注意到一些软件包使用app容器本身来访问IOC,而不是Facades.你为什么要用这样的东西......
$app = app();
$user = $app['db']->connection()->table('users')->where('name', '=', 'Foo')->first();
Run Code Online (Sandbox Code Playgroud)
......而不是这个?
$user = User::where('name', '=', 'Foo')->first();
Run Code Online (Sandbox Code Playgroud) 使用asp.net mvc并通过nuget 安装unity bootstraper.
我想知道如何在bootstraper中配置的"Application_BeginRequest()","Application_Error()","Application_EndRequest()"中访问同一个统一容器?
以及如何在Application_BeginRequest中解析接口的所有实例?
我想我错过了如何实际使用IoC/DI的关键部分.我碰巧使用的是Unity容器.我知道如何设置一个类来注入它的依赖项,我也知道如何使Unity寄存器成为一种类型.
但我不知道的是如何实际使用这些注册.
例如:
var container = new UnityContainer();
container.RegisterType<IRepository, XmlRepository>();
var service = new MyService(container.Resolve<IRepository>());
public interface IRepository
{
void GetStuff();
}
public class XmlRepository : IRepository
{
public void GetStuff()
{
throw new NotImplementedException();
}
}
public class MyService
{
private readonly IRepository _myRepository;
public MyService(IRepository repository)
{
_myRepository = repository;
}
}
Run Code Online (Sandbox Code Playgroud)
这里我有一个服务层,它接受一个类型的参数IRepository.这是我似乎不理解的容器部分.
var service = new MyService(...)正在调用container.Resolve这样做的正确方法吗?c# dependency-injection inversion-of-control unity-container
我想知道你将如何使用打字稿IOC专门节点应用程序.
在基于外部模块的体系结构的情况下,应用程序中没有任何类.只是纯模块,因为我的应用程序在很大程度上取决于node_modules.
在这种情况下,我如何整合IOC解决方案?有什么想法吗?
以下是我希望使用IOC的具体案例:
我有猫鼬模型:
interface IStuffModel extends IStuff, mongoose.Document { }
var Stuff= mongoose.model<IStuffModel>('Stuff', Schemas.stuffSchema);
export = Stuff;
Run Code Online (Sandbox Code Playgroud)
和相关的假类:
export class Stuff implements IStuff {
//do stuff
}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有许多服务,我使用以下模式:在与接口相同的文件中,我定义了一个静态工厂方法,该方法由IoC容器控制,如下所示:
public interface ISomethingService {
Task DoSomethingAsync(int id);
}
public class SomethingServicFactory : ServiceFactory<ISomethingService > { }
public class ServiceFactory<T>
{
public static Func<T> CreateClosure;
public T GetDefault() => CreateClosure();
}
Run Code Online (Sandbox Code Playgroud)
创建和配置IoC容器后:
SomethingServicFactory .CreateClosure = () =>
Container.GetInstance<ISomethingService >();
Run Code Online (Sandbox Code Playgroud)
稍后在我的应用程序中,当我需要SomethingService时:
var somethingService= new SomethingService().GetDefault();
Run Code Online (Sandbox Code Playgroud)
这允许我将创建推迟到最后一刻,但仍然使用容器控制服务创建.我刚开始使用SimpleInjector.更重要的是,它允许我创建服务实例并轻松传递参数,同时控制IoC.
这个模式帮助我的一个很好的例子是WPF XAML实例化的用户控件,它需要填充数据(即数据库中的查找值).在后面的代码中,我能够轻松地创建DbContext并从数据库中获取数据.但是,我也开始在整个应用程序中使用它.
我担心通过使用这种模式我错过了重大的设计/架构问题,我正在寻找IoC专家对此模式的评论.
c# ×7
asp.net-mvc ×2
facade ×1
laravel ×1
mef ×1
metadata ×1
ninject ×1
node-modules ×1
node.js ×1
typescript ×1