using Autofac;
using System.Reflection;

namespace QYZH.InteractiveMagazine.Infrastructure.Autofacs
{
    public class AutofacModuleRegister : Autofac.Module
    {
        private readonly string _assemblyName;
        public AutofacModuleRegister(string assemblyNames)
        {
            _assemblyName = assemblyNames;
        }

        /// <summary>
        /// 加在程序集
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            //注册Service
            builder.RegisterAssemblyTypes(GetAssemblyByName($"{_assemblyName}.Service")).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();
        }

        /// <summary>
        /// 根据程序集名称获取程序集
        /// </summary>
        /// <param name="AssemblyName">程序集名称</param>
        public static Assembly GetAssemblyByName(string AssemblyName)
        {
            return Assembly.Load(AssemblyName);
        }
    }
}
