English 中文(简体)
结构图的统一
原标题:Unity to Structure Map

我正试图从这个职位上删除以下文字:。 1. 驱散建筑 (方式令人非常感兴趣)。 他的IOC集装箱是团结的,我希望利用结构图这样做。

他的法典是:

public class EventSubscriptions : ISubscriptionService
{
   public static void Add<T>()
   {
       var consumerType = typeof(T);

       consumerType.GetInterfaces()
                   .Where(x => x.IsGenericType)
                   .Where(x => x.GetGenericTypeDefinition() == typeof(IConsumer<>))
                   .ToList()
                   .ForEach(x => IoC.Container.RegisterType(x, 
                                                            consumerType, 
                                                            consumerType.FullName));
   }

   public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
   {
       var consumers =  IoC.Container.ResolveAll(typeof(IConsumer<T>));
       return consumers.Cast<IConsumer<T>>();
   }
}

我看来没有做以下工作:

public class SubscriptionService : ISubscriptionService
{
    public static void Add<T>()
    {
        var consumerType = typeof(T);

        consumerType.GetInterfaces()
            .Where(x => x.IsGenericType)
            .Where(x => x.GetGenericTypeDefinition() == typeof (IConsumer<>))
            .ToList().ForEach(x => ObjectFactory.Inject(consumerType, x));                                  
    }

    public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
    {
        var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>));
        return consumers.Cast<IConsumer<T>>();
    }
}

我显然不熟悉结构图。 如果我错做些什么,将真的受到赞赏。

<>Update:

从Henning的回答来看,我最后说:

public class SubscriptionService : ISubscriptionService
{
    public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
    {
        var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>));
        return consumers.Cast<IConsumer<T>>();
    }
}

然后,在我要求申请的间歇班上,我开始:

public static void ConfigureStuctureMap()
        {
            ObjectFactory.Initialize(x =>
            {       
                x.Scan(y =>
                {
                    y.Assembly("Domain");           
                    y.Assembly("Website");
                    y.AddAllTypesOf(typeof(IConsumer<>));
                    y.WithDefaultConventions();
                });
            });
        }
最佳回答
问题回答

建设采用ITypeScanner界面的定制类型扫描仪。

public class EventSubConventionScanner : ITypeScanner
{
    public void Process(Type type, PluginGraph graph)
    {
        Type interfaceType = type.FindInterfaceThatCloses(typeof(IConsumer<>));

        if (interfaceType != null)
        {
            graph.AddType(interfaceType, type);
        }
    }
}

例行写字后、登记或初步写成:

 Scan(x =>
        {
            x.With<EventSubConventionScanner>();
        });




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签