简体   繁体   English

我应该在具有数据库访问权限的应用程序中应用哪些设

[英]What design patterns should I apply in application with database access

I will start to develop an application that has to access to database. 我将开始开发一个必须访问数据库的应用程序。 What kind of design patterns area usually need to be implemented to make my application more flexible for example changing DB client from SQL to Oracle. 通常需要实现哪种设计模式区域才能使我的应用程序更加灵活,例如将DB客户端从SQL更改为Oracle。

I believe to perform execution of some query I can implement Template Method Pattern. 我相信执行一些查询我可以实现模板方法模式。 And to get connection a singleton pattern with double check would be sufficient. 要获得连接,使用双重检查的单例模式就足够了。

Is there anything else I should know before starting? 在开始之前还有什么我应该知道的吗?

Application will be developed in C#, so there is support for object inheritance and polymorphism. 应用程序将在C#中开发,因此支持对象继承和多态。

Any help is appreciated. 任何帮助表示赞赏。

Make sure all your code is encapsulated in a data access layer. 确保所有代码都封装在数据访问层中。 Code against interfaces so that if you need to write a new data access library, you don't have to change all calling code. 针对接口的代码,以便在需要编写新的数据访问库时,不必更改所有调用代码。 This will at least isolate all data access into on library. 这至少会将所有数据访问隔离到库中。 How likely is the changing of database to be? 变更数据库的可能性有多大? Don't make the software complex for the what-ifs as this will just make life more difficult. 不要让软件复杂的假设,因为这会让生活变得更加困难。

Abstract something 'on-fly' and only when You can clearly see a benefit. 只有当你可以清楚地看到一个好处时,才能“飞行”。
Otherwise - that's just waste of time. 否则 - 那只是浪费时间。

Do not think like: 不要这样想:

I should use pattern [x] because it might fix [y] 我应该使用pattern [x],因为它可能会修复[y]

Think like this: 这样想:

Oh crap, again got to write same stuff. 哦废话,再次写同样的东西。 Let's see how we could avoid that... 让我们看看我们如何避免......

Check the Catalog of Patterns of Enterprise Application Architecture by Martin Fowler. 查看Martin Fowler 的企业应用程序架构模式目录

You may find a few good ideas there. 你可能会在那里找到一些好主意。

There's a good design pattern called Data Access Object , you will have to incorporate it into C#. 有一个很好的设计模式称为数据访问对象 ,您必须将其合并到C#中。

Summary: 摘要:

The DAO implements the access mechanism required to work with the data source. DAO实现了使用数据源所需的访问机制。 The data source could be a persistent store like an RDBMS, an external service like a B2B exchange, a repository like an LDAP database, or a business service accessed via CORBA Internet Inter-ORB Protocol (IIOP) or low-level sockets. 数据源可以是持久性存储,如RDBMS,外部服务(如B2B交换),存储库(如LDAP数据库),或通过CORBA Internet Inter-ORB协议(IIOP)或低级别套接字访问的业务服务。 The business component that relies on the DAO uses the simpler interface exposed by the DAO for its clients. 依赖于DAO的业务组件使用DAO为其客户端公开的更简单的接口。 The DAO completely hides the data source implementation details from its clients. DAO完全隐藏了客户端的数据源实现细节。 Because the interface exposed by the DAO to clients does not change when the underlying data source implementation changes, this pattern allows the DAO to adapt to different storage schemes without affecting its clients or business components. 由于DAO向客户端公开的接口在基础数据源实现更改时不会更改,因此此模式允许DAO适应不同的存储方案,而不会影响其客户端或业务组件。 Essentially, the DAO acts as an adapter between the component and the data source. 实质上,DAO充当组件和数据源之间的适配器。

you should investigate the user of the data mapper pattern to keep the implementation details of how your data is stored (SQL/Oracele/Access) independent from the use of the data itself. 您应该调查数据映射器模式的用户,以保持数据存储(SQL / Oracele / Access)的实现细节,而不依赖于数据本身的使用。

http://martinfowler.com/eaaCatalog/dataMapper.html http://martinfowler.com/eaaCatalog/dataMapper.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM