entlib 5.0学习笔记 unity入门介绍

1. unity简介

2.简单的哦入门示例程序

 

3. 代码下载 

1. unity简介

javaee中在很多能够实现IoC模式的框架, unity就是在.net平台上的一种实现。

2.简单的入门示例程序

2.1 新建一个c# console application,添引用:

 

2.2 新建接口ILogger:

 namespace HelloUnity

{
    public interface ILogger
    {
        void Log(String message);
    }
}

2.3 新建类SpecialPortSettings:

namespace HelloUnity
{
    class SpecialPortSettings
    {
        public String Port = "8080";
    }
}
2.4 新建SpecialLogger类,实现ILogger接口:
namespace HelloUnity.Implementations
{
    class SpecialLogger : ILogger
    {
        public SpecialPortSettings Settings { get; set; }
        #region ILogger 成员
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
        #endregion
    }
}
2.5 添加配置文件App.config:
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
  
< configSections >
    
< section  name ="unity"  type ="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  
</ configSections >
  
< unity  xmlns ="http://schemas.microsoft.com/practices/2010/unity" >
    
< alias  alias ="ILogger"  type ="HelloUnity.ILogger, HelloUnity"   />
    
< namespace  name ="HelloUnity.Implementations"   />
    
< assembly  name ="HelloUnity"   />
    
< container >
      
< register  type ="ILogger"  name ="special"  mapTo ="SpecialLogger" >
        
< property  name ="Settings" >
        
</ property >
      
</ register >
    
</ container >
  
</ unity >

</ configuration> 

最终的项目工程如下:

 

2.6 Main测试程序:

namespace HelloUnity
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer().LoadConfiguration();
            ILogger specialLogger = container.Resolve<ILogger>("special");
            specialLogger.Log("Log");
            // 通过unity向specialLogger注入Settings
            Console.WriteLine(((SpecialLogger)specialLogger).Settings.Port);
            Console.ReadKey();
        }
    }
}

 

测试程序首先得到IUnityContainer,这里使用的是默认的配置文件App.config,然后通过Reslove得到specialLogger,最后测试Settings的注入是否成功。

3. 代码下载 

/Files/xuqiang/entlib/HelloUnity.rar

 

转载于:https://www.cnblogs.com/xuqiang/archive/2011/03/30/1999416.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值