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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM