您的位置:首页 > 编程语言 > Java开发

spring.net 结合简单三层实例

2013-02-26 10:46 351 查看

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();
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: