autofac

.NET Core下自带容器IServiceCollection以及AutoFac以及AutoFac中AOP简介

匿名 (未验证) 提交于 2019-12-02 22:09:29
https://www.cnblogs.com/artech/p/net-core-di-01.html 大内老A的在.NET Core下对这些的介绍,有一系列文章 https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html https://www.zybuluo.com/dasajia2lang/note/1481011 下面开始 在上一篇的笔记中,在.NET Freamwork中,有一个第三方容器Unity,可以实现注入,但是在.NET Core里面,有一个IServiceCollection,这个是.NET Core框架自带的一个容器,和Unity很相似,都是个容器。 下面我们新建一个控制台程序,在控制台程序中,对IServiceCollection的使用做介绍。 下面代码,是本次实例中需要注入的类型,需要用的倒是再点开来看吧 namespace Bingle . Core . Interface { public interface ITestServiceA { void Show (); } } namespace Bingle . Core . Service {

How to register many for open generic in Autofac

久未见 提交于 2019-12-02 22:08:31
I'm new to Autofac (not to DI ). Here is the situation: I have these interfaces: public interface IQuery<out TResult> : IQuery { } public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult> { TResult Handle(TQuery query); } and there is a lot of implementation of them in my solution: class GetPersonQuery : IQuery<PersonModel> { } class GetPersonQueryHandler : IQueryHandler<GetPersonQuery, PersonModel> { } class GetArticleQuery : IQuery<ArticleModel> { } class GetArticleQueryHandler : IQueryHandler<GetArticleQuery, ArticleModel> { } class GetSomethingQuery : IQuery

用Autofac替换.net core 内置容器

匿名 (未验证) 提交于 2019-12-02 22:06:11
官方建议使用内置容器,但有些功能并不支持,如下: 属性注入 基于名称的注入 子容器 自定义生存期管理 Func<T> 支持 所以可以使用其他第三方IOC容器,如Autofac,下面为学习使用记录 一、首先准备了一个接口和其实现类 public interface ITestService { string ShowMsg(); } public class TestService: ITestService { public string ShowMsg() { return "test123"; } } 二、安装Nuget 包 Autofac Autofac.Extensions.DependencyInjection 三、在 Startup.ConfigureServices 中配置容器 注:使用第三方容器,Startup.ConfigureServices 必须返回 IServiceProvider。    第一种方式,使用AutofacModule配置文件,原来代码修改为: public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //

Using AutoFac

