开发者

Registry equivalent in Unity

Is 开发者_开发知识库there any equivalent to the Registry class from StructureMap in Unity?

I like to think about a layer/component/library to configure it self - thus populating the container. So the "parent" layer will just need to know about the Registration class.


No, there isn't. In our current project we have manually mimic'd the concept of a Registry although our implementation isn't nearly as powerful as a StructureMap Registry.

If all you are wanting is modularized container configuration, what you could do is create a marker interface (maybe IRegistry) and then have your application scan for IRegistry classes. With Unity, you can resolve classes that haven't been registered into Unity yet, so you could simply resolve each IRegistry implementation as you find it. The registry classes could take the container as a constructor parameter and then each class could configure the container as needed for that layer/component/library.

public interface IRegistry 
{ 
    void Configure();
}

public class ServicesRegistry : IRegistry
{
    public ServicesRegistry(IUnityContainer container)
    {
        _container = container;
    }

    public sub Configure()
    {
        // Configure container for layer
    }
}

Now in your app startup somewhere you could have an application bootstrapper that either knows about all your registries or knows how to scan for them.


You can try UnityConfiguration which is a convention based configuration API for the Unity IoC container, heavily influenced by StructureMap

https://github.com/thedersen/UnityConfiguration

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