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的使用。
NuGet方式获取
我们做如下修改:
核心代码:
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 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SpringNetApp { public class Person { public string UserName { get; set; } } }
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 !"; } } }
<?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>
<?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>
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); } } }
运行效果:
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文:https://www.cnblogs.com/jeremywucnblog/p/12871388.html