使用Abstract类的Nest类? (以及保护/隐藏课程)?

时间:2013-07-14 18:46:10

标签: c#

当我处理嵌套类并遇到问题时。那是有人告诉我我需要使用界面。在实现接口的过程中,我了解到接口不允许构造函数或传递参数参数。所以,我正在考虑抽象,但我不确定实现它们并保持简单。

在下面的源代码中。环境设置在Console项目中填充,并传递到DLL项目中的Database类。

问题#1 - 是否可以抽象环境设置以获得更简单的脚本?

问题#2 - 使用嵌套类,我如何保护DealerRepository类(让它对Console项目不可见?(我猜抽象)。

问题#3 - DealerRepository类是否需要抽象或什么?

我想要的是在Console项目中保护/隐藏DealerRepository类,而不必通过nest类重复传递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...

    }
  }
}

2 个答案:

答案 0 :(得分: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属性从Console应用程序中使用_libraryDatabase.Dealer类。我不确定你在这里想要达到的目标。

答案 1 :(得分: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");

        // ...
    }
}