Esempio n. 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 void RegisterDependencies(Container container)
        {
            //http context
            container.RegisterInstance<HttpContextBase>(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, new SingletonReuse());

            //cache provider
            container.Register<ICacheProvider, HttpCacheProvider>(reuse: Reuse.Singleton);

            // settings register for access across app
            container.Register<IDatabaseSettings>(made: Made.Of(() => new DatabaseSettings()), reuse: Reuse.Singleton);

            //data provider : TODO: Use settings to determine the support for other providers
            container.Register<IDatabaseProvider>(made: Made.Of(() => new SqlServerDatabaseProvider()), reuse: Reuse.Singleton);

            //database context
            container.Register<IDatabaseContext>(made: Made.Of(() => DatabaseContextManager.GetDatabaseContext()), reuse: Reuse.Singleton);

            //and respositories
            container.Register(typeof(IDataRepository<>), typeof(EntityRepository<>));

            var asm = AssemblyLoader.LoadBinDirectoryAssemblies();

            //services
            //to register services, we need to get all types from services assembly and register each of them;
            var serviceAssembly = asm.First(x => x.FullName.Contains("mobSocial.Services"));
            var serviceTypes = serviceAssembly.GetTypes().
                Where(type => type.IsPublic && // get public types
                              !type.IsAbstract && // which are not interfaces nor abstract
                              type.GetInterfaces().Length != 0);// which implementing some interface(s)

            container.RegisterMany(serviceTypes, reuse: Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Replace);

            //we need a trasient reporter service rather than singleton
            container.Register<IVerboseReporterService, VerboseReporterService>(reuse: Reuse.InResolutionScope, ifAlreadyRegistered:IfAlreadyRegistered.Replace);

            //settings
            var allSettingTypes = TypeFinder.ClassesOfType<ISettingGroup>();
            foreach (var settingType in allSettingTypes)
            {
                var type = settingType;
                container.RegisterDelegate(type, resolver =>
                {
                    var instance = (ISettingGroup) Activator.CreateInstance(type);
                    resolver.Resolve<ISettingService>().LoadSettings(instance);
                    return instance;
                }, reuse: Reuse.Singleton);

            }
            //and ofcourse the page generator
            container.Register<IPageGenerator, PageGenerator>(reuse: Reuse.Singleton);

            //event publishers and consumers
            container.Register<IEventPublisherService, EventPublisherService>(reuse: Reuse.Singleton);
            //all consumers which are not interfaces
            container.RegisterMany(new[] {typeof(IEventConsumer<>)}, serviceTypeCondition: type => !type.IsInterface);
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs the service provider around the given context.
        /// </summary>
        /// <param name="context">The system context in which this service provider
        /// will be constructed around.</param>
        public SystemServiceProvider(ISystemContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            SystemContext = context;

            container = new DryIoc.Container();
            container.RegisterInstance<ISystemContext>(context);

            resolveContexts = new List<IServiceResolveContext>();
        }
Esempio n. 4
0
        private static IMediator BuildMediator()
        {
            var container = new Container();

            container.RegisterDelegate<SingleInstanceFactory>(r => serviceType => r.Resolve(serviceType));
            container.RegisterDelegate<MultiInstanceFactory>(r => serviceType => r.ResolveMany(serviceType));
            container.RegisterInstance(Console.Out);

            container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, type => type.GetTypeInfo().IsInterface);

            return container.Resolve<IMediator>();
        }
        /// <summary>
        ///     Initialisiert den Bootloader
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static Container Init(Container container)
        {

            GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "extra");
            client.Connect();

            container.RegisterInstance(client);

            RegisterDaos(container);
            RegisterServices(container);

            return container;
        }
Esempio n. 6
0
        void Configure(IAppBuilder app_)
        {
            var config = new HttpConfiguration();

            config.MapHttpAttributeRoutes();

            var di = new DryIoc.Container();

            di.RegisterInstance <IBackStore>(new BackStore(), Reuse.Singleton);

            di.WithWebApi(config);
            app_.UseDryIocOwinMiddleware(di);

            app_.UseWebApi(config);
        }