)
五.首先看看实体层的定义,我相信代码大家都看得懂就不多说了,直接贴上来。
using System;
using System.Collections.Generic;
using System.Text;
namespace Spring.Demo.Model
{
/// <summary>
/// 用户类
/// </summary>
public class Users
{
/// <summary>
/// 编号
/// </summary>
private int _oid;
public int Oid
{
get { return _oid; }
set { _oid = value; }
}
/// <summary>
/// 姓名
/// </summary>
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// 性别
/// </summary>
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}
/// <summary>
/// 年龄
/// </summary>
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
}
Spring.net(二)----初探IOC容器(3)
时间:2011-02-09 博客园 Beniao
六.在看接口的定义
using System;
using System.Collections.Generic;
using System.Text;
namespace Spring.Demo.Service
{
public interface IUsers
{
/// <summary>
/// 返回用户的详细信息的方法
/// </summary>
/// <returns></returns>
string GetUserInfo();
}
} 七.实现接口方法的逻辑
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Demo.Service;
using Spring.Demo.Model;
namespace Spring.Demo.Compontext
{
public class UsersCompontents:IUsers
{
public UsersCompontents()
{ }
#region 获取用户信息
public string GetUserInfo()
{
Users user = new Users();
user.Oid = 1;
user.Name = "Beniao";
user.Sex = "Boy";
user.Age = 25;
return string.Format("编号:{0}--->姓名:{1}--->性别:{2}--->年龄:{3}",
user.Oid,
user.Name,
user.Sex,
user.Age);
}
#endregion
}
}
八.通过Spring.net的IOC容器IApplicationContext从config文件中取得程序集信息和调用方法实现控制反转
//从config文件中取得程序集信息
IApplicationContext context = ConfigurationManager.GetSection("spring/context")
as IApplicationContext;
//调用方法
//Users为config文件里的配置节
//<object name="Users"
// type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">
//</o
|