简体   繁体   English

数据访问层设计模式

[英]Data Access Layer design patterns

I have to design a Data Access Layer with .NET that probably will use more than one database management system (Mysql and Sql Server) with the same relational design. 我必须设计一个带.NET的数据访问层,它可能会使用多个具有相同关系设计的数据库管理系统(Mysql和Sql Server)。

Basically, it has to be simple to switch from one database to another so I would like you to recommend me some web-sites or books that has been useful for you, with common design patterns or information in general to implement this kind of data access layer. 基本上,从一个数据库切换到另一个数据库必须简单,所以我希望你向我推荐一些对你有用的网站或书籍,通常的设计模式或信息来实现这种数据访问层。

Thank you. 谢谢。

I recommend Patterns of Enterprise Application Architecture by Martin Fowler. 我推荐Martin Fowler的企业应用程序架构模式。

A list of the patterns is also on his website 他的网站上也有一系列模式

The DataMapper pattern is also relevant. DataMapper模式也很重要。

I like using interface based Db access. 我喜欢使用基于接口的Db访问。 Every db provider for Ado.net implements basic interfaces, and when you use them your code may look like this: Ado.net的每个数据库提供程序都实现了基本接口,当您使用它们时,您的代码可能如下所示:

public static IDbConnection GetConnection(string connectionName)
{
  ConnectionStringSettings ConnectString = ConfigurationManager.ConnectionStrings[connectionName];
  DbProviderFactory Factory = DbProviderFactories.GetFactory(ConnectString.ProviderName);
  IDbConnection Connection = Factory.CreateConnection();
  Connection.ConnectionString = ConnectString.ConnectionString;
  return Connection;
}

Then, when you need to communicate with db: 然后,当您需要与db通信时:

public static DataTable Dummy()
{
  using (IDbConnection Connection = GetConnection("SiteSqlServer"))
  {
    IDbCommand Command = Connection.CreateCommand();
    Command.CommandText = "DummyCommand";
    Command.CommandType = CommandType.StoredProcedure;

    Connection.Open();

    using (IDataReader reader = Command.ExecuteReader())
    {
      DataTable Result = new DataTable();
      Result.Load(reader);
      return Result;
    }
  }
}

With this technique you can create fully db independent DAL. 使用此技术,您可以创建完全独立于数据库的DAL。 Of course for some complex scenarios this is not enough. 当然,对于一些复杂的情况,这还不够。 But mostly this will do the work, and you don't needed various external libs. 但大多数情况下这将完成工作,并且您不需要各种外部库。

The easiest solution would be to use an ORM. 最简单的解决方案是使用ORM。 Check out LLBLGen. 查看LLBLGen。 Using the Adapter Model you can switch between data providers while using the same business objects. 使用适配器模型,您可以在使用相同的业务对象时在数据提供程序之间切换。 It can generate code for both MySql and Sql Server. 它可以为MySql和Sql Server生成代码。

In general, I second John Nolan's recommendation of Patterns of Enterprise Application Architecture . 总的来说,我是John Nolan 对企业应用程序架构模式的第二个推荐。

More specifically, I would always recommend that you hide your Data Access Layer behind an interface and use Dependency Injection to inject a particular Data Access Component into your Domain Logic at run-time. 更具体地说,我总是建议您在接口后面隐藏数据访问层,并使用依赖注入在运行时将特定的数据访问组件注入域逻辑。

You can use a Dependency Injection Container or do it manually . 您可以使用依赖注入容器或手动执行

On the technology side I would recommend Microsoft's Entity Framework , since your data access needs seem to be constrained to relational databases. 在技​​术方面,我会推荐Microsoft的Entity Framework ,因为您的数据访问需求似乎受限于关系数据库。 The Entity Framework is Microsoft's official OR/M and it has providers for many different RDBMSs, as well as LINQ support. 实体框架是微软官方的OR / M,它提供许多不同的RDBMS以及LINQ支持。

