使用 Unity 注入 DAAB 数据库对象
我正在尝试创建一个 DAO 对象,该对象采用对数据访问应用程序块的数据库对象的构造函数依赖项。如何设置 .config 文件以使 Unity 解决依赖关系?我尝试映射数据库类型来创建数据库对象,但无法使其工作。
还有其他人尝试过做这样的事情吗?
I'm trying to create a DAO object that takes a constructor dependency to the Data Access Application Block's Database object. How do I setup my .config file to have Unity resolve the dependency? I've tried mapping the Database type to create a Database object, but couldn't get it to work.
Has anyone else tried doing anything like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个开始提示,添加您对问题所做的代码示例。它告诉我们您已经走了多远,以及至少您正在使用什么语言。我将假设使用 c#、EL V5 并从头开始:
首先让我们回顾一下依赖于数据库应用程序块的类的标准构造函数:
以下是调用创建者的方法:
简而言之,您可以使用依赖注入,忘记如何实现要创建它,只需请求它的一个实例,库就会完成其余的工作。
创建配置的最简单方法是使用 Enterprise Library V5 配置工具,因为它创建了必要的部分,我建议您“阅读精细手册”。
如果您手动执行此操作,则以下是您需要的部分:
Just a starting hint, add a sample of the code you've done to your question. It tells us how far you've got, and at least what language you are using. I'll assume c#, EL V5 and starting from scratch:
Firstly let's review a standard constructor for a class that depends on the database application block:
and here's how to call the creator:
So in short there's dependency injection for you, forget about how to create it, just ask for an instance of it and the library does the rest.
The easiest way to create the config is to use the Enterprise Library V5 Configuration Tool, as it creates the necessary parts, I advise you to 'Read The Fine Manual'.
If you are doing it manually here are the parts you need: