我如何设计一个数据访问层是否恰当? [英] How do I design a Data Access Layer appropriately?

查看:157
本文介绍了我如何设计一个数据访问层是否恰当?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据访问层(DAL)。我在想,如果它的设置正确,或者如果我需要改进呢?

I have the following Data Access Layer (DAL). I was wondering if it's set up correctly, or if I need to improve it?

public class User 
{

}

//Persistence methods
static class UserDataAccess
{
   UsersDAL udal = // Choose SQL or FileSystem DAL impl.


   InsertUser(User u)
   {
      // Custom logic , is 'u' valid etc. 

      udal.Insert(u);
   }
}

abstract class UsersDAL
{    
   GetUserByID();
   InsertUser(u);
   ...
}

// implementaitons of DAL

static class UsersSQLStore : UsersDAL
{

}

static class UsersFileSystemStore : UsersDAL
{

}

我是从用户类访问方法收集进一步调用任何自定义DAL分开的存储层。

I separated the storage layer from the User class to access methods collection which further call any custom DAL.

是在DAL实现使用静态是否正确?

Is use of static in DAL implementation correct?

请建议更正或方法,我可以使这个更好。我没有很多的经验与写作code的层。

Please suggest corrections or ways I can make this better. I don't have a lot of experience with writing code in layers.

推荐答案

没有这些类的应该是静态。我不认为你应该命名您的类 DAL 或者,因为它短的数据访问层,和一类本身就不是一个层(在我的脑海至少) 。您可以使用广泛采用的术语代替。我建议你​​做一些这样的:

None of those classes should be static. I don't think you should name your classes DAL either, because its short for Data Access Layer, and a class in itself is not a layer (in my mind at least). You can use the widely adopted term repository instead. I suggest you do something like the following:

public class User{

}

public abstract class UserRepository{
    public abstract void InsertUser(User user);
}

public class SqlUserRepository : UserRepository{
    public override void InsertUser(User user)
    {
      //Do it
    }
}

public class FileSystemUserRepository : UserRepository{
    public override void InsertUser(User user)
    {
      //Do it
    }
}

public class UserService{
    private readonly UserRepository userRepository;

    public UserService(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    public void InsertUser(User user){
        if(user == null) throw new ArgumentNullException("user");
        //other checks
        this.userRepository.InsertUser(user);
    }
}

请注意, UserService的被注入在其构造抽象类 UserRepository 的实例。您可以使用依赖注入(DI)框架来自动为你做这个,比如温莎城堡距离的Castle项目。这将允许你指定( UserRepository )来具体实现从抽象的映射(如 SqlUserRepository )的构文件或code。

Note that the UserService is injected with an instance of the abstract class UserRepository in its constructor. You can use a Dependency Injection (DI) framework to do this for you automatically, such as Windsor Castle from Castle Project. It will allow you to specify a mapping from abstraction (UserRepository) to concrete implementation (eg. SqlUserRepository) in a configuration file, or in code.

希望这点你在正确的方向,并请询问​​您是否需要更多的信息。

Hope this points you in the right direction, and please ask if you need more information.

这篇关于我如何设计一个数据访问层是否恰当?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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