LazyLoad 和 ConnectionString
对于你的想法类型的问题来说,这有点小钱。
我们从头开始编写所有类的代码。对于我们的业务实例,连接字符串是构造函数的一部分。这很简单,因为通常它是唯一的构造函数参数。当我们从数据库加载一条记录并将其存储在实体对象中时......我们应该如何记住连接字符串?这是为了延迟加载的目的。因为我们可能正在谈论多个相同基本数据库类型的数据库实例。我们需要知道从哪里获取记录,并知道从哪里延迟加载相关对象。
我知道这听起来是一个简单的问题。我们正在努力避免代码气味。
我们是否在每个构造函数中将连接字符串设置为字符串以尝试强制设置它?我们是否存储对加载实体的业务对象的引用?同时,还认为在某些情况下从 xml 反序列化时我们需要有一个空的构造函数。
没有这方面的经验,但想到了工厂。要获取新的实体对象,必须通过必须证明连接字符串的工厂。
这不是一个公开的 API,只是供团队正确实施和采用的东西。
一些链接/代码示例也很棒。谢谢
This is sort of penny for your thoughts type question.
We code all our classes from scratch. For our business instances, connectionstring is part of the constructor. That is simple because usually it is the only constructor param. When we load a record from the database and store it in an entity object... how should we remember the connectionstring? This is for lazyload purposes. Since we may be talking to more that one database instance of the same base database type. We need to know where we got the record from and to know where to lazyload related objects from.
I know this sounds like a simple question. We are trying to avoid codesmell.
Do we set a connectionstring in every constructor as a string to try to force it to be set? Do we store a reference to the business object that loaded the entity. Meanwhile, also thinking that we need to have an empty constructor when deserializing from xml for some cases.
Not experienced with this, but have thought of factories. That to get a new entity object must be through a factory where connectionstring must be proved.
This is not an explosed API, just something for the team to implement and adopt properly.
Some links/codeexamples would be great too. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是实体框架吗?您使用 POCO 吗?您可以利用 EF 内置的延迟加载功能吗?
对于它的价值,您可以询问上下文它连接到哪个数据库:
但这可能没有那么有帮助。
Is this Entity Framework? Are you using POCOs? Can you take advantage of EF's built in lazy loading capabilities?
For what it's worth you can ask the context what database it is connected to with:
That is probably not that helpful though.