通过关注SRP和OCP来帮助重构 [英] Help refactoring with attention to SRP and OCP

查看:47
本文介绍了通过关注SRP和OCP来帮助重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示我的数据库中用户的集合类,名为Users。我想在类中添加一个Load函数来从数据库中获取数据。稍后可以更改加载函数以实现某种异步加载和/或ui更新它的进度,所以我知道我希望它是一个单独的类并使用接口。这是我到目前为止的想法:


I have a collection class representing users in my database, called Users.  I want to add a Load function to the class to get data from the database.  The load function could later be changed to implement some kind of asynchronous loading and/or ui update on it's progress so I know that I want it to be a separate class and utilize an Interface.  Here's what I'm thinking so far:


Public
 Interface
 ILoad
    Function
 Load() As
 Integer

End
 Interface


Public
 Enum
 DatabaseType
    MySql
    MsSql
End
 Enum


Public
 Class
 LoadFactory
    Private
 _load As
 ILoad

	Public
 Sub
 New
(databaseType as
 Enum
.DatabaseType, connectionString as
 string
)
        Select
 Case
 databaseType
            Case
 databaseType.MySql
                _load = New
 MySqlLoad(connectionString)
            Case
 databaseType.MsSql
                _load = New
 MsSqlLoad(connectionString)
        End
 Select

    End
 Sub


    Public
 Function
 GetInstance() As
 ILoad
        Return
 _load
    End
 Function

End
 Class


Public
 Class
 Users
    'collection code here


    'implement ILoad interface

    Private
 _load As
 ILoad

    Public
 Function
 Load() As
 Integer

        'TO DO: get load factory constructor values from global variables

        Dim
 loadFactory As
 New
 LoadFactory(DatabaseType.MySql, "MyConnectionString"
)
        _load = loadFactory.GetInstance
        return _load.Load()
    End
 Function

End
 Class

推荐答案

在您的设计中,Users类实例化一个新的LoadFactory。这不是SRP也不是OCP。

In your design Users class instantiates a new LoadFactory. That’s not SRP nor OCP.

下面的示例(C#)使用DataMapper模式。您可能希望通过将DAL实现打包在单独的程序集中并使用DI框架(例如Unity)注入它们来解耦DAL实现。这允许您添加新数据库而无需重新编译项目。

The example below (C#) uses the DataMapper pattern. You might want to decouple DAL implementations by packaging them in separate assemblies and inject them using a DI framework (for example Unity). This allows you to add new databases without the need to recompile your project.

ps:您可能还想查看对象关系映射工具。

ps: you might also want to take a look at object-relational mapping tools.

SRP:

UsersCollection类表示用户集合。
MySqlDal类从MySql数据库中检索数据。
MsSqlDal类检索数据表单SQL Server数据库。
UserMapper类在数据库和用户对象之间移动数据。

OCP:

The UsersCollection class represents a collection of users.
The MySqlDal class retrieves data from the MySql database.
The MsSqlDal class retrieves data form the SQL Server database.
The UserMapper class moves data between the database and the user object.

OCP:

IDal接口定义合同数据访问要实现的组件。

The IDal interface defines the contract for data access components to implement.

IDal {
DataTable GetAllUsers();
DataTable GetUsers(字符串语句);
DataTable GetById(int id);
};

IDal {
  DataTable GetAllUsers();
  DataTable GetUsers(string statement);
  DataTable GetById(int id);
};

UserMapper {

UserMapper {

public UserMapper(IDal dal){
this.userDal = dal;
}

  public UserMapper(IDal dal) {
    this.userDal = dal;
  }

UsersCollection GetAllUsers(){
UsersCollection result = new UsersCollection();
DataTable usersTable = dal.GetAllUsers();

  UsersCollection GetAllUsers() {
    UsersCollection result = new UsersCollection();
    DataTable usersTable = dal.GetAllUsers();

fo到达(DataRow [] userRow in usersTable.Rows){
result.Add(新用户(userRow [0],..,..,..));
}左右br />};

    foreach(DataRow[] userRow in usersTable.Rows) {     
      result.Add(new User(userRow[0], .., .., ..));
    }
  }
};

MySqlDal:IDal {
...
};

MySqlDal : IDal {
  ..
};

MsSqlDal:IDal {
..
};

MsSqlDal : IDal {
  ..
};


这篇关于通过关注SRP和OCP来帮助重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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