#眉標=Enterprise Library #副標=設計模型套件系列(12) #大標=Policy Injection Application Block(下) #作者=文/圖 王寧疆 #引言= ==<反灰>=========== System.Windows.Forms System.Management System.Configuration System.Security ================ ==<反灰>=========== using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Installers; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using System.Configuration.Install; using Microsoft.Practices.EnterpriseLibrary.PolicyInjection; using System.Security.Principal; using System.Threading; using System.Data.SqlClient; ================ ==<反灰>=========== Model m = PolicyInjection.Create(); /建立PIAB欲控制的目標物件 DataTable dt=m.GetProducts(int.Parse(txtSupplierID.Text)); //呼叫物件的GetProducts方法 GridView1.DataSource = dt; //將GetProducts方法傳回的DataTable交由GridView控制項顯示 GridView1.DataBind(); //命令GridView控制項顯示DataTable的內容 ================ ==<反灰>=========== protected void btnInstall_Click(object sender, EventArgs e) { PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource()); //建立PerformanceCountersInstaller類別的物件 IDictionary state = new System.Collections.Hashtable(); //建立Hashtable型態的集合 installer.Context = new InstallContext(); //建立InstallContext類別的物件 installer.Install(state); //安裝所需要的效能計數器 installer.Commit(state); //確認安裝 lbMessage.Text="效能計數器安裝成功!"; } protected void btnUninstall_Click(object sender, EventArgs e) { PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource()); //建立PerformanceCountersInstaller類別的物件 installer.Context = new InstallContext(); //建立InstallContext類別的物件 installer.Uninstall(null); //解除所安裝的效能計數器 lbMessage.Text="效能計數器解除安裝成功!"; } ================ ==<反灰>=========== Model m = PolicyInjection.Create(); //建立PIAB欲控制的目標物件 try { m.UpdateProduct(int.Parse(txtUpdateProductID.Text), txtProductName.Text, decimal.Parse(txtUnitPrice.Text)); //呼叫物件的UpdateProduct方法 } catch (Exception ex) { Label1.Text = ex.Message; } ================ ==<反灰>=========== Model m = PolicyInjection.Create(); try { m.DeleteProduct(int.Parse(txtProductID.Text)); //呼叫物件的DeleteProduct方法 } catch (ApplicationException ex) //處理型態為ApplicationException類別的例外 { lbError.Text = ex.Message; //將例外資訊顯示在網頁上的Label控制項 } catch (Exception ex) //處理型態為Exception類別的例外 { lbError.Text = "使用者未具備刪除資料庫記錄的權限!"; } ================ ==<反灰>=========== GenericIdentity MyIdentity = new GenericIdentity("John"); //依據使用者名稱建立GenericIdentity類別的物件 string[] MyStringArray = {"Clerk"}; //準備使用者角色的名稱 GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray); //指定使用者的角色 Thread.CurrentPrincipal = MyPrincipal; //改變目前執行緒的執行者身份 ================ ==<反灰>=========== string[] MyStringArray = {"Clerk"}; ================ ==<反灰>=========== string[] MyStringArray = {"Manager"}; ================ ==<反灰>=========== DELETE 陳述式與 REFERENCE 條件約束 "FK_Order_Details_Products" 衝突。衝突發生在資料庫 "Northwind",資料表 "dbo.Order Details", column 'ProductID'。陳述式已經結束。 ================ ==<反灰>=========== public class Model {   [CachingCallHandler(0, 0, 20)] public DataTable GetProducts(int SupplierID) { //code } } ================ ==<反灰>=========== public interface ICustomerInterface { DataTable GetCustomerList(); } ================ ==<反灰>=========== public class InterfaceCustomerModel : ICustomerInterface { public DataTable GetCustomerList() { //code } } ================ ==<反灰>=========== ICustomerInterface customers = PolicyInjection.Create(); ================