C# 在任何应用程序中使用持久层有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16016023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of a persistence layer in any application?
提问by
I have to create an application in which I am asked to create an persistence layer in the application. The application is in .net. I have created a business layer and a presentation layer but I don't know how and why I should create a persistence layer.
我必须创建一个应用程序,其中要求我在应用程序中创建一个持久层。该应用程序位于 .net 中。我已经创建了一个业务层和一个表示层,但我不知道我应该如何以及为什么要创建一个持久层。
I googled and came to know that persistence layer is used for storing and retrieving data usually from a database.
我用谷歌搜索并了解到持久层通常用于从数据库中存储和检索数据。
Can anybody explain in detail?
谁能详细解释一下?
采纳答案by Davide Piras
the reason for you to build a DAL ( Data Access Layer ) or any other kind of intermediate layer between database engine and Business / Application logic, is that by adding this layer in the between you isolate the rest / upper layers of your application from the specific database engine / technology you are using right now.
您在数据库引擎和业务/应用程序逻辑之间构建 DAL(数据访问层)或任何其他类型的中间层的原因是,通过在两者之间添加这一层,您可以将应用程序的其余层/上层与您现在正在使用的特定数据库引擎/技术。
This has several advantages, like easier migration to other storage engines, better encapsulation of database logic in a single layer ( easier to replace or modify later depending on how well you have designed your cross-layer interfaces etc...)
这有几个优点,比如更容易迁移到其他存储引擎,更好地将数据库逻辑封装在单层中(更容易替换或修改,取决于你设计的跨层接口等......)
see my answer here, it is an example about ASP.NET MVC and EF but the structuring of solution and projects is actually technology independent: MVC3 and Entity Framework
在这里看到我的回答,这是一个关于 ASP.NET MVC 和 EF 的例子,但解决方案和项目的结构实际上是独立于技术的:MVC3 和实体框架
Also read some articles to better understand this matter, for example: http://www.developerfusion.com/article/84492/net-and-data-persistence/
另外阅读一些文章可以更好地理解这件事,例如:http: //www.developerfusion.com/article/84492/net-and-data-persistence/
回答by Belogix
In very simple terms a persistence layer is a way to SAVEand RETRIEVEitems that your application uses.
用非常简单的术语来说,持久层是一种保存和检索应用程序使用的项目的方法。
A simple example is you have a class that represents a person (name, age and gender). While your application is running this is held in memory. But, say you want that information available if you close and open your application again. Well, you need some way to SAVEthat person and then later on RETRIEVEit again. This is where a persistence layer comes in and will write your person somewhere "permanent".
一个简单的例子是你有一个代表一个人(姓名、年龄和性别)的类。当您的应用程序运行时,它保存在内存中。但是,假设您希望在关闭并再次打开应用程序时可以使用该信息。好吧,您需要某种方法来拯救那个人,然后再重新取回它。这是持久层的用武之地,它将把你的人写在“永久”的地方。
That could be a database, a flat file, registry depending on the life-time and requirements etc.
这可能是数据库、平面文件、注册表,具体取决于生命周期和要求等。
In your persistence layers you will perform CRUD (Create, Read, Update, Delete) operations. Often against a database so you would Createa new person (Fred Bloggs). Say they change their name another user of your system might Readthe record and change to Fred Miggins and Updatethe database. That customer then leaves the country so you Deletethem.
在持久层中,您将执行 CRUD(创建、读取、更新、删除)操作。经常针对一个数据库,所以你会创建一个新人(Fred Bloggs)。假设他们更改了姓名,您系统的另一个用户可能会读取记录并更改为 Fred Miggins 并更新数据库。然后该客户离开该国家/地区,因此您将其删除。
回答by Kenneth Garza
persistance layer otherwise known as a Data Access Layer or other terminology.
持久层也称为数据访问层或其他术语。
It seperates the guts of getting and saving the data from the business layer. The reason you do this is so your business logic (the part of the application that does the heavy lifting for your data manipulation) is not tied to a specific type of data source.
它将获取和保存数据与业务层分开。您这样做的原因是您的业务逻辑(为您的数据操作执行繁重工作的应用程序部分)不依赖于特定类型的数据源。
The data layer will need to be written to be database specific. So if you're using MySQL to access all your data then you will write the dataLayer for that use.
数据层将需要编写为特定于数据库的。因此,如果您使用 MySQL 访问所有数据,那么您将为该用途编写 dataLayer。
If at some point you decide to move to MongoDB, then instead of rewriting your entire application. You can rewrite just the data access parts to get the data from MongoDB. Since the business logic doesnt care how you get the data, only that you do, it and the Presenation layer can remain intact.
如果在某个时候您决定迁移到 MongoDB,那么不要重写整个应用程序。您可以只重写数据访问部分以从 MongoDB 获取数据。由于业务逻辑不关心您如何获取数据,只要您这样做,它和 Presenation 层就可以保持完整。
Hope this helps.
希望这可以帮助。
回答by Renan
If you're not saving your data somewhere, it won't be there again when your app is opened again. You can save it to a database as you said (SQL, Oracle, PostGRE etc.), or directly to disk (serializing in binary format, or plain text etc.).
如果您没有将数据保存在某处,那么当您的应用再次打开时,它就不会再出现了。您可以将其保存到您所说的数据库(SQL、Oracle、PostGRE 等),或直接保存到磁盘(以二进制格式或纯文本等进行序列化)。
Using a layer to do that is a good practice. It reduces coupling (i.e. spaghetti code), and makes maintenance much easier.
使用层来做到这一点是一个很好的做法。它减少了耦合(即意大利面条式代码),并使维护更容易。
I suggest you read about Hibernate and it's .NET cousin, NHibernate.
我建议您阅读有关 Hibernate 的内容,它是 .NET 的表亲 NHibernate。
回答by jonathangersam
Persistence = read/write/delete records to disk or database.
持久性 = 读取/写入/删除记录到磁盘或数据库。
Layer = insulation, etc.
层=绝缘等。
Persistence Layer = generally means to isolate read/write/delete logic from the business logic. ideally by placing a few (or a single) point of interaction between the business logic and the persistence modules.
持久层=一般是指将读/写/删除逻辑与业务逻辑隔离。理想情况下,通过在业务逻辑和持久性模块之间放置几个(或单个)交互点。
I think its something we've all done (save to disk, db, etc), this is just a fancy academic term, they're just asking us to:
我认为这是我们都做过的事情(保存到磁盘、数据库等),这只是一个花哨的学术术语,他们只是要求我们:
- make sure that the persistence parts group together nicely,
- ideally with few (or exactly a single) point of interaction inbetween business logic (ie. gateway pattern),
- so in the future maybe we'd like to switch database types or hosts (i.e. smaller scope - we'd just need to muck around the persistence parts to get it running).
- 确保持久性部分很好地组合在一起,
- 理想情况下,业务逻辑(即网关模式)之间的交互点很少(或恰好是一个),
- 所以在未来也许我们想切换数据库类型或主机(即更小的范围 - 我们只需要解决持久性部分来让它运行)。
Cheers,
干杯,