匿名 (未验证) 提交于 2019-12-02 22:06:11
第一次整理了下关于autofac的一些具体的用法 2. 创建两个类库项目,IService (用于编写接口),ServiceImpl(用于创建实现类) IService 下 public interface IAnimalBark { void Bark(); } public interface IAnimalSleep { void Sleep(); } public interface IUser { void AddNew(string name, string pwd); } public interface ISchool { void AfterSchool(); } ServiceImpl下 public class Dog : IAnimalBark,IAnimalSleep { public void Bark() { Console.WriteLine("汪汪汪汪汪"); } public void Sleep() { Console.WriteLine("zZ,睡着了"); } } public class Cat : IAnimalBark { public void Bark() { Console.WriteLine("喵喵喵"); } } public class User : IUser { public void AddNew(string

从零开始搭建.NET Core 2.0 API(学习笔记一)

匿名 (未验证) 提交于 2019-12-02 22:06:11
一、 VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2.0版本 运行项目 http://localhost:prot/swagger 即可打开帮助页。 public void Configure(IApplicationBuilder app, IHostingEnvironment env) {   if (env.IsDevelopment())   {     app.UseDeveloperExceptionPage();     app.UseBrowserLink();   }   else   {     app.UseExceptionHandler("/Home/Error");   }   app.UseStaticFiles(); app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings => {   settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase; }); app.UseMvc(route => {   route.MapRoute(name: "default",

ASP.NET MVC IOC 之AutoFac攻略

匿名 (未验证) 提交于 2019-12-02 22:06:11
一、环境及优点介绍: 开发环境 :vs2017(.Net Framework 4.5) 数据库 :MySQL(选择原因是占用资源少) : SqlSuger (是由园中大神孙凯旋写的,向大神致敬!膜拜! 博客园 , 官网 ) : LayUI (扁平化设计,个人体验(看着舒服、用着方便 模块化开发)向团队致敬) Web :MVC5 Ioc : autofac (推荐博客: NET领域最为流行的IOC框架之一Autofac , ASP.NET MVC IOC 之AutoFac攻略 等) 优点:性能高、可扩展、灵活性好、安全性(数据安全,即使是本地后台取数据,也有安全过滤)、代码规范、开发维护简单、面向插件编程、功能(模块)独立 二、整体框架介绍: 先来张图(项目架构): 框架中各个模块介绍 01:Client Client:主要负责客户端,上图建的意义是多个项目可以放到一起,但又不冲突,方便维护管理(MVC 空项目:个人喜欢干净不喜欢冗余的一些代码) 02PluginServices 2.1:ClientsPlugin 2.1.1AdminPlugin:实现插件功能 2.1.2BasePlugin:过滤插件安全等 2.1.3IAdminPlugin:插件契约 2.2: InterPlugin 这里是针对WebAPI 插件 负责get/post 调用插件 2.3: WCFPlugin 2

第二节:框架前期准备篇之AutoFac常见用法总结

匿名 (未验证) 提交于 2019-12-02 22:06:11
一. 说在前面的话   在框架搭建过程中,在层与层的解耦方面,势必会涉及到IOC框架,.Net 平台下我用过的IOC框架主要是: Spring.Net 、Unity、AutoFac,当然还有Castle(我没用过,就不发表任何评论了), 在用过的IOC框架中,Spring.Net 相对很老了,貌似在2015年就不在更新了,但基本的功能也够用了。 现阶段用的最多的就是Unity和AutoFac了,版本更新也比较快,Unity大约一年前写过两篇文章了,本次在该框架系列也会考虑更新一下Unity,本节主要介绍一下AutoFac的几个基本用法。   先说一下两个概念IOC和DI,我的理解:   ① IOC:调用者不再创建(不自己new)被调用者的实例,而是交给容器去创建(AutoFac就充当这里的容器),这就是控制反转。   ② DI:容器创建好的实例再注入调用者的过程,就是依赖注入(比如:属性注入、构造函数注入等)。 AutoFac的信息:   ② 官方文档:http://autofac.readthedocs.io/en/latest/index.html   ③ 最新版本:4.8.1 (截止2018-08-21)   本节的内容主要包括:     1. 在使用IOC框架之前的几种创建对象的方式。     2. AutoFac的基本用法和几种生命周期。     3.

ASP.net Identity, IoC and sharing DbContext

半世苍凉 提交于 2019-12-02 21:25:17
Have anyone been successful in attaching an IoC with OWIN ASP.NET Identity to share the same DbContext as the WebApi (or MVC) application? I would like that if ASP.Net identity loads the user, it loads in the same dbcontext as used in the rest of the application. (using AutoFac as IoC - but wouldn't mind an example with other container) Edit : 06/Jan/2014: Just to be a bit more specific, when the ASP.Net identity attempts to use a DbContext it needs to be the same DbContext instance as the rest of the application. Otherwise it will create two instances of the same DbContext. I am already using

In Autofac how do I change the instance that is registered after Build has been called?

回眸只為那壹抹淺笑 提交于 2019-12-02 20:14:34
So lets say i have this code var builder = new ContainerBuilder(); builder.RegisterInstance(new MyType()); var container = builder.Build(); Then some time later I want to change the instance of MyType for all future resolves that are called on container . At the time you want to change the registration, create a new ContainerBuilder , register the new instance, and call Update passing in the container: // at some later point... builder = new ContainerBuilder(); builder.RegisterInstance(myType2); builder.Update(container); An alternative could be to register a delegate that is able to change

Dynamic menu creation IoC

我们两清 提交于 2019-12-02 19:45:38
问题 I am wondering if anyone out there knows how I could create how could i use something like AutoFac to let me dynamically allow dll's to create there own forms and menu items to call them at run time. So if I have an, Employee.dll New Starter Form Certificate Form Supplier.dll Supplier Detail From Products Form In my winform app it would create a menu with this and when each one clicked load the relavent form up People New Starter Certificate Supplier Supplier Details Products So I can add a