保持业务对象集合同步到DB的最佳实践 [英] Best practice keeping business object collection synced to DB

查看:47
本文介绍了保持业务对象集合同步到DB的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试n层开发的水域,我遇到了一个场景,

我不确定最好的解决方案是什么。我有一个公司对象

包含从数据库中检索到的联系人集合。


在表示层,用户可以添加/删除/修改这个

集合,在这种情况下需要与数据库同步。


问题基本上是如何最好地做到这一点?除了覆盖该集合的

添加/删除方法之外,我无法想到以什么方式保持

1:1之间的对应关系收集和DB中的内容。


我会这样做,但有些东西告诉我,这不是一个好主意

集合了解数据库访问。


或者我是否要求同步集合?我应该只是在
上提供公司对象的方法,让我添加/删除/编辑

联系人吗?这意味着如果开发人员直接修改了这个集合,它就不会在数据库中出现



谢谢,

-A

解决方案

" Alfred Taylor" < OM *** @ R-E-M-O-V-Eyahoo.com>在消息中写道

news:e9 ************** @ TK2MSFTNGP09.phx.gbl ...

我正在测试n层开发的水域,我遇到了一个场景
我不确定最好的解决方案是什么。我有一个公司
对象,其中包含从数据库中检索到的联系人集合。

在表示层,用户可以添加/删除/修改此
集合在这种情况下,它需要与数据库同步。

问题基本上是如何最好地做到这一点?除了覆盖集合的
添加/删除方法之外,我无法想出任何方式来保持集合中的内容与内容之间的1:1对应关系。
DB。




为什么不简单地用强类型数据集替换你的集合?那个

仍允许你独立于数据源,同时

允许你将数据集传递回数据层(DataSet.GetChanges())

更容易更新。


什么是集合,真的吗?它只是一个项目列表,每个项目都有

属性,如LastName,PhoneNumber等。强类型数据集给出了

你完全相同的东西,但是建立了更新数据源的方法。


HTH,


Mike Rodriguez


< BLOCKQUOTE>人力资源管理模式。这是一个有趣的建议。我只花了过去一小时左右

阅读MSDN文章,看起来好像使用DataSet会大大简化很多事情。


明天,当我有更多时间时,我会仔细看看它。感谢

的建议。


其他人有什么意见吗?


-A


" Michael Rodriguez" < MI ** @ nospamforme.com>在消息中写道

news:e3 ************** @ TK2MSFTNGP10.phx.gbl ...

" Alfred Taylor" < OM *** @ R-E-M-O-V-Eyahoo.com>在消息中写道
新闻:e9 ************** @ TK2MSFTNGP09.phx.gbl ...

我正在测试n的水域 - 开发和我遇到了一个场景
我不确定最好的解决方案是什么。我有一个公司
对象,其中包含从
数据库中检索到的联系人集合。
在表示层中,用户可以添加/删除/修改
此集合在这种情况下,它需要与数据库同步。

问题基本上是如何最好地做到这一点?除了重写
集合的添加/删除方法之外,我无法想到任何方式
在集合中的内容和内容之间保持1:1的对应关系。
数据库。

为什么不简单地用强类型数据集替换你的集合?



这仍然允许你独立于数据源,而同时
允许您将数据集传递回数据层(DataSet.GetChanges())
以便更新。

什么是集合,真的吗?它只是一个项目列表,每个项目都具有LastName,PhoneNumber等属性。强类型数据集
为您提供完全相同的东西,但内置方式更新数据
来源。
HTH,

Mike Rodriguez



Alfred Taylor写道:

我正在测试n层开发的水域,我遇到了一个
场景,我不确定最佳解决方案是什么。我有一个
公司对象,其中包含从数据库中检索到的联系人集合。

在表示层,用户可以添加/删除/修改
问题基本上是如何最好地做到这一点?除了重写集合的添加/删除方法之外,我无法想办法在集合中的内容和
之间保持1:1的对应关系。在数据库中。

我会这样做,但有些东西告诉我,让这个集合了解数据库访问并不是一个好主意。
<或者我是否要求同步收集太多了?我是否应该在公司对象上提供方法,让我添加/删除/编辑
联系人?这意味着如果开发人员直接修改集合,则不会在数据库中出现。




保持断开连接的数据存储区,无论是数据集还是

对象的集合,与DB同步始终是一个挑战。这是两个不同的场景。


1)只需删除数据库中数据的状态并替换为

对象集合中的当前状态



2)跟踪对象集合所做的更改,并在

上重播它们数据存储。


另一个人建议使用一个类型化的数据集,但这对你来说不会有帮助,你仍需要对变更进行排序,创建sql得到的东西

完成。


例如从公司的联系人中删除联系人

list 。这只是从列表中删除联系人(即一个

内存中的动作,不影响数据库中的任何数据)?或者是等于

到:删除联系人?或者是等于:将FK联系到

公司?


数据集不能解决这个问题,你必须编写代码就像那样

你自己,总是,它归结为:什么是最简单的出路?


大部分时间,你都可以选择1:首先从

联系人执行删除,其中companyid = @companyID,然后执行插入仍然在company.Contacts中的

联系人。然后,您执行

联系人的重新获取,以读取默认约束值和计算的列值。


