如果一个应用程序的配置使用DAL来访问? [英] Should the configuration of an application be accessed using DAL?

查看:211
本文介绍了如果一个应用程序的配置使用DAL来访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释了什么是的数据访问层下面的定义从维基百科在多层应用

一个数据访问层(DAL)的是一个层中的计算机程序的这   提供了存储在永久存储数据的简化访问   某种,例如数据库。

A data access layer (DAL) is a layer of a computer program which provides simplified access to data stored in persistent storage of some kind, such as a database.

持久存储还可包括一个或多​​个文件,但上层不知道我怎样组织中的文件中的信息。假设我们有一个使用的配置文件的应用程序,如的app.config 的web.config :在的app.config 文件中,有可能会出现一些参数(例如高速缓存的最大尺寸)的值,所以这些值必须在应用程序启动时加载。此外,如果应用允许这些参数的编辑通过一个UI,那么的app.config 文件应该被更新。是这些操作部分的 DAL 的?

The persistent storage could also consist of one or more files, but the upper layers do not know how I organized the information in the files. Suppose we have an application that uses a configuration file, such as app.config or web.config: in the app.config file there may be the values ​​of some parameters (for example the maximum size of a cache), so these values must be loaded during application startup. Moreover, if the application allows the editing of these parameters through a UI, then the app.config file should be updated. Are these operation part of the DAL?

推荐答案

分离关注的一点是明智的,因为它可以让你单独测试片更容易,因为他们没有背负着处理具体问题,如在哪里以及如何配置存储和检索。

A bit of separation of concerns is sensible, as it allows you to test pieces individually easier since they are not burdened with dealing with specific concerns such as where and how configuration is stored and retrieved.

这可能是明智的,以创建所需要的配置数据的模型,并接受这个模型的依赖(通过构造函数),或者如果配置的数据很简单,它可以只是在组件本身的一些属性。

It may be sensible to create a model of the configuration data you need and accept this model as a dependency (via a constructor), or if the configuration data is simple enough, it can just be a few properties on the component itself.

在创建模型,传送设备配置信息的情况下,你就必须使用该对象在应用程序的一部分,允许用户与配置的值进行交互更容易的时间。用户界面来配置你的应用程序,这本身就是一个单独的功能。

In the case of creating a model to transmit configuration information, you'll have an easier time using that object in the part of your application that allows the user to interact with configurable values. UI to configure your app is a separate feature in itself.

下面是一个愚蠢的例子。

Here's a silly example.

public class Settings
{
    public string SettingOne { get; set; }

    public bool SettingTwo { get; set; }

}

public class DAL
{
    public Settings Settings { get; private set; }

    public DAL(Settings settings)
    {

    }
}

所以,如果你使用的单元测试,你可以给你想要的设置,而不必用管理您的设置(以下是NUnit测试的轻佻的例子)。

So, if you use unit tests, you can test just the DAL by giving it the settings you want, without bothering with the piece that manages your settings (below is a frivolous example of an NUnit test).

[Test]
public void Should_do_something_important()
{
    // Arrange
    var dal = new DAL(new Settings { SettingOne = "whatever", SettingTwo = true });

    // Act
    var result = dal.DoSomethingImportant();

    // Assert
    Assert.That(result, Is.Not.Null);
}

现在,在您的应用程序,你可以有管理设置一个独立的组件(如果你选择......也许你的设置真的很简单,这个额外步骤是不必要的,它是由你),你可以全面测试自己。

Now, in your application, you can have a separate component that manages settings (if you so choose... maybe your settings are really quite simple, and this extra step is unnecessary; it's up to you), which you can fully test on its own.

public class SettingsManager
{
     public Settings ReadSettings(string path)
     {
         // read from the store

     }

     public void WriteSettings(Settings settings, string path)
     {
        // write settings to the store
     }

}

和其它轻薄测试:

[Test]
public void Should_write_settings_to_store()
{
    // Arrange
    var manager = new SettingsManager();

    // Act
    var settings = new Settings { SettingOne = "value", SettingsTwo = true };
    manager.WriteSettings(settings, @"C:\settings\settings.config");

    // Assert
    Assert.That(File.Exists(@"C:\settings\settings.config", Is.True));
}

现在在你的应用程序,你可以做这样的事情把它们放在一起:

Now in your application, you can do something like this to bring them together:

protected DAL DAL { get; private set; }

public void Init()
{
      var manager = new SettingsManager();

      DAL = new DAL(manager.ReadSettings(@"C:\settings\settings.config"));
}

通过走这条路的好处是,现在你的DAL和你设置的管理都不耦合。现在,您可以构建和独立测试两部分。让你的DAL专注于DAL的东西,并设置经理专注于管理设置。

The benefit with going this route is now your DAL and your management of settings are uncoupled. You can now build and test the two pieces independently. Let your DAL focus on DAL stuff, and settings manager focus on managing settings.

这篇关于如果一个应用程序的配置使用DAL来访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