Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

c# - How to register All types of an interface and get instance of them in unity?

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();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...