It really depends on the size of your layer and the type of product your developing. 这实际上取决于图层的大小和您开发的产品类型。 If it is fairly well contained then ADO.NET will probably be ideal. 如果它包含得相当好,那么ADO.NET可能是理想的。 If it is a bigger DAL layer, and its greenfield development of multitargeted dbms it's best to use an ORM tool. 如果它是一个更大的DAL层,并且它的多区域dbms的绿地开发,最好使用ORM工具。 They are fast, efficient and mature products, and can quickly enable the retargetting to another db, simply by changing a single parameter. 它们是快速,高效和成熟的产品,只需更改单个参数即可快速启用到另一个数据库的重新定位。 Writing static ADO, is something that is passing into legacy. 编写静态ADO是传递给遗留的东西。

There are several ORM tools which can do the job, all work slightlty differently, and depend on your budget, size of your team etc. They can either work by having to write a mapping class like NHibernate, or work through reflection, ie attribute markup. 有几个ORM工具可以完成这项工作,所有工作都略有不同,并且取决于您的预算,团队规模等。他们可以通过编写像NHibernate这样的映射类来工作,或者通过反射工作,即属性标记。

For free, ie open source, if your skint, NHibernate is ideal. 对于免费,即开源,如果你的skint,NHibernate是理想的。 I'm using this at the moment, to build the DAL layer, for a large enteprise product. 我现在正在使用它,为大型企业产品构建DAL层。 Its excellent, but take some time to master. 它的优秀,但需要一些时间来掌握。 With NHibernate you define mapping classes, which when executed generate the db model for you. 使用NHibernate,您可以定义映射类,在执行时为您生成db模型。 It supports stored procedures. 它支持存储过程。 The downside is that it takes some time to learn, especially around mapping complex data correctly. 缺点是需要一些时间来学习,特别是在正确映射复杂数据方面。 Its excellent. 它的优秀。 It has a huge bundle of samples and other projects floating about thats used it. 它有一大堆样本和其他项目浮动使用它。 Check out Koders.com. 看看Koders.com。

If you have a some budget, then LLBLGen is ideal. 如果你有一些预算,那么LLBLGen是理想的选择。 It is strongly typed and also supports stored procedures. 它是强类型的,也支持存储过程。

If have some of the data model already available, then TierDeveloper is ideal. 如果某些数据模型已经可用,那么TierDeveloper是理想的选择。 Its essentially free, and works by developing a set of classes from your database model. 它基本上是免费的,并且通过从数据库模型开发一组类来工作。 The only downside is the mapper for mysql is 3rd party. 唯一的缺点是mysql的mapper是第三方。 It is a enteprise class product that has been made free to support ncache, and its a possible approach. 它是一个企业级产品,可以自由支持ncache,它是一种可能的方法。

If you are desperate to stick with MS, they are moving towards ORM, and have a product called ADO.NET Entity Framework. 如果您迫切希望坚持使用MS,他们将转向ORM,并拥有一个名为ADO.NET Entity Framework的产品。 Functionally it is not as complete as the tools defined above. 从功能上讲,它不如上面定义的工具那么完整。 Its about 3 generations behind in maturity. 它在成熟时落后约3代。 Its available in vs 2008 sp1. 它在vs sp1中可用。 The connector for mysql would be a cost. mysql的连接器是一个成本。

Also, you could use LINQ. 此外,您可以使用LINQ。 It will also target mysql, if you would also need the connector. 如果你还需要连接器,它也将以mysql为目标。

Ideally, your best bet is with ORM. 理想情况下,最好的选择是使用ORM。 If you can't support open source, and have budget, then get 如果你不能支持开源,并有预算,那就搞定吧

Hope that helps. 希望有所帮助。

I've found ADO.NET to be very useful for this. 我发现ADO.NET对此非常有用。 It has all the features you need to create a data access layer independent of the database you use. 它具有创建独立于您使用的数据库的数据访问层所需的所有功能。

如果您愿意学习它, NHibernate旨在处理这种情况

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

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