可能这对您的情况不可行所以你必须跟踪内存中对集合所做的更改并将它们逐个重放到数据库上。一个典型的对象是UnitOfWork,使用

单位工作模式,几乎每个O / R映射器都有一个。


Frans


-

------------------------------ ------------------------------------------

获取LLBLGen Pro,用于.NET的高效O / R映射: http://www.llblgen.com

我的.NET博客: http://weblogs.asp .net / fbouma

Microsoft MVP(C#)

--------------------- -------------------------------------------------- -


I''m testing the waters of n-tier development and I ran into a scenario that
I''m not sure what the best solution would be. I have a Company object which
contains a collection of contacts retrieved from a database.

In the presentation layer, the user will be able to add/delete/modify this
collection in which case it needs to be synced with the database.

The question is basically how best to do this? Aside from overriding the
add/remove methods of the collection, I can''t think of any way to maintain a
1:1 correspondence between what''s in the collection and what''s in the DB.

I would do this, but something tells me it''s not a good idea to have the
collection know about database access.

Or am I asking too much to have the collection synced? Should I just
provide methods on the Company object which let me add/delete/edit a
contact? Which means if a developer modifies the collection directly, it
won''t occur in the DB.

Thanks,
-A

解决方案

"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...

I''m testing the waters of n-tier development and I ran into a scenario
that I''m not sure what the best solution would be. I have a Company
object which contains a collection of contacts retrieved from a database.

In the presentation layer, the user will be able to add/delete/modify this
collection in which case it needs to be synced with the database.

The question is basically how best to do this? Aside from overriding the
add/remove methods of the collection, I can''t think of any way to maintain
a 1:1 correspondence between what''s in the collection and what''s in the
DB.



Why not simply replace your collection with a strongly typed dataset? That
still allows you independance from the data source, while at the same time
allowing you to pass a dataset back to a data layer (DataSet.GetChanges())
for much easier updates.

What is a collection, really? It''s just a list of items, each one with
properties like LastName, PhoneNumber, etc. A strongly typed dataset gives
you the exact same thing, but with built in ways to update the data source.

HTH,

Mike Rodriguez


Hrmm. That''s an interesting suggestion. I just spent the past hour or so
reading MSDN articles and it seems like using a DataSet would greatly
simplify a lot of things.

I''ll take a closer look at it tomorrow when I have more time. Thanks for
the suggestion.

Anybody else have any input?

-A

"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:e3**************@TK2MSFTNGP10.phx.gbl...

"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...

I''m testing the waters of n-tier development and I ran into a scenario
that I''m not sure what the best solution would be. I have a Company
object which contains a collection of contacts retrieved from a database.
In the presentation layer, the user will be able to add/delete/modify this collection in which case it needs to be synced with the database.

The question is basically how best to do this? Aside from overriding the add/remove methods of the collection, I can''t think of any way to maintain a 1:1 correspondence between what''s in the collection and what''s in the
DB.

Why not simply replace your collection with a strongly typed dataset?


That still allows you independance from the data source, while at the same time
allowing you to pass a dataset back to a data layer (DataSet.GetChanges())
for much easier updates.

What is a collection, really? It''s just a list of items, each one with
properties like LastName, PhoneNumber, etc. A strongly typed dataset gives you the exact same thing, but with built in ways to update the data source.
HTH,

Mike Rodriguez



Alfred Taylor wrote:

I''m testing the waters of n-tier development and I ran into a
scenario that I''m not sure what the best solution would be. I have a
Company object which contains a collection of contacts retrieved from
a database.

In the presentation layer, the user will be able to add/delete/modify
this collection in which case it needs to be synced with the database.

The question is basically how best to do this? Aside from overriding
the add/remove methods of the collection, I can''t think of any way to
maintain a 1:1 correspondence between what''s in the collection and
what''s in the DB.

I would do this, but something tells me it''s not a good idea to have
the collection know about database access.

Or am I asking too much to have the collection synced? Should I just
provide methods on the Company object which let me add/delete/edit a
contact? Which means if a developer modifies the collection
directly, it won''t occur in the DB.



Keeping a disconnected datastore, be it a dataset or collection of
objects, in sync with the DB is always a challenge. THere are two
different scenario''s.

1) simply remove the state for the data in the db and replace it with
the current state in the object collection
or
2) track changes made to the object collection, and replay them on the
datastore.

Another person suggested a typed dataset, but that''s not going to help
you, you still have to sort the changes out, create sql to get things
done.

take for example the removal of a contact from a company''s contacts
list. Is that just the removal of the contact from the list (i.e. an
in-memory action, not affecting any data in the db)?, or is that equal
to: delete the contact? or is that equal to: reset the FK in contact to
company?

A dataset won''t solve that for you, you have to write code like that
yourself, always, it then comes down to: what''s the easiest way out?

Most of the time, you go for option 1: first perform a delete from
contacts where companyid = @companyID and then perform inserts for the
contacts still in company.Contacts. Then you perform a refetch of the
contacts to read default constraint values and calculated column values.

It can be that''s not feasable for your situation so you have to track
the changes made to the collection in memory and replay them one by one
onto the db. A typical object to do taht with is a UnitOfWork, using
the Unit of work pattern, almost every O/R mapper has one.

Frans

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


这篇关于保持业务对象集合同步到DB的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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