基本结构:
第一步准备实体类:
[Serializable]
public class Person
{
/// <summary>
/// 编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public string Sex { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 爱好
/// </summary>
public string Like { get; set; }
}
第二步:准备测试业务类
public class PersonManager
{
private List<Person> list = new List<Person>();
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
list.Add(new Person { Id = 1, Name = "Jok bose", Sex = "男", Age = 22, Like = "打篮球" });
list.Add(new Person { Id = 2, Name = "Aaron", Sex = "男", Age = 23, Like = "踢足球" });
list.Add(new Person { Id = 3, Name = "Abelard", Sex = "女", Age = 23, Like = "乒乓球" });
list.Add(new Person { Id = 4, Name = "Acton", Sex = "男", Age = 23, Like = "网球" });
list.Add(new Person { Id = 5, Name = "Broderick", Sex = "女", Age = 23, Like = "橄榄球" });
list.Add(new Person { Id = 6, Name = "Buckley", Sex = "男", Age = 23, Like = "踢足球" });
list.Add(new Person { Id = 7, Name = "urne", Sex = "女", Age = 23, Like = "棒球" });
list.Add(new Person { Id = 8, Name = "Calder", Sex = "男", Age = 23, Like = "游泳" });
list.Add(new Person { Id = 9, Name = "Caldwell", Sex = "女", Age = 23, Like = "赛车" });
list.Add(new Person { Id = 10, Name = "Calhoun", Sex = "男", Age = 23, Like = "旅游" });
list.Add(new Person { Id = 11, Name = "Calvert", Sex = "女", Age = 23, Like = "交朋友" });
list.Add(new Person { Id = 12, Name = "Carleton", Sex = "男", Age = 23, Like = "做好事" });
list.Add(new Person { Id = 13, Name = "Carlisle", Sex = "女", Age = 23, Like = "喝茶" });
}
/// <summary>
/// 查询所有
/// </summary>
/// <returns></returns>
public IList<Person> GetAll()
{
return list;
}
/// <summary>
/// 根据编号查询
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Person GetPersonById(int id)
{
return list.Find(delegate(Person obj) {
return obj.Id == id;
});
}
/// <summary>
/// 根据姓名查询
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Person GetPersonByName(string name)
{
return list.Find(delegate(Person obj) {
return obj.Name == name;
});
}
/// <summary>
/// 根据年龄查询所有的人
/// </summary>
/// <param name="age"></param>
/// <returns></returns>
public List<Person> SelectAge(int age)
{
return list.FindAll(delegate(Person p) {
return p.Age == age;
});
}
}
第三步:准备控制器类
[HandleError]
public class HomeController : Controller
{
private PersonManager personManager;
public PersonManager PersonManager
{
set { personManager = value; }
}
public ActionResult Index()
{
personManager.Init();
ViewData["list"] = personManager.GetAll();
return View();
}
}
第四步:配置web.config
<configuration>
......
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
.....
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="Level" value="ALL"/>
</factoryAdapter>
</logging>
</common>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<!--Controller注入-->
<object id="Home" type="SpringNetIntegrationMVC.Controllers.HomeController,SpringNetIntegrationMVC" singleton="false">
<property name="PersonManager" ref="psersonManager"/>
</object>
<!--简单业务类-->
<object id="psersonManager" type="BLL.PersonManager,BLL"/>
</objects>
</spring>
......
</configSections>
第五步:交接控制器类的生成权
在asp.net mvc中控制器类是有DefaultControllerFactory这个默认工厂来产生对象
可以利用这一点,我们来拓展这个工厂
具体类如下:
public class SpringControllerFactory : DefaultControllerFactory
{
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(SpringControllerFactory));
private static DefaultControllerFactory defaultControllerFactory = null;
#region IControllerFactory 成员
public override IController CreateController(RequestContext requestContext, string controllerName)
{
if (requestContext==null)
{
throw new ArgumentNullException("requestContext");
}
if (String.IsNullOrEmpty(controllerName))
{
throw new ArgumentException("controllerName");
}
RequestContext = requestContext;
IApplicationContext ctx = ContextRegistry.GetContext();
if (ctx.ContainsObject(controllerName))
{
return ctx.GetObject(controllerName) as IController;
}
else
{
if (defaultControllerFactory == null)
{
defaultControllerFactory = new DefaultControllerFactory();
}
return defaultControllerFactory.CreateController(requestContext, controllerName);
}
}
//public override void ReleaseController(IController controller)
//{
// IApplicationContext ctx = ContextRegistry.GetContext();
// if (!ctx.ContainsObject(controller.GetType().Name.Replace("Controller", "")))
// {
// if (defaultControllerFactory == null)
// {
// defaultControllerFactory = new DefaultControllerFactory();
// }
// defaultControllerFactory.ReleaseController(controller);
// }
//}
#endregion
}
第六步:在全局应用程序中做个小修改
将拓展方法注入到mvc中
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(typeof(Common.SpringControllerFactory));
RegisterRoutes(RouteTable.Routes);
}
第七步:在控制器类中添加注入属性
private PersonManager personManager;
public PersonManager PersonManager
{
set { personManager = value; }
}
第八步:需要特别注意的地方
在spring.net的ioc容器中配置控制器类的时候,id或者name 必须与控制器类的名字一致,例如:控制器类叫做:HomeController
那么在Ioc容器中的Id="Home"
一切搞定
呵呵
讲的不详细,如果有什么问题的可以咨询本人,qq:332453111