zoukankan      html  css  js  c++  java
  • 开源框架

    简介

    Spring.Net的在线手册简介如下:

    即使有先进的工具和技术,软件开发也是一件相当令人头疼的工作。Spring.NET为建立企业级应用提供了一套轻量级的解决方案。通过Spring.NET,我们可以用统一且透明的方式来配置应用程序,并在应用中集成AOP的功能。Spring.NET的重点是为中间层提供声明式事务管理,以及一个功能齐全的ASP.NET扩展框架。

    Spring.NET可以为很多领域的企业级应用开发提供“一站式服务”。虽然功能强大,Spring.NET仍然是模块化的,允许单独使用其中的任一部分。在使用IoC容器来配置应用程序时,我们既可以用传统的ADO.NET来访问数据库,也可以使用Spring.NET的Hibernate集成代码或ADO.NET抽象层来访问数据库。Spring.NET是非侵入式的,代码对框架本身不会产生任何依赖(或者只需要极少的依赖,取决于应用的范畴)。

    通过上面的说明我们可以知道,在软件开发中可以借助其进行关键类的创建,降低数据层与业务逻辑层的耦合,在一定程度上减少二次开发的难度。

    实例说明

    下面我们通过一个简单的Demo进行说明一下SpringNet的使用。

    • 获取SpringNet动态链接库。
      • NuGet方式获取(搜索Spring.Core)
      • 手工下载后,添加到项目的引用中(添加BinRelease目录下Common.Logging.dll和Spring.Core.dll到项目中)

         

                NuGet方式获取

    • 创建一个空白解决方案,并命名为SpringNetDemo
    • 添加一个桌面应用程序,并命名为SpringNetApp
    • 添加一个接口,并命名为IUserInfoService.cs
    • 添加一个类,并命名为Person.cs
    • 添加一个类,并命名为UserInfoService.cs
    • 添加一个应用程序配置文件,并命名为App.Config
    • 添加一个xml文件,并命名为Services.xml      

    我们做如下修改:

    1. 在IUserInfoService接口中定义一个方法;
    2. 在Person类中声明一个UserName属性;
    3. 在UserInfoService类中定义一个属性Age,并让UserInfoService继承IUserInfoService并去实现showMsg这个方法;
    4. 接下来我们分别为App.Config和Services.xml进行配置,实现通过SpringNet进行相关接口的创建;
    5. 通过IApplicationContext接口下的GetObject方法即可实现在不创建类的情况下,完成类的相关属性、方法等的调用。

    核心代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace SpringNetApp
     7 {
     8     public interface IUserInfoService
     9     {
    10         /// <summary>
    11         /// 展示相关信息
    12         /// </summary>
    13         string showMsg();
    14     }
    15 }
    IUserInfoService.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace SpringNetApp
    {
        public class Person
        {
            public string UserName { get; set; }
        }
    }
    Person
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace SpringNetApp
    {
        public class UserInfoService : IUserInfoService
        {
            /// <summary>
            /// 用户年龄信息
            /// </summary>
            public int Age { get; set; }
            /// <summary>
            /// 人类
            /// </summary>
            public Person person { get; set; }
            /// <summary>
            /// 展示一段信息,并根据用户名和年龄动态加载
            /// </summary>
            public string showMsg()
            {
                return "Hi this is my first SpringNet Test Demo and I am " + person.UserName + " " + Age + " years old !";
            }
        }
    }
    UserInfoService.cs
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <!--- 配置SpringNet 开始 -->
        <configSections>
            <sectionGroup name="spring">
                <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
                <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
            </sectionGroup>
        </configSections>
        <spring>
            <context>
                <resource uri="config://spring/objects"/>
                <resource uri="file://Services.xml"/>
            </context>
            <objects xmlns="http://www.springframework.net" >
            </objects>
        </spring>
        <!--- 配置SpringNet 结束 -->
    </configuration>
    App.Config
    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">
        <description>An  example that demonstrates simple IoC features.</description>
        <!-- 单独写在此处主要为了和后续维护的方便 -->
        <object name="UserInfoService" type="SpringNetApp.UserInfoService, SpringNetApp">
            <property name="Age" value="29" />
            <property name="Person" ref="Person" />
        </object>
    
    
        <object name="Person" type="SpringNetApp.Person, SpringNetApp">
            <property name="UserName" value="Jeremy.Wu" />
        </object>
    
    </objects>
    Services.xml
    using Spring.Context;
    using Spring.Context.Support;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace SpringNetApp
    {
        public partial class FrmMain : Form
        {
            public FrmMain()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 测试一下
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                // 返回的就是一个已经根据<objects/>节点的内容配置好的容器对象
                IApplicationContext ctx = ContextRegistry.GetContext();
        
                IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");
                MessageBox.Show(lister.showMsg(), "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    
    }
    FrmMain.cs

    运行效果:

      作者:Jeremy.Wu
      出处:https://www.cnblogs.com/jeremywucnblog/
      本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    记录wordpress+nginx配置的坑
    Nginx进行反向代理多个web项目
    Docker 安装 Zabbix-4
    小特跨境电商ERP 小程序版 库存好帮手
    小特跨境电商ERP 浏览器版 为决策者提供数据支持
    小特跨境电商ERP桌面版 8.如何部署 真的这么难安装吗?
    小特跨境电商ERP桌面版 7.销售订单毛利计算 就是这么简单
    小特跨境电商ERP桌面版 6.运费结算是个大问题?容易一团糟
    小特跨境电商ERP桌面版 5.订单发货提交后,后续还要做什么?
    小特跨境电商ERP桌面版 4.平台商品和本地单品如何映射?
  • 原文地址:https://www.cnblogs.com/jeremywucnblog/p/12871388.html
Copyright © 2011-2022 走看看