Example #1
0
            /// <summary>
            /// Registers the specified name.
            /// </summary>
            /// <typeparam name="TService">The type of the service.</typeparam>
            /// <param name="name">The name.</param>
            /// <param name="factory">The factory.</param>
            /// <param name="isAlwaysCreatingNew">if set to <c>true</c> [always new].</param>
            /// <returns>
            /// ServiceLocatorEntryStruct&lt;TService&gt;.
            /// </returns>
            public ServiceLocatorEntryStruct <TService> Register <TService>(string name, Func <object, TService> factory, bool isAlwaysCreatingNew = true)
            {
                name = name ?? "";
                ServiceLocatorEntryStruct <TService> rval;

                if (isAlwaysCreatingNew)
                {
                    ServiceTypedCache <TService> .dic[name] =
                        rval =
                            new ServiceLocatorEntryStruct <TService>(name)
                    {
                        ServiceFactory = factory,
                        CacheType      = CacheType.Factory
                    };
                }
                else
                {
                    ServiceTypedCache <TService> .dic[name] =
                        rval =
                            new ServiceLocatorEntryStruct <TService>(name)
                    {
                        ServiceFactory = factory,
                        CacheType      = CacheType.LazyInstance
                    };
                }
                if (!disposeActions.ContainsKey(typeof(TService)))
                {
                    disposeActions[typeof(TService)] = () => ServiceTypedCache <TService> .dic.Clear();
                }

                return(rval);
            }
Example #2
0
            public ServiceLocatorEntryStruct <TService> Register <TService>(string name, Func <object, Task <TService> > asyncFactory, bool alwaysNew = true)
            {
                name = name ?? "";
                ServiceLocatorEntryStruct <TService> rval;

                if (alwaysNew)
                {
                    ServiceTypedCache <TService> .dic[name] = rval = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType           = CacheType.AsyncFactory,
                        AsyncServiceFactory = asyncFactory
                    };
                }
                else
                {
                    ServiceTypedCache <TService> .dic[name] = rval = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType           = CacheType.AsyncLazyInstance,
                        AsyncServiceFactory = asyncFactory
                    };
                }
                if (!disposeActions.ContainsKey(typeof(TService)))
                {
                    disposeActions[typeof(TService)] = () => ServiceTypedCache <TService> .dic.Clear();
                }
                return(rval);
            }
Example #3
0
 public ServiceLocatorEntryStruct <TService> Register(string name, TService instance)
 {
     name = name ?? "";
     return(dic[name] =
                new ServiceLocatorEntryStruct <TService>(name)
     {
         CacheType = CacheType.Instance,
         ServiceInstance = instance,
     });
 }
Example #4
0
            public bool IsAsync <TService>(string name = "")
            {
                name = name ?? "";
                ServiceLocatorEntryStruct <TService> entry = null;

                if (ServiceTypedCache <TService> .dic.TryGetValue(name, out entry))
                {
                    return
                        (entry.GetIsValueCreated());
                }
                else
                {
                    throw new ArgumentException("No such key");
                }
            }
Example #5
0
            public bool HasInstance <TService>(string name = "")
            {
                name = name ?? "";
                ServiceLocatorEntryStruct <TService> entry = null;

                if (ServiceTypedCache <TService> .dic.TryGetValue(name, out entry))
                {
                    return
                        (entry.GetIsValueCreated());
                }
                else
                {
                    return(false);
                }
            }
Example #6
0
            public TService Resolve <TService>(string name = null, object paremeters = null)
            {
                name = name ?? "";
                var subdic = ServiceTypedCache <TService> .dic;
                ServiceLocatorEntryStruct <TService> entry = null;

                if (subdic.TryGetValue(name, out entry))
                {
                    return(entry.GetService(paremeters));
                }
                else
                {
                    return(default(TService));
                }
            }
Example #7
0
            /// <summary>
            /// resolve as an asynchronous operation.
            /// </summary>
            /// <param name="name">The name.</param>
            /// <param name="parameter">The parameter.</param>
            /// <returns>Task&lt;TService&gt;.</returns>
            public async Task <TService> ResolveAsync(string name = null, object parameter = null)
            {
                name = name ?? "";
                var subdic = dic;
                ServiceLocatorEntryStruct <TService> entry = null;

                if (subdic.TryGetValue(name, out entry))
                {
                    return(await entry.GetServiceAsync());
                }
                else
                {
                    return(await Task.FromResult(default(TService)));
                }
            }
Example #8
0
            public async Task <TService> ResolveAsync <TService>(string name = null, object paremeter = null)
            {
                name = name ?? "";
                var subdic = ServiceTypedCache <TService> .dic;
                ServiceLocatorEntryStruct <TService> entry = null;

                if (subdic.TryGetValue(name, out entry))
                {
                    return(await entry.GetServiceAsync());
                }
                else
                {
                    //#if SILVERLIGHT_5||WINDOWS_PHONE_7
                    //                    return await T.askEx.FromResult(default(TService));
                    //#else
                    //                    return await T.ask.FromResult(default(TService));
                    //#endif
                    return(await TaskExHelper.FromResult(default(TService)));
                }
            }
Example #9
0
            public ServiceLocatorEntryStruct <TService> Register(string name, Func <object, Task <TService> > asyncFactory, bool alwaysNew = true)
            {
                name = name ?? "";

                if (alwaysNew)
                {
                    return(dic[name] = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType = CacheType.AsyncFactory,
                        AsyncServiceFactory = asyncFactory
                    });
                }
                else
                {
                    return(dic[name] = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType = CacheType.AsyncLazyInstance,
                        AsyncServiceFactory = asyncFactory
                    });
                }
            }
Example #10
0
            /// <summary>
            /// Registers the specified name.
            /// </summary>
            /// <param name="name">The name.</param>
            /// <param name="factory">The factory.</param>
            /// <param name="isAlwaysCreatingNew">if set to <c>true</c> [always new].</param>
            /// <returns>ServiceLocatorEntryStruct&lt;TService&gt;.</returns>
            public ServiceLocatorEntryStruct <TService> Register(string name, Func <object, TService> factory, bool isAlwaysCreatingNew = true)
            {
                name = name ?? "";

                if (isAlwaysCreatingNew)
                {
                    return(dic[name] = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType = CacheType.Factory,
                        ServiceFactory = factory
                    });
                }
                else
                {
                    return(dic[name] = new ServiceLocatorEntryStruct <TService>(name)
                    {
                        CacheType = CacheType.LazyInstance,
                        ServiceFactory = factory
                    });
                }
            }