所谓“依懒注入”
<!-- google_ad_client = "pub-6628499292856412"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; //2006-12-02: petshop google_ad_channel = "8929656665"; google_color_border = "4C4C4C"; google_color_bg = "FFFFFF"; google_color_link = "000033"; google_color_text = "000000"; google_color_url = "4C4C4C"; //-->
//
就是将应用程序所依赖的组件在运行时动态的加载到应用
程序中。依赖注入的目标并非为应用程序添加更多的功能,而是提升了组件的
重用性,并为应用程序搭建一个具有较强灵活性和扩展性的平台。在程序实现上就用
Assembly.Load(AssemblyName).CreateInstance(typeName)在程序运行时动态创建实例.PetShop4.0中多次应用了与配置文件相强合的反射注入技术。下面就介绍一下反射注入技术在PetShop4.0多层应用程序和应用。
程序中。依赖注入的目标并非为应用程序添加更多的功能,而是提升了组件的
重用性,并为应用程序搭建一个具有较强灵活性和扩展性的平台。在程序实现上就用
Assembly.Load(AssemblyName).CreateInstance(typeName)在程序运行时动态创建实例.PetShop4.0中多次应用了与配置文件相强合的反射注入技术。下面就介绍一下反射注入技术在PetShop4.0多层应用程序和应用。
<!-- google_ad_client = "pub-6628499292856412"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; //2006-12-02: petshop google_ad_channel = "8929656665"; google_color_border = "4C4C4C"; google_color_bg = "FFFFFF"; google_color_link = "000033"; google_color_text = "000000"; google_color_url = "4C4C4C"; //-->
项目名
|
PetShop.CacheDependencyFactory
| ||
类名
|
DependencyAccess
|
方法
|
privatestaticIPetShopCacheDependency LoadInstance(string className)
|
配置文件中的值
|
<appSetting>
……….
<add key="CacheDependencyAssembly" value="PetShop.TableCacheDependency"/>
……
</appSetting>
| ||
代码文件中的实现
|
string path=ConfigurationManager.AppSettings["CacheDependencyAssembly"];
string fullyQualifiedClass = path + "." + className;
return (IPetShopCacheDependency)Assembly.Load(path)
.CreateInstance(fullyQualifiedClass);
| ||
从上面的实现来看其实是很简单的。三个步骤就实现了:两前个步骤,主要是为动态创建
实例提供参数:程序集名称,和类名称(类型名).
1. 执行ConfigurationManager.AppSettings[“keyName”]来获取配置文件中的键值,也就
程序集的名称AssemblyName。
2. 组合类名称typeName。
3. 执行Assembly.Load(AssemblyName).CreateInstance(typeName)进行动太创建实例
其实PetShop4.0中实现反射注入技术都是上面的那种思想。所以下面就省去配置文件中的
信息和实现的代码实现信息,只是统计反射注入技术在PetShop4.0中应用的位置信息。
|
项目名
|
PetShop.DALFactory
|
类名
|
DataAccess
|
方法
|
1. public static PetShop.IDAL.ICategory CreateCategory()
2. public static PetShop.IDAL.IInventory CreateInventory()
3. public static PetShop.IDAL.IItem CreateItem()
4. public static PetShop.IDAL.IOrder CreateOrder()
5. public static PetShop.IDAL.IProduct CreateProduct()
|
项目名
|
PetShop.MessagingFactory
|
类名
|
QueueAccess
|
方法
|
public static PetShop.IMessaging.IOrder CreateOrder()
|
项目名
|
PetShop.ProfileDALFactory
|
类名
|
DataAccess
|
方法
|
Publicstatic
PetShop.IProfileDAL.IPetShopProfileProvider
CreatePetShopProfileProvider()
|
项目名
|
PetShop.ProfileDALFactory
|
类名
|
DataAccess
|
方法
|
Publicstatic
PetShop.IProfileDAL.IPetShopProfileProvider
CreatePetShopProfileProvider()
|
<style type="text/css">
td { font-size: 12px }
.commentTextBox
{
font-family : Verdana;
font-size: 13px;
}</style>
<!--Beging Temp Save--><style type="text/css">
.userData { BEHAVIOR: url(#default#userdata) }
</style>
function pageLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}
function handleInitializeRequest(sender, args)
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
var eid = args.get_postBackElement().id;
if (eid.indexOf("DeleteLink")>0)
{
args.get_postBackElement().innerHTML = "正在删除...";
}
if (eid.indexOf("btnSubmit")>0)
{
document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
}
}
function TempSave(ElementID)
{
try
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
catch(ex)
{
}
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
<!--Ene TempSave-->