예제 #1
0
        /// <summary>
        /// Register service factory
        /// </summary>
        /// <typeparam name="TFactory">factory of services</typeparam>
        /// <typeparam name="TService">type of services</typeparam>
        /// <param name="container">container of dependencies</param>
        /// <param name="lifestyle">Lifestyle</param>
        /// <param name="patternSuffix">pattern suffix</param>
        /// <param name="callback">callback for product key</param>
        /// <returns></returns>
        public static TFactory RegisterFactory <TFactory, TService>(Container container, Lifestyle lifestyle, string patternSuffix, ProductName callback)
            where TService : class
            where TFactory : Dictionary <string, Func <TService> >
        {
            var factory = Activator.CreateInstance <TFactory>();
            var registrationsServiceFacade =
                from type in typeof(TFactory).Assembly.GetExportedTypes()
                where type.Name.EndsWith(patternSuffix)
                where !type.IsInterface
                where type.GetInterfaces().Any()
                where type.GetInterfaces().Contains(typeof(TService))
                select new
            {
                Implementation = type,
                ProductName    = callback != null?callback.Invoke(type) : type.Name
            };

            foreach (var reg in registrationsServiceFacade)
            {
                factory.Add(reg.ProductName, () => (TService)container.GetInstance(reg.Implementation));
            }
            return(factory);
        }