如何注册所有类型的接口,团结让他们的实例? [英] How to register All types of an interface and get instance of them in unity?

查看:226
本文介绍了如何注册所有类型的接口,团结让他们的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何团结可以得到一个接口的所有实例,然后访问它们。



代码段采取从这里:的故障跟踪



在StrcutureMap它可以从一个程序集注册的所有类型的接口然后访问他们像下面这样:

 公共类TaskRegistry:注册表
{
公共TaskRegistry()
{
扫描(扫描=>
{
scan.AssembliesFromApplicationBaseDirectory(
A => a.FullName.StartsWith(FailTracker));
scan.AddAllTypesOf< IRunAtInit>();
scan.AddAllTypesOf< IRunAtStartup>();
scan.AddAllTypesOf< IRunOnEachRequest>();
scan.AddAllTypesOf< IRunOnError>();
scan.AddAllTypesOf< IRunAfterEachRequest>();
});
}
}


ObjectFactory.Configure(CFG =>
{

cfg.AddRegistry(新TaskRegistry( ));

});



,然后访问所有类型的实施像那些接口:

 使用(VAR容器= ObjectFactory.Container.GetNestedContainer())
{
的foreach(在container.GetAllInstances< VAR任务; IRunAtInit>())
{
task.Execute();
}

的foreach(在container.GetAllInstances< VAR任务; IRunAtStartup>())
{
task.Execute();
}
}



这是什么代码团结相当于<? / p>

我怎样才能得到这些在的Application_BeginRequest像structuremap

 公共无效的Application_BeginRequest ()
{
集装箱= ObjectFactory.Container.GetNestedContainer();

的foreach(在Container.GetAllInstances< VAR任务; IRunOnEachRequest>())
{
task.Execute();
}
}


解决方案

团结3加入注册按照惯例,以做大规模注册。



此外,统一注册了一个未命名的映射和许多命名映射的概念。调用解决()或其重载之一时,不愿透露姓名的映射将得到解决。所有命名的映射(而不是无名映射)将调用ResolveAll()或其重载之一时得到解决。

  //有每个参数RegisterTypes()
//(你可以提供自己的自定义选项)
container.RegisterTypes(
AllClasses.FromLoadedAssemblies()
其他选项凡(类型=> typeof运算(IRunOnEachRequest).IsAssignableFrom(类型)),
WithMappings.FromAllInterfaces,
WithName.TypeName,
WithLifetime.Transient);

的foreach(在container.ResolveAll< VAR任务; IRunOnEachRequest>())
task.Execute();


How unity can get all instances of an interface and then access them?

Code pieces are taken from here : Fail-Tracker

In StrcutureMap its possible to register all types of an interface from an assembly and then access them like following:

public class TaskRegistry : Registry
{
    public TaskRegistry()
    {
        Scan(scan =>
        {
            scan.AssembliesFromApplicationBaseDirectory(
                a => a.FullName.StartsWith("FailTracker"));
            scan.AddAllTypesOf<IRunAtInit>();
            scan.AddAllTypesOf<IRunAtStartup>();
            scan.AddAllTypesOf<IRunOnEachRequest>();
            scan.AddAllTypesOf<IRunOnError>();
            scan.AddAllTypesOf<IRunAfterEachRequest>();
        });
    }
}


  ObjectFactory.Configure(cfg =>
        {

            cfg.AddRegistry(new TaskRegistry());

        });

and then access all types implementing those interfaces like:

        using (var container = ObjectFactory.Container.GetNestedContainer())
        {
            foreach (var task in container.GetAllInstances<IRunAtInit>())
            {
                task.Execute();
            }

            foreach (var task in container.GetAllInstances<IRunAtStartup>())
            {
                task.Execute();
            }
        }

What is the equivalent of this code in unity?

How can i get these at Application_BeginRequest like structuremap

public void Application_BeginRequest()
    {
        Container = ObjectFactory.Container.GetNestedContainer();

        foreach (var task in Container.GetAllInstances<IRunOnEachRequest>())
        {
            task.Execute();
        }
    }

解决方案

Unity 3 added registration by convention to do the mass registration.

Also, Unity has the concept of registering an unnamed mapping and many named mappings. The unnamed mapping will be resolved when calling Resolve() or one of its overloads. All of the named mappings (and not the unnamed mapping) will be resolved when calling ResolveAll() or one of its overloads.

// There's other options for each parameter to RegisterTypes()
// (and you can supply your own custom options)
container.RegisterTypes(
    AllClasses.FromLoadedAssemblies().
        Where(type => typeof(IRunOnEachRequest).IsAssignableFrom(type)),
    WithMappings.FromAllInterfaces,
    WithName.TypeName,
    WithLifetime.Transient);

foreach (var task in container.ResolveAll<IRunOnEachRequest>())
    task.Execute();

这篇关于如何注册所有类型的接口,团结让他们的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