今天我就给大家讲讲我在项目中遇到的一个问题(即泛型类的注入)。首先看看代码,郁闷得很,搞了一早上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Spring.Context;
using Spring.Context.Support;
namespace Dao
{
public class Common<T>
{
public delegate List<T> ItemList( string hql);
/// <summary>
/// 获取下拉列表的项
/// </summary>
/// <param name="hql">传递的hql语句</param>
/// <param name="dataValueField">下拉列表Value值</param>
/// <param name="dataTextField">下拉列表文本值</param>
/// <param name="optionLabel">下拉列表默认值</param>
/// <param name="GetItemlist">委托返回list泛型</param>
/// <returns>下拉列表项</returns>
public SelectList GetListItem( string hql, string dataValueField, string dataTextField, string optionLabel, ItemList GetItemlist)
{
IEnumerable<T> iEnumerable = GetItemlist(hql).AsEnumerable();
SelectList selectList = new SelectList(iEnumerable, dataValueField, dataTextField, optionLabel);
return selectList;
}
/// <summary>
/// 根据Id获取spring容器加载的对象
/// </summary>
/// <param name="objId">对象映射Id</param>
/// <returns></returns>
public object GetClass( string objId)
{
IApplicationContext context = ContextRegistry.GetContext();
return context.GetObject(objId);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Spring.Context;
using Spring.Context.Support;
namespace Dao
{
public class Common<T>
{
public delegate List<T> ItemList( string hql);
/// <summary>
/// 获取下拉列表的项
/// </summary>
/// <param name="hql">传递的hql语句</param>
/// <param name="dataValueField">下拉列表Value值</param>
/// <param name="dataTextField">下拉列表文本值</param>
/// <param name="optionLabel">下拉列表默认值</param>
/// <param name="GetItemlist">委托返回list泛型</param>
/// <returns>下拉列表项</returns>
public SelectList GetListItem( string hql, string dataValueField, string dataTextField, string optionLabel, ItemList GetItemlist)
{
IEnumerable<T> iEnumerable = GetItemlist(hql).AsEnumerable();
SelectList selectList = new SelectList(iEnumerable, dataValueField, dataTextField, optionLabel);
return selectList;
}
/// <summary>
/// 根据Id获取spring容器加载的对象
/// </summary>
/// <param name="objId">对象映射Id</param>
/// <returns></returns>
public object GetClass( string objId)
{
IApplicationContext context = ContextRegistry.GetContext();
return context.GetObject(objId);
}
}
}
这个是我写的公共类Common.cs。再看看我的Controller调用
public
class CodeController : BaseController<SS_CODE>
{
private SS_CODE ssCode { set; get; }
private Common<SS_CODE> common { set; get; }
public ActionResult Index()
{
try
{
// Common<SS_CODE> common = new Common<SS_CODE>();
SS_CodeDao sc = common.GetClass( "CodeDao") as SS_CodeDao;
SelectList list = common.GetListItem( " from SS_CODE", "C_DATA", "C_ENAME", "---选择---", sc.Select);
ViewData.Add( "items", list);
return View( "~/Views/System/User/UserManager.aspx");
}
catch (Exception e)
{
ILog lg = log4net.LogManager.GetLogger( "WebLogger");
{
if (lg.IsErrorEnabled)
{
lg.Error( this.GetType().ToString() + e.Message);
}
}
return View();
}
}
}
{
private SS_CODE ssCode { set; get; }
private Common<SS_CODE> common { set; get; }
public ActionResult Index()
{
try
{
// Common<SS_CODE> common = new Common<SS_CODE>();
SS_CodeDao sc = common.GetClass( "CodeDao") as SS_CodeDao;
SelectList list = common.GetListItem( " from SS_CODE", "C_DATA", "C_ENAME", "---选择---", sc.Select);
ViewData.Add( "items", list);
return View( "~/Views/System/User/UserManager.aspx");
}
catch (Exception e)
{
ILog lg = log4net.LogManager.GetLogger( "WebLogger");
{
if (lg.IsErrorEnabled)
{
lg.Error( this.GetType().ToString() + e.Message);
}
}
return View();
}
}
}
我们再看看xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
< object id ="ssCode" type ="Model.SS_CODE,Model" >
< property name ="C_DATA" value ="2" > </ property >
</ object >
< object id ="CodeController" type ="Controllers.CodeController,NormalUniversity" singleton ="false" autowire ="byName" >
< property name ="ssCode" ref ="ssCode" > </ property >
< property name ="common" ref ="common" > </ property >
</ object >
< object id ="common" type="Dao.Common <Model.SS_CODE >,Dao">
</ object >
</ objects >
< objects xmlns ="http://www.springframework.net"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
< object id ="ssCode" type ="Model.SS_CODE,Model" >
< property name ="C_DATA" value ="2" > </ property >
</ object >
< object id ="CodeController" type ="Controllers.CodeController,NormalUniversity" singleton ="false" autowire ="byName" >
< property name ="ssCode" ref ="ssCode" > </ property >
< property name ="common" ref ="common" > </ property >
</ object >
< object id ="common" type="Dao.Common <Model.SS_CODE >,Dao">
</ object >
</ objects >
这里我将common泛型类注入到controller。注意xml文件的最后一个object,本来泛型要写成Dao.Common<Model.SS_CODE>但是xml会把第一个<翻译成小于号。因此这里需要用<代替。
最后我很郁闷的是这样一种写法报错,我始终没搞明白。大家帮我看看。如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Spring.Context;
using Spring.Context.Support;
namespace Dao
{
public class Common
{
public delegate List<dynamic> ItemList( string hql);
/// <summary>
/// 获取下拉列表的项
/// </summary>
/// <param name="hql">传递的hql语句</param>
/// <param name="dataValueField">下拉列表Value值</param>
/// <param name="dataTextField">下拉列表文本值</param>
/// <param name="optionLabel">下拉列表默认值</param>
/// <param name="GetItemlist">委托返回list泛型</param>
/// <returns>下拉列表项</returns>
public SelectList GetListItem( string hql, string dataValueField, string dataTextField, string optionLabel, ItemList GetItemlist)
{
IEnumerable<dynamic> iEnumerable = GetItemlist(hql).AsEnumerable();
SelectList selectList = new SelectList(iEnumerable, dataValueField, dataTextField, optionLabel);
return selectList;
}
/// <summary>
/// 根据Id获取spring容器加载的对象
/// </summary>
/// <param name="objId">对象映射Id</param>
/// <returns></returns>
public object GetClass( string objId)
{
IApplicationContext context = ContextRegistry.GetContext();
return context.GetObject(objId);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Spring.Context;
using Spring.Context.Support;
namespace Dao
{
public class Common
{
public delegate List<dynamic> ItemList( string hql);
/// <summary>
/// 获取下拉列表的项
/// </summary>
/// <param name="hql">传递的hql语句</param>
/// <param name="dataValueField">下拉列表Value值</param>
/// <param name="dataTextField">下拉列表文本值</param>
/// <param name="optionLabel">下拉列表默认值</param>
/// <param name="GetItemlist">委托返回list泛型</param>
/// <returns>下拉列表项</returns>
public SelectList GetListItem( string hql, string dataValueField, string dataTextField, string optionLabel, ItemList GetItemlist)
{
IEnumerable<dynamic> iEnumerable = GetItemlist(hql).AsEnumerable();
SelectList selectList = new SelectList(iEnumerable, dataValueField, dataTextField, optionLabel);
return selectList;
}
/// <summary>
/// 根据Id获取spring容器加载的对象
/// </summary>
/// <param name="objId">对象映射Id</param>
/// <returns></returns>
public object GetClass( string objId)
{
IApplicationContext context = ContextRegistry.GetContext();
return context.GetObject(objId);
}
}
}
SS_CodeDao sc = common.GetClass(
"CodeDao")
as SS_CodeDao;
SelectList list = common.GetListItem( " from SS_CODE", "C_DATA", "C_ENAME", "---选择---", sc.Select);
SelectList list = common.GetListItem( " from SS_CODE", "C_DATA", "C_ENAME", "---选择---", sc.Select);
xml也配置无误。再调用的话就会报错,sc.select报错返回类型不正确。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/328874,如需转载请自行联系原作者