2013-07-14 36 views
0

當我在研究嵌套類並遇到問題時。那時有人告訴我我需要使用界面。在實現接口的過程中,我瞭解到接口不允許構造函數或傳遞參數參數。所以,我在想抽象,但我不確定如何實現它們並保持簡單。使用抽象類的嵌套類? (以及保護/隱藏課程)?

在下面的源代碼。環境設置在Console項目中填充並傳遞給DLL項目中的Database類。

問題1 - 是否可以抽象簡化腳本的環境設置?

問題#2 - 使用嵌套類或沒有,我如何保護DealerRepository類(使其不可見到控制檯項目(我猜抽象)

問題#3 - ?是否DealerRepository類需要被抽象還是什麼?

我想這裏是保護/隱藏控制檯項目DealerRepository類,並沒有對EnvironmentSetting屢通過巢班班通/模型。

謝謝...

//EnvironmentSetting.cs 
namespace zTestcase1.Model { 
    public class EnvironmentSetting : IDisposable 
    { 
    public EnvironmentSetting() { } //Constructor... 
    public void Dispose() { } /Dispose... 

    //Member variables... 
    private string _emulation = ""; 
    private string _application = ""; 
    private string _database = ""; 

    //Get/Set properties... 
    public string Emulation { get {return _emulation;} set {_emulation = value;} } 
    public string Application { get {return _application;} set {_application = value;} } 
    public string Database { get {return _database;} set {_database = value;} } 
    } 
} 

//DealerRepository.cs 
namespace zTestcase1.Data.Dealer 
{ 
    public class DealerRepository : IDisposable 
    { 
    public DealerRepository(EnvironmentSetting parmEnvironmentSetting) { 
     _environmentSettingA = parmEnvironmentSetting; 
    } //Constructor... 
    public void Dispose() { } //Dispose... 

    //Member variables... 
    private EnvironmentSetting _environmentSettingA = ""; 

    //Get/Set properties... 
    //N/A... 

    //Functions... 
    public string EnvironmentResponse() 
    { 
     return "Emulation - " + _environmentSettingA.Emulation + ", Application - " + _environmentSettingA.Application + ", Database - " + _environmentSettingA.Database + ""; 
    } 
    public string DealerProifle_Lookup(string parmName) 
    { 
     return "(DealerProfile-Lookup) - " + parmName; 
    } 
    public string DealerProfile_Save(string parmName) 
    { 
     return "(DealerProfile-Save) - " + parmName; 
    } 
    } 
} 

namespace zTestcase1.Library 
{ 
    /*public class Database : IDisposable 
    { 
    //Class... 
    public class DataDealer : IDisposable 
    { 
     //Constructor... 
     public DataDealer(EnvironmentSetting parmEnvironmentSetting) 
     { 
      _environmentSettingA = parmEnvironmentSetting; 
     } 
     //Dispose... 
     public void Dispose() { } 

     //Member variables... 
     private EnvironmentSetting _environmentSettingA = null; 
     private DealerRepository _dataDealer = null; 

     //Get/Set properties... 
     public DealerRepository Dealer { get { if (_dataDealer == null) { _dataDealer = new DealerRepository(_environmentSettingA); } return _dataDealer; } } 

     //Functions... 
     //N/A... 
    }*/ 

    //Constructor... 
    public Database(EnvironmentSetting parmEnvironmentSetting) 
    { 
     _environmentSettingB = parmEnvironmentSetting; 
    } 
    public void Dispose() { } //Dispose... 

    //Member variables... 
    private EnvironmentSetting _environmentSettingB = null; 
    //private DataDealer _dataDealerB = null; 
    private DealerRepository _dataDealerC = null; 

    //Get/Set properties... 
    //public DataDealer Dealer { get { if (_dataDealerB == null) { _dataDealerB = new DataDealer(_environmentSettingB); } return _dataDealerB; } } 
    public DealerRepository Dealer { get { if (_dataDealerC == null) { _dataDealerC = new DealerRepository(_environmentSettingB); } return _dataDealerC; } } 

    //Functions... 
    //N/A... 
    } 
} 

