2013-11-24 69 views
0

我正在使用VS2010在C#中創建控制檯應用程序。它是基於在3層結構的含三層在c#控制檯應用程序中添加引用錯誤

  • PMS.UI
  • PMS.DAL
  • PMS.BL

要刪除PMS.DAL和PMS之間循環依賴 .BL我添加了一個額外的層PMS.Service。

  1. 我創建在PMS.BL一個Vehicle類從PMS.Service實現接口IVehicle
  2. 我在DAL和BL中都添加了PMS.Service的參考。
  3. 現在UI調用方法的AddNewVehicle()Vehicle類BL的,它實現IVehicle
  4. BL調用PMS.DAL VehicleDao的AddNewVehicle(IVehicle obj)方法...

所有工作正常,但在構建編譯器的時候說要加PMS.UI中的PMS.Service參考。

PMS.UI沒有實現PMS.Service的任何接口,但調用AddNewVehicle() PMS.BL的Vehicle類的方法實現IVehicle

是否有必要添加PMS.Service的參考,只有當它創建Vehicle類PMS.BL的一個實現IVehicle目前在PMS.Service的實例PMS.UI ..

請幫助我,我是新在c#中使用接口...

謝謝你的答案,但我仍然困惑。我將在這裏介紹我的代碼。我已經將所有四層作爲不同的c sharp類庫(不同的層)添加。

1)PMS.UI(Added reference of PMS.BL) 
Program.cs 
using System; 
using PMS.BL; 
namespace PMS.APP 
{ 
    class Program 
    { 
     static void Main() 
     { 
      var vBo = new VehicleBo();//Compiler Says Add reference of PMS.Service here.Why is it necessary to add Reference of it?? 
      vbo.VehicleNumber = "BA1PA 1212"; 
      vbo.VehicleType = "Bike"; 
      vbo.SaveNewVehicle(); 
     } 
    } 
} 

2)PMS.BL(Added reference of PMS.DAL and PMS.Service) 
VehicleBO.cs 

using PMS.DAL; 
using PMS.Service; 
namespace PMS.BL 
{ 
    public class VehicleBo : IVehicle 
    { 
     public string VehicleNumber { get; set; } 
     public string VehicleType { get; set; } 
     public void SaveNewVehicle() 
     { 
      var vDao = new VehicleDao(); 
      vDao.SaveNewVehicle(this); 
     } 
    } 
} 

3)PMS.DAL(Added reference of PMS.Service) 
using PMS.Service; 
namespace PMS.DAL 
{ 
    public class VehicleDao 
    { 
     public void SaveNewVehicle(IVehicle obj) 
     { 
      //code to insert in database 
     } 
    } 
} 

4)PMS.Service 
IVehicle.cs 
namespace PMS.Service 
{ 
    public interface IVehicle 
    { 
     string VehicleNumber { get; set; } 
     string VehicleType { get; set; } 
     void SaveNewVehicle(); 
    } 
} 
+1

請在PMS.UI中調用'Vehicle'類的'AddNewVehicle()'方法的地方添加代碼段?無論您是通過「Vehicle」類還是「IVehicle」接口實例來調用它,都至關重要。 –

+0

我已在Andrii Kalytiiuk上面添加代碼請檢查它.. – rriwaj

+0

您需要在PMC.UI中爲PMS.Service提供參考,因爲PMS.UI引用了PMS.BL,它定義了像'SaveNewVehicle(IVehicle obj)'這樣的函數,其中定義了IVehicle在PMS.Service中。因此,爲了確保所有來自PMS.BL的調用在PMS.UI中都是有效的 - 您需要在PMS.UI中與PMS.BL一起引用PMS.Service(否則調用'SaveNewVehicle(IVehicle obj)'將無效PMS.UI將不知道任何關於'IVehicle')。 –

回答

0

用給出的細節(也沒有代碼)。這是我的理解。

PMS.Service (IVehicle.cs) 
PMS.BL (Vehicle : IVehicle) 

在這種情況下,如果您公開Vehicle,則還必須添加對PMS.Service的引用。無論如何,在模型接口/服務實現中的聯繫看起來不正確。我寧願考慮創建PMS.Contracts並在那裏簽署我的模型/服務合同。

希望有所幫助。

+0

PMS.Service只包含在PMS.BL中實現的接口爲什麼我需要添加對PMS.Service的引用(如果它只包含不依賴於其他接口的)。 – rriwaj

0

我認爲你有一個架構問題。基本上,如果你是在三層,這是很好的方式:

IHM => BLL => DAL 
Core 

核心是一個項目中包含的工具功能(格式日期,數量等),你的界面。

相關性:IHM參考BLL/BLL參考DAL。所有這些參考核心。核心沒有依賴性。

我是初學者,喜歡你與界面。

  • DAL(depecencies核心)
  • IHM -

    1. 核心
    2. BLL(核心depecencies DAL):

      4個項目:在這裏,如果我必須這樣做,我會選擇的方式(depecencies BLL - Core)

    內核:兩件事:接口IVehicle和實現此類調用的類Vehicle

    因爲我們需要使用DAL,所以我不知道如何處理不使用Core.Vehicle。抽象類不好,因爲如果DAL需要返回一個「IVehicule」對象,我們需要實現一個對象,我們不能實現一個Abstract或Interface。

    在BLL:兩個對象:汽車和卡車實現Core.Vehicule

    在DAL:一個目標:用VEHICULE的方法返回的一個Core.Vehicule

    在IHM:BLL的呼叫。汽車

    而且它做的事...

    編輯: 我已經交你們這樣一個問題:POO and Interface (in C#)

    希望它能幫助你。