淺析依附注入框架Autofac的應用。本站提示廣大學習愛好者:(淺析依附注入框架Autofac的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析依附注入框架Autofac的應用正文
上面經由過程代碼給年夜家分享下依附注入框架Autofac的應用,詳細以下所示:
Autofac是一款IOC框架,比擬於其他的IOC框架,如Spring.NET,Unity,Castle等等所包括的,它很輕量級機能上也是很高的。
1)解壓它的緊縮包,重要看到Autofac.dll,Autofac.Configuration.dll,這也是本篇文章重點應用的Autofac的類庫。
2)創立一個掌握台工程,而且援用以上的DLL文件。創立一個數據庫操作接口IDatabase.cs:
/// <summary> /// Database operate interface /// </summary> public interface IDatabase { string Name { get; } void Select(string commandText); void Insert(string commandText); void Update(string commandText); void Delete(string commandText); }
這裡包括CRUD四種操作的辦法。
3)創立兩種數據庫的操作類,SqlDatabase.cs和OracleDatabase.cs:
public class SqlDatabase : IDatabase { public string Name { get { return "sqlserver"; } } public void Select(string commandText) { Console.WriteLine(string.Format("'{0}' is a query sql in {1}!", commandText, Name)); } public void Insert(string commandText) { Console.WriteLine(string.Format("'{0}' is a insert sql in {1}!", commandText, Name)); } public void Update(string commandText) { Console.WriteLine(string.Format("'{0}' is a update sql in {1}!", commandText, Name)); } public void Delete(string commandText) { Console.WriteLine(string.Format("'{0}' is a delete sql in {1}!", commandText, Name)); } }
和
public class OracleDatabase : IDatabase { public string Name { get { return "oracle"; } } public void Select(string commandText) { Console.WriteLine(string.Format("'{0}' is a query sql in {1}!", commandText, Name)); } public void Insert(string commandText) { Console.WriteLine(string.Format("'{0}' is a insert sql in {1}!", commandText, Name)); } public void Update(string commandText) { Console.WriteLine(string.Format("'{0}' is a update sql in {1}!", commandText, Name)); } public void Delete(string commandText) { Console.WriteLine(string.Format("'{0}' is a delete sql in {1}!", commandText, Name)); } }
4)接著創立一個數據庫治理器DatabaseManager.cs:
public class DatabaseManager { IDatabase _database; public DatabaseManager(IDatabase database) { _database = database; } public void Search(string commandText) { _database.Select(commandText); } public void Add(string commandText) { _database.Insert(commandText); } public void Save(string commandText) { _database.Update(commandText); } public void Remove(string commandText) { _database.Delete(commandText); } }
5)在掌握台中,編寫以下測試法式:
var builder = new ContainerBuilder();
builder.RegisterType<DatabaseManager>();
builder.RegisterType<SqlDatabase>().As<IDatabase>();
using (var container = builder.Build())
{
var manager = container.Resolve<DatabaseManager>();
manager.Search("SELECT * FORM USER");
}
運轉成果:
剖析:
這裡經由過程ContainerBuilder辦法RegisterType對DatabaseManager停止注冊,當注冊的類型在響應獲得的容器中可以Resolve你的DatabaseManager實例。
builder.RegisterType<SqlDatabase>().As<IDatabase>();
經由過程AS可讓DatabaseManager類中經由過程結構函數依附注入類型響應的接口。
Build()辦法生成一個對應的Container實例,如許,便可以經由過程Resolve解析到注冊的類型實例。
異樣地,假如你修正數據庫類型注冊為:
builder.RegisterType<OracleDatabase>().As<IDatabase>();
運轉成果:
6)明顯以上的法式中,SqlDatabase或許OracleDatabase曾經裸露於客戶法式中了,如今我想將該類型選擇經由過程文件設置裝備擺設停止讀取。
Autofac自帶了一個Autofac.Configuration.dll 異常便利地對類型停止設置裝備擺設,防止了法式的從新編譯。
修正App.config:
<configuration>
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>
<autofac defaultAssembly="AutofacDemo">
<components>
<component type="AutofacDemo.SqlDatabase, AutofacDemo" service="AutofacDemo.IDatabase" />
</components>
</autofac>
</configuration>
經由過程Autofac.Configuration.SectionHandler設置裝備擺設節點對組件停止處置。
對應的客戶端法式改成:
var builder = new ContainerBuilder();
builder.RegisterType<DatabaseManager>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
using (var container = builder.Build())
{
var manager = container.Resolve<DatabaseManager>();
manager.Search("SELECT * FORM USER");
}
運轉成果:
7)別的還有一種方法,經由過程Register辦法停止注冊:
var builder = new ContainerBuilder(); //builder.RegisterType<DatabaseManager>(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>())); using (var container = builder.Build()) { var manager = container.Resolve<DatabaseManager>(); manager.Search("SELECT * FORM USER"); }
獲得成果也是一樣的。
8)如今我想經由過程一個用戶類來掌握操作權限,好比增刪改的權限,創立一個用戶類:
/// <summary> /// Id Identity Interface /// </summary> public interface Identity { int Id { get; set; } } public class User : Identity { public int Id { get; set; } public string Name { get; set; } }
修正DatabaseManager.cs代碼:
public class DatabaseManager { IDatabase _database; User _user; public DatabaseManager(IDatabase database) : this(database, null) { } public DatabaseManager(IDatabase database, User user) { _database = database; _user = user; } /// <summary> /// Check Authority /// </summary> /// <returns></returns> public bool IsAuthority() { bool result = _user != null && _user.Id == 1 && _user.Name == "leepy" ? true : false; if (!result) Console.WriteLine("Not authority!"); return result; } public void Search(string commandText) { _database.Select(commandText); } public void Add(string commandText) { if (IsAuthority()) _database.Insert(commandText); } public void Save(string commandText) { if (IsAuthority()) _database.Update(commandText); } public void Remove(string commandText) { if (IsAuthority()) _database.Delete(commandText); } }
在結構函數中增長了一個參數User,而Add,Save,Remove增長了權限斷定。
修正客戶端法式:
User user = new User { Id = 1, Name = "leepy" }; var builder = new ContainerBuilder(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.RegisterInstance(user).As<User>(); builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>(), c.Resolve<User>())); using (var container = builder.Build()) { var manager = container.Resolve<DatabaseManager>(); manager.Add("INSERT INTO USER ..."); }
運轉成果:
剖析:
builder.RegisterInstance(user).As<User>();注冊User實例。
builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>(), c.Resolve<User>()));經由過程Lampda表達式注冊
DatabaseManager實例。
假如這裡我修正User的屬性值:
User user = new User { Id = 2, Name = "zhangsan" };
運轉成果:
解釋該用戶無權限操作。
以上就是本文的全體內容,願望年夜家愛好。