示例#1
2
        private static void ConfigureContainer()
        {
            Container = new Container();

            // Basic test first
            Container.Register<IClient, TestClient>();

            // Singletons can be set up using Reuse - demo this by getting an instance, updating value, then resolving a new instance
            Container.Register<ISingletonClient, SingletonClient>(Reuse.Singleton);

            // Registering with a primitive type to be passed to constructor
            Container.Register<IServiceWithParameter, ServiceWithParameter>(Made.Of(() => new ServiceWithParameter(Arg.Index<string>(0)), requestIgnored => "this is the parameter"));

            // Then registering a complex object instance to be used
            Container.Register<TestObject>();
            var testObj = new TestObject
            {
                ObjectName = "Ian",
                ObjectType = "Person"
            };
            // Register the instance above (into the container) - giving it an Id ("serviceKey") = "obj1"
            Container.RegisterInstance<TestObject>(testObj, serviceKey: "obj1");
            // Register ServiceWithTypeParameter - saying "When you make me a ServiceWithTypeParameter; and a contructor needs a TestObject - use the one with Id "obj1"
            Container.Register<IServiceWithTypeParameter, ServiceWithTypeParameter>(made: Parameters.Of.Type<TestObject>(serviceKey: "obj1"));

            // And finally multiple implementations
            // Registering multiple interface implementations using an enum key
            Container.Register<IMultipleImplementations, MultipleImplementationOne>(serviceKey: InterfaceKey.FirstKey);
            Container.Register<IMultipleImplementations, MultipleImplementationTwo>(serviceKey: InterfaceKey.SecondKey);
        }
 public ServiceWithTypeParameter(TestObject param)
 {
     _paramObject = param;
 }
 public ServiceWithTypeParameter(TestObject param)
 {
     _paramObject = param;
 }