控制反转
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[ASP.NET Core Web]]
&color(red){※This article is based on .NET 7};
#contents
控制反转是一种思想,依赖注入是一种设计模式,控制反转的思...
* 概要 [#t07feb1c]
控制反转 (Inversion of Control) ,简称IoC,是面向对象编程...
在传统的程序开发中,开发人员往往会手动去new一个对象,换言...
* IoC容器的例程 [#n6a7208c]
下面就来编写一个简单的IoC容器,主要分为IoC、Controller、S...
** Model [#e46a11a7]
#codeprettify{{
namespace App.Model
{
public class UserInfo
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
}}
** Repository [#v558e0dc]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Repository
{
public interface IUserInfoRepository
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System.Collections.Generic;
using App.Model;
namespace App.Repository
{
public class UserInfoRepository : IUserInfoRepository
{
public IEnumerable<UserInfo> GetUserInfoList()
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo { UserName = "super", P...
list.Add(new UserInfo { UserName = "admin", P...
return list;
}
}
}
}}
** Service [#k08c2ac9]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Service
{
public interface IUserInfoService
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System;
using System.Collections.Generic;
using App.Model;
using App.Repository;
namespace App.Service
{
public class UserInfoService : IUserInfoService
{
private readonly IUserInfoRepository repository;
public UserInfoService(IUserInfoRepository reposi...
{
this.repository = repository;
}
public IEnumerable<UserInfo> GetUserInfoList()
{
return repository.GetUserInfoList();
}
}
}
}}
** Controller [#vd08d092]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Controller
{
public interface IUserInfoController
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System.Collections.Generic;
using App.Model;
using App.Service;
namespace App.Controller
{
public class UserInfoController : IUserInfoController
{
private readonly IUserInfoService service;
public UserInfoController(IUserInfoService service)
{
this.service = service;
}
public IEnumerable<UserInfo> GetUserInfoList()
{
return service.GetUserInfoList();
}
}
}
}}
** IoC [#xde9dee5]
#codeprettify{{
using System;
using System.Collections.Generic;
using System.Reflection;
namespace App.IoC
{
public class SimpleIocContainer
{
private readonly Dictionary<string, Type> contain...
/// <summary>
/// 构造函数
/// </summary>
public SimpleIocContainer()
{
container = new Dictionary<string, Type>();
}
/// <summary>
/// 注册类型
/// </summary>
/// <typeparam name="TSuper">超类</typeparam>
/// <typeparam name="TSub">子类</typeparam>
public void RegisterType<TSuper, TSub>()
where TSuper : class
where TSub : class
{
string fullName = typeof(TSuper).FullName;
if (!container.ContainsKey(fullName))
{
container.Add(fullName, typeof(TSub));
}
}
/// <summary>
/// 获取需要的类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>() where T : class
{
string fullName = typeof(T).FullName;
if (!container.ContainsKey(fullName))
{
throw new Exception("Error");
}
return (T)CreateObject(container[fullName]);
}
/// <summary>
/// 递归创建对象
/// </summary>
/// <param name="type">子类类型</param>
/// <returns>object</returns>
private object CreateObject(Type type)
{
ConstructorInfo[] constructors = type.GetCons...
ConstructorInfo constructor = GetConstructor(...
List<object> list = new List<object>();
foreach (ParameterInfo parameter in construct...
{
Type paraType = parameter.ParameterType;
if (!container.ContainsKey(paraType.FullN...
{
throw new Exception("Error");
}
list.Add(CreateObject(container[paraType....
}
return Activator.CreateInstance(type, list.To...
}
/// <summary>
/// 获取构造函数
/// </summary>
/// <param name="constructors">构造函数集合</param>
/// <returns>构造函数</returns>
private ConstructorInfo GetConstructor(Constructo...
{
// 构造函数个数为0,则抛出异常
if (constructors.Length == 0)
{
throw new Exception("Error");
}
// 构造函数个数为1,则直接返回
if (constructors.Length == 1)
{
return constructors[0];
}
// 获取形参个数最多的构造函数
ConstructorInfo constructor = constructors[0];
foreach (var item in constructors)
{
if (item.GetParameters().Length > constru...
{
constructor = item;
}
}
return constructor;
}
}
}
}}
** 主程序调用 [#a71fd8a7]
#codeprettify{{
using System;
using App.Controller;
using App.IoC;
using App.Repository;
using App.Service;
namespace App
{
class Program
{
static void Main(string[] args)
{
// 创建IoC容器,注册接口和类
var container = new SimpleIocContainer();
container.RegisterType<IUserInfoRepository, U...
container.RegisterType<IUserInfoService, User...
container.RegisterType<IUserInfoController, U...
// 利用IoC容器自动创建对象
var controller = container.Resolve<IUserInfoC...
// 输出结果
var list = controller.GetUserInfoList();
foreach (var item in list)
{
Console.WriteLine(item.UserName + "," + i...
}
Console.ReadKey(true);
}
}
}
}}
运行结果如下所示:
super,12345
admin,12345
在上面的代码中,我们并没有手动去创建Repository、Service、...
#hr();
コメント:
#comment_kcaptcha
終了行:
[[ASP.NET Core Web]]
&color(red){※This article is based on .NET 7};
#contents
控制反转是一种思想,依赖注入是一种设计模式,控制反转的思...
* 概要 [#t07feb1c]
控制反转 (Inversion of Control) ,简称IoC,是面向对象编程...
在传统的程序开发中,开发人员往往会手动去new一个对象,换言...
* IoC容器的例程 [#n6a7208c]
下面就来编写一个简单的IoC容器,主要分为IoC、Controller、S...
** Model [#e46a11a7]
#codeprettify{{
namespace App.Model
{
public class UserInfo
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
}}
** Repository [#v558e0dc]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Repository
{
public interface IUserInfoRepository
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System.Collections.Generic;
using App.Model;
namespace App.Repository
{
public class UserInfoRepository : IUserInfoRepository
{
public IEnumerable<UserInfo> GetUserInfoList()
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo { UserName = "super", P...
list.Add(new UserInfo { UserName = "admin", P...
return list;
}
}
}
}}
** Service [#k08c2ac9]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Service
{
public interface IUserInfoService
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System;
using System.Collections.Generic;
using App.Model;
using App.Repository;
namespace App.Service
{
public class UserInfoService : IUserInfoService
{
private readonly IUserInfoRepository repository;
public UserInfoService(IUserInfoRepository reposi...
{
this.repository = repository;
}
public IEnumerable<UserInfo> GetUserInfoList()
{
return repository.GetUserInfoList();
}
}
}
}}
** Controller [#vd08d092]
#codeprettify{{
using System.Collections.Generic;
using App.Model;
namespace App.Controller
{
public interface IUserInfoController
{
IEnumerable<UserInfo> GetUserInfoList();
}
}
using System.Collections.Generic;
using App.Model;
using App.Service;
namespace App.Controller
{
public class UserInfoController : IUserInfoController
{
private readonly IUserInfoService service;
public UserInfoController(IUserInfoService service)
{
this.service = service;
}
public IEnumerable<UserInfo> GetUserInfoList()
{
return service.GetUserInfoList();
}
}
}
}}
** IoC [#xde9dee5]
#codeprettify{{
using System;
using System.Collections.Generic;
using System.Reflection;
namespace App.IoC
{
public class SimpleIocContainer
{
private readonly Dictionary<string, Type> contain...
/// <summary>
/// 构造函数
/// </summary>
public SimpleIocContainer()
{
container = new Dictionary<string, Type>();
}
/// <summary>
/// 注册类型
/// </summary>
/// <typeparam name="TSuper">超类</typeparam>
/// <typeparam name="TSub">子类</typeparam>
public void RegisterType<TSuper, TSub>()
where TSuper : class
where TSub : class
{
string fullName = typeof(TSuper).FullName;
if (!container.ContainsKey(fullName))
{
container.Add(fullName, typeof(TSub));
}
}
/// <summary>
/// 获取需要的类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>() where T : class
{
string fullName = typeof(T).FullName;
if (!container.ContainsKey(fullName))
{
throw new Exception("Error");
}
return (T)CreateObject(container[fullName]);
}
/// <summary>
/// 递归创建对象
/// </summary>
/// <param name="type">子类类型</param>
/// <returns>object</returns>
private object CreateObject(Type type)
{
ConstructorInfo[] constructors = type.GetCons...
ConstructorInfo constructor = GetConstructor(...
List<object> list = new List<object>();
foreach (ParameterInfo parameter in construct...
{
Type paraType = parameter.ParameterType;
if (!container.ContainsKey(paraType.FullN...
{
throw new Exception("Error");
}
list.Add(CreateObject(container[paraType....
}
return Activator.CreateInstance(type, list.To...
}
/// <summary>
/// 获取构造函数
/// </summary>
/// <param name="constructors">构造函数集合</param>
/// <returns>构造函数</returns>
private ConstructorInfo GetConstructor(Constructo...
{
// 构造函数个数为0,则抛出异常
if (constructors.Length == 0)
{
throw new Exception("Error");
}
// 构造函数个数为1,则直接返回
if (constructors.Length == 1)
{
return constructors[0];
}
// 获取形参个数最多的构造函数
ConstructorInfo constructor = constructors[0];
foreach (var item in constructors)
{
if (item.GetParameters().Length > constru...
{
constructor = item;
}
}
return constructor;
}
}
}
}}
** 主程序调用 [#a71fd8a7]
#codeprettify{{
using System;
using App.Controller;
using App.IoC;
using App.Repository;
using App.Service;
namespace App
{
class Program
{
static void Main(string[] args)
{
// 创建IoC容器,注册接口和类
var container = new SimpleIocContainer();
container.RegisterType<IUserInfoRepository, U...
container.RegisterType<IUserInfoService, User...
container.RegisterType<IUserInfoController, U...
// 利用IoC容器自动创建对象
var controller = container.Resolve<IUserInfoC...
// 输出结果
var list = controller.GetUserInfoList();
foreach (var item in list)
{
Console.WriteLine(item.UserName + "," + i...
}
Console.ReadKey(true);
}
}
}
}}
运行结果如下所示:
super,12345
admin,12345
在上面的代码中,我们并没有手动去创建Repository、Service、...
#hr();
コメント:
#comment_kcaptcha
ページ名: