posts - 131,  comments - 37,  views - 52万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

【本段摘录自:IOC容器Unity 使用 http://blog.csdn.net/gdjlc/article/details/8695266】

面向接口实现有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等,但是接口一定是需要实现的,如果一个子类实现换成另一个子类实现,就需要在代码中改动,或者建立一个工厂来根据条件生成,还是存着着一定的耦合关系。
依赖注入(Dependency Injection,DI),也叫控制反转(Inversion of Control,IoC)是一个重要的面向对象编程的法则用来削减程序的耦合问题,它把耦合从代码中移出去,放到统一的XML文件中,通过一个容器在需要的时候把这个依赖关系形成,即把需要的接口实现注入到需要它的类中,当需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以,这样可以实现对象的热插拨(有点象USB接口)。

Unity 是一个轻量级、可扩展的依赖注入容器,支持构造函数、属性和方法的依赖注入。

1.C# Unity使用(独立配置文件unity.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 configSource="unity.config" />
 </configuration>
复制代码
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <!--alias 定義別名-->
  <alias alias="IClass" type="ConsoleApplication1.IClass, ConsoleApplication1" />
  <alias alias="MyClass" type="ConsoleApplication1.MyClass, ConsoleApplication1" />
  <!--引入下面的命名空間,可以省的輸入過多的內容-->
  <namespace name="ConsoleApplication1" />
  <assembly name="ConsoleApplication1" />
  
  <container >
    <register type="IClass" mapTo="MyClass" />
    <register type="ILogger" mapTo="FileLogger"></register>
    
    <!--註冊類+構造函數 默認數值是3-->
    <register type="InstallerBase" mapTo="BlogInstaller">
      <constructor>
        <param name="pubContext" type="IPubContext" />
        <param name="sum" type="System.Int32" value="3" />
      </constructor>
    </register>

    <!--<register type="ISomeInterface[]" mapTo="MyTypeImplementingSomeInterface[]"/>-->
    <register type="ISomeInterface[System.Int32]" mapTo="MyTypeImplementingSomeInterface[System.Int32]"/>

    
    <register type="ObjectWithOverloads" name="callFirstOverload">
      <method name="CallMe">
        <param name="param" type="int" value="17"/>
      </method>
    </register>

    <register type="ILogger" mapTo="MockLogger" name="validLogger" />
    <register type="ObjectUsingLogger" name="dependencyRegistered">
      <property name="Logger">
        <optional name="validLogger" />
      </property>
    </register>
    <!--數值-->
    <register type="ILogger" name="main" mapTo="MockLogger" />
    <register type="ILogger" name="another" mapTo="MockLogger" />
    <register type="ILogger" name="special" mapTo="SpecialLogger" />
    <register type="ArrayDependencyObject" name="specificElements">
      <property name="Loggers">
        <array>
          <dependency name="main" />
          <dependency name="another" />
        </array>
      </property>
    </register>
  </container>
  
</unity>
View Code
复制代码

 2. Using Unity for IoC and DI(codeproject国外网站给出的如何将unity.config作为web.config或者app.config的独立配置文件来引入)

<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity configSource="unity.config"/>

此处给出unity.config文件的具体代码:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <typeAliases>
    <!-- Models-->
    <typeAlias alias="IBook" type="BusinessBackend.IBook, BusinessBackend" />
    <typeAlias alias="Book" type="BusinessBackend.Book, BusinessBackend" />
    <!-- Services -->
    <typeAlias alias="IBookService" type="BusinessBackend.IBookService, BusinessBackend" />
    <typeAlias alias="BookService" type="BusinessBackend.BookService, BusinessBackend" />
    <!-- Repositories -->
    <typeAlias alias="IBookRepository" type="BusinessBackend.IBookRepository, BusinessBackend" />
    <typeAlias alias="BookRepository" type="BusinessBackend.BookRepository, BusinessBackend" />
  </typeAliases>
  <container>
    <register type="IBook" mapTo="Book" />
    <register type="IBookRepository" mapTo="BookRepository" name="SQLrepo" />
    <register type="IBookService" mapTo="BookService" >
      <constructor>
        <param name="br" dependencyName="SQLrepo">
        <!--<param name="br" dependencyType="BookRepository">-->
        <!--<dependency type="BookRepository" />-->
        <!--<dependency name="SQLrepo" />-->
        </param>
      </constructor>
    </register>
  </container>
</unity>
View Code
复制代码

 

3.Unity 2.0 Web.config settings with MVC(使用unity的答疑帖子)

http://stackoverflow.com/questions/3925019/unity-2-0-web-config-settings-with-mvc

 

 

 

posted on   踏歌&而行  阅读(8304)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示