spring.net 结合简单三层实例
最近在学习spring.net 接下来将实现一个与我们普通三层结合的实例!简单了解一下spring.net的运用;
该项目共分四层;接口层IClassLibrary 被BLL 及DAL层引用;层;
BLL不引用DAL 因为我们这用spring.net来加载;
BLL引用的spring.net所要的DLL配置文件在UI
我们首先来看一下接口层两个类的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IClassLibrary { public interface IBll { void GetBllData(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IClassLibrary { public interface IDal { void GetDataStr(); } }
接下来我们看一下DAL层的两个类的代码(这两个类都实例的接口IDal;主要是为了后面测试只要修改配置就可以实现实例化不同的类):
using System.Text; using IClassLibrary; namespace DAL { public class DalClass:IDal { public void GetDataStr() { Console.WriteLine("通过手动获得数据"); } } }
using IClassLibrary; namespace DAL { public class DalTwoClass:IDal { public void GetDataStr() { Console.WriteLine("通过自动获得信息"); } } }
DAL这边有个地方必须要注意;因为我们DAL层没有让任务引用;所以我们要修改它生成后的路径;否则会报无法加载的错误;我们要把生成的路径改在我们UI层的BIN文件夹里;
接下来我们看一下BLL层的类代码:
using IClassLibrary; using Spring.Context; using Spring.Context.Support; using Spring.Core.IO; using Spring.Objects.Factory.Xml; using Spring.Objects.Factory; namespace BLL { public class BllClass:IBll { private IDal DB; public BllClass() { //string[] xmlFiles = new string[] { "file://Config/Objects.xml" }; //IApplicationContext context = new XmlApplicationContext(xmlFiles); //DB = context.GetObject("readerDal") as IDal; IResource input = new FileSystemResource("file://Config/Objects.xml"); IObjectFactory factory = new XmlObjectFactory(input); DB = (IDal)factory.GetObject("readerDals"); } public void GetBllData() { DB.GetDataStr(); } } }
接下来我们要看一下配置文件里的内容Objects.xml:
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <object id="readerDals" type="DAL.DalClass,DAL"> </object> </objects>
type=类库.类,类库
接下来我们看一下配置文件App.Config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <!--提供Spring对应用程序上下文的支持--> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <!--提供Spring对 对象容器的支持--> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <context> <!--Spring中 IOC容器 所使用的对象XML文件定义--> <resource uri="assembly://UI/UI.Config/Objects.xml"/> </context> </spring> </configuration>
接下来我们看一下UI层的代码:
using BLL; namespace UI { class Program { static void Main(string[] args) { //配置文件要放在哪里 BllClass bllCient = new BllClass(); bllCient.GetBllData(); Console.ReadLine(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述