我现在已经实现了动态创建对象,但是被人质疑设计有问题,所以才来反思的 至于多注册可以这样: builder.RegisterType<OrtherPlatformGroupPriceService>().Named<IGroupPriceService>("OrtherPlatform") builder.RegisterType<OrtherPlatformGroupPriceService>().Keyed<IGroupPriceService>(PlatformType.Windows)谢谢拉,查了一下文档,是有可以根据Named和Keyed来区分~ var service = container.ResolveKeyed<IGroupPriceService>(PlatformType.Windows);//可以来失去不同的事例了 也达到创建不同的对象了~ 虽然这里不是有点像工厂~但很不错啊~
MVC主要关注职责的分离,IoC主要关注依赖关系,
如果设计者能很好的贯彻SOLID原则,也就无所谓这些所谓模式
而依赖查找经常是通过一个所谓的IoC容器实现,
大致的意思就是容器提供一套API,负责注册所有的提供者组件,向使用者动态提供组件,
其实.net本身就具备这个能力,
如果第三方的容器在性能上没有优势,就没有太大的实际意义
在情况A下使用短信实现A,在情况B下使用短信实现B看过Autofac的例子,当一个接口注册两个实现的时候,是会覆盖的~
感觉是在程序开始运行的时候就要运行注册的了~ 所以....好难啊~
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); //依赖注入
DependencyResolver.SetResolver(new AutofacBuilder().DependencyResolver);
}如果找到方案了,告诉我
至于多注册可以这样:
builder.RegisterType<OrtherPlatformGroupPriceService>().Named<IGroupPriceService>("OrtherPlatform")
builder.RegisterType<OrtherPlatformGroupPriceService>().Keyed<IGroupPriceService>(PlatformType.Windows)
至于多注册可以这样:
builder.RegisterType<OrtherPlatformGroupPriceService>().Named<IGroupPriceService>("OrtherPlatform")
builder.RegisterType<OrtherPlatformGroupPriceService>().Keyed<IGroupPriceService>(PlatformType.Windows)谢谢拉,查了一下文档,是有可以根据Named和Keyed来区分~
var service = container.ResolveKeyed<IGroupPriceService>(PlatformType.Windows);//可以来失去不同的事例了
也达到创建不同的对象了~
虽然这里不是有点像工厂~但很不错啊~