namespace zTestcase1.Sample1.Console 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     EnvironmentSetting environmentSettting = new EnvironmentSetting(); 
     environmentSettting.Application = "DOS Console"; 
     environmentSettting.Database = "Not yet hooked up"; 
     environmentSettting.Emulation = "Development"; 

     Database _libraryDatabase = new Database(environmentSettting); 
     //var test1 = _libraryDatabase.Dealer.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy"); 
     var test2 = _libraryDatabase.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy"); 
     System.Console.WriteLine(_libraryDatabase.Dealer.EnvironmentResponse() + " [=] " + test2); 
     System.Console.ReadLine(); //To pause the console... 

    } 
    } 
} 

回答

0

答案#1:而不是...

//Member variables... 
private string _emulation = ""; 
private string _application = ""; 
private string _database = ""; 

//Get/Set properties... 
public string Emulation { get {return _emulation;} set {_emulation = value;} } 
public string Application { get {return _application;} set {_application = value;} } 
public string Database { get {return _database;} set {_database = value;} } 

你的屬性可以代替這種方式表達(也就相當於,除了初始化「」,它可以根據需要在構造函數來完成而不是):

public string Emulation { get; set; } 
public string Application { get; set; } 
public string Database { get; set; } 

答案2/3:抽象性和內部類不用於限制類的外部呼叫者的可見性。改爲使用internal:這將使同一庫內的其他類訪問,同時禁止從當前庫外部訪問。雖然在這種情況下,看起來好像您正在使用控制檯應用程序中的DealerRepository類通過_libraryDatabase.Dealer屬性。我不完全確定你想在這裏實現什麼。

+0

感謝您的迴應。感謝您對抽象和內部類的澄清。是的,我試圖使用_libraryDatabase.Dealer來顯示DealerRepository類中的所有函數。讓我看看它在內部使用時是否有效。 – fletchsod

+0

#1 - 當我試圖把這個內部的「公共類DealerRepository」改爲「內部類DealerRepository」時,它並沒有在控制檯項目的intellisense中顯示,這是我想要的,但是我得到編譯器或紅色下劃線語法錯誤,無法訪問。 #2 - 取而代之的是,如果我將構造函數內部化,則可以在控制檯項目的intellisense中看到DealerRepository類。有沒有讓DealerRepository類在控制檯項目中對intellisen不可見的方法?謝謝... – fletchsod

+0

@fletchsod:我還不清楚你期望的最終目標是什麼。您希望:(1)DealerRepository類不會顯示在intellisense中,但(2)該類繼續使用_libraryDatabase.Dealer在相同範​​圍內工作。你不能同時看到和看不到同一個班級! – CSJ

0

如果我正確地理解了你,你需要dependency injection
這將需要DI容器。例如,與MEF代碼看起來是這樣的:

1)組件 「A.DLL」(合同):

public interface IEnvironmentSetting 
{ 
    string Emulation { get; } 
    string Application { get; } 
    string Database { get; } 
} 

public interface IDealerRepository 
{ 
    string EnvironmentResponse(); 
    string DealerProifle_Lookup(string parmName); 
    string DealerProfile_Save(string parmName) 
} 

public interface IDatabase 
{ 
    IDealerRepository Dealer { get; } 
} 

2)彙編 「B.DLL」(實現):

[Export(typeof(IDatabase))] 
public class Database : IDatabase, IDisposable 
{ 
    [Import] 
    public IDealerRepository Dealer { get; set; } 

    // other code here... 
} 

[Export(typeof(IDealerRepository))] 
public class DealerRepository : IDealerRepository, IDisposable 
{ 
    [Import] 
    private IEnvironmentSetting EnvironmentSetting { get; set; } 

    // other code here... 
} 

[Export(typeof(IEnvironmentSetting))] 
public class EnvironmentSetting : IEnvironmentSetting, IDisposable 
{ 
    // other code here... 
} 

3)組裝「C.exe」(控制檯應用程序)。您只需從「C.exe」中引用程序集「A.dll」:

static void Main(string[] args) 
{ 
    using (var catalog = // configure composable part catalog here, read MEF docs to find best way for you) 
    using (var container = new CompositionContainer(catalog)) 
    { 
     var database = container.GetExportedValue<IDatabase>(); 

     // you're working with IDatabase and IDealerRepository here, 
     // there's no direct access to the particular implementation classes 
     database.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy"); 

     // ... 
    } 
}