标签:两种 routes 自动 反转 设计原则 实现 tty not array
原因:
+ 若高层依赖于底层,那么底层的变动也会导致高层的变动,这就会导致模块的复用性降低而且大大提高了开发的成本。
+ 若是依赖于抽象的话,那么是比较稳定的,底层或者高层的变动都不会互相影响
控制反转背后的核心思想是, 我们不再将类绑定在应用里,让类自己去 "new up" 他们的依赖, 而是反过来在类的构造方法中将依赖传递进去
寻找到相关实例,将其赋给接口对象。
public class SomeType : IService
{
}
在代码中可以通过两种方法访问该类型(SomeType):
SomeType
IService
组件 指的是 SomeType
而它暴露的 服务 指的是 SomeType
和 IService
.
public HomeController(IGradeService gradeService)
{
_gradeService = gradeService;
}
public ActionResult Index()
{
var model = _gradeService.GetAllGrade().ToList();
return View(model);
}
public void Application_Start()
{
...
//依赖注入
var builder = new ContainerBuilder();
builder.RegisterType().As().InstancePerHttpRequest();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AsImplementedInterfaces();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
namespace MvcApp5.Models
{
public interface IDependency
{
}
}
public class HomeController : Controller
{
private readonly IModelCar _carmodel;
private readonly IModelUser _usermodel;
public HomeController(IModelCar carmodel,IModelUser usermodel)
{
_carmodel = carmodel;
_usermodel = usermodel;
}
public ActionResult Index()
{
ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";
var carname=_carmodel.GetCarName();
var username = _usermodel.GetUserName();
return View();
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
var builder=RegisterService();
DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
private ContainerBuilder RegisterService()
{
var builder = new ContainerBuilder();
var baseType=typeof(IDependency);
var assemblys=AppDomain.CurrentDomain.GetAssemblies().ToList();
var AllServices = assemblys
.SelectMany(s => s.GetTypes())
.Where(p => baseType.IsAssignableFrom(p)&&p!=baseType);
builder.RegisterControllers(assemblys.ToArray());
builder.RegisterAssemblyTypes(assemblys.ToArray())
.Where(t =>baseType.IsAssignableFrom(t)&&t!=baseType)
.AsImplementedInterfaces().InstancePerLifetimeScope();
return builder;
}
}
public class ValuesController : ApiController
{
private readonly IUserService _UserService;
public ValuesController(IUserService UserService)
{
_UserService = UserService;
}
public IHttpActionResult Get(string name)
{
string str = _UserService.Set(name);
return Ok(str);
}
}
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
#region 自动注入
? ? ? ? ? ? //创建autofac管理注册类的容器实例
? ? ? ? ? ? var builder = new ContainerBuilder();
HttpConfiguration config = GlobalConfiguration.Configuration;
Assembly[] assemblies = Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath, "*.dll").Select(Assembly.LoadFrom).ToArray();
? ? ? ? ? ? //注册所有实现了 IDependency 接口的类型
? ? ? ? ? ? Type baseType = typeof(IDependency);
builder.RegisterAssemblyTypes(assemblies)
.Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
.AsSelf().AsImplementedInterfaces()
.PropertiesAutowired().InstancePerLifetimeScope();
? ? ? ? ? ? //注册MVC类型
? ? ? ? ? ? // builder.RegisterControllers(assemblies).PropertiesAutowired();
? ? ? ? ? ? //注册Api类型
? ? ? ? ? ? builder.RegisterApiControllers(assemblies).PropertiesAutowired();
? ? ? ? ? ? //builder.RegisterFilterProvider();
? ? ? ? ? ? builder.RegisterWebApiFilterProvider(config);
var container = builder.Build();
? ? ? ? ? ? //注册api容器需要使用HttpConfiguration对象
? ? ? ? ? ? config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
? ? ? ? ? ? //注册解析
? ? ? ? ? ? DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
? ? ? ? ? ? #endregion
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
标签:两种 routes 自动 反转 设计原则 实现 tty not array
原文地址:https://www.cnblogs.com/weiyongguang/p/12142044.html