Prism 模块和数据库
我正忙于学习 Prism 4 以及所有内容的细节,但我还没有看到关于我想要完成的任务的教程/演练。希望这里有人已经或曾经从事过类似的项目。
我的应用程序是一个基本的 CRUD 应用程序,我已将其分解为不同的关注领域和模块。但是,我希望所有模块共享一个公共的本地 SQL Express 数据库。数据库一开始只有有限数量的表,每个模块都会检查数据库中是否有它需要的表,如果不存在,则创建它们。我怎样才能做到这一点?
我曾考虑过最初添加所有表格,但这似乎打破了我心中的模块化原则。也许我的想法是错误的,但是如果数据库已经完全意识到并与数据库创建时的给定模块强耦合,那么松散耦合的模块有什么意义呢?
寻找一些见解。
I'm busy learning Prism 4 and the in and outs of everything, but I've yet to see a tutorial/walk through on what I want to accomplish. Hoping someone here has or has worked on a similar project.
My application is a basic CRUD application that I've broken down in to separate areas of concern and hence modules. However, I want all the modules to share one common local SQL Express database. The database will start off with just a limited number of tables to start with and each module will check the database for the tables it needs and if they are not there, create them. How can I go about accomplishing this?
I've thought about just adding in all my tables initially but this seems to break the principal of modularity in my mind. Perhaps my thinking is wrong, but what would be the point of loosely coupled modules if the database is already fully aware and strongly coupled to a given module from db creation?
Looking for some insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的模块真正独立 - 每个模块都有一个数据库怎么样?如果您需要模块之间的外键 - 它们本质上并没有真正封装 - 我会从一开始就将整个数据库投入使用。在更新之间保持模式最新要容易得多。
模块化有多种形式——业务角度(按模块付费)、职责方面的模块化等等。
我的 5 美分:)
If your modules are truly independent - how about a database per module? If you need foreign keys between your modules - they are in essense not really encapsulated - and I'd put the whole database into play from the get-go. Much easier to keep the schema up-to-date betweeen updates.
Modularity comes in many flavours - business-perspective (pay-per-module), modularity in terms of responsibilities etc. etc.
My 5 cents :)
此响应适用于任何想要查看连接本地数据库的代码的人。它对我有用,不确定这是否是最佳实践。
我正在使用 prism,我需要让我的数据库正常工作。这就是我所做的。实体框架似乎“正好”可以将数据库放置在某个地方。
Bootstrapper.cs 文件:
我的 AppDatabaseContext.cs 文件:
在我的 ViewModel 之一中:
This response is for anyone coming here that wants to see code that hooks up a local database. It works for me, not sure if it's best practice or not.
I'm using prism and I needed to get my database working. Here is what I did. The Entity Framework seems to "just work" for putting the database somewhere.
Bootstrapper.cs file:
My AppDatabaseContext.cs file:
In one of my ViewModels:
你似乎在问两个问题。第一个是:如何使用 PRISM 确保数据库中存在我的模块特定架构,如果不存在,则创建它。第二个问题是:如何最好地构建我的数据层,使其在模块化应用程序中解耦。
为了回答您关于如何进行模块模式检查的第一个问题,我这样说:
如果您一直在使用 Prism,那么您无疑会想出几种方法来完成它。与编程中的任何事情一样,有很多方法可以完成它。如果我需要使用 Prism 执行此操作,我可能会执行以下操作: 在模块程序集中创建一个实现 Microsoft.Practices.Prism.Modularity.IModule 接口的类 (MyPlugInModule.cs)。然后,我将代码放入构造函数或 Initialize 方法中,该方法检查数据库以查看模块架构是否存在。如果没有,则创建它。
为了回答关于如何最好地构建数据模块化的第二个问题,我这样说:
就像 Goblin 所说,这实际上取决于您想要实现的模块化类型。如果您正在销售此应用程序,并且希望将模块作为独立的软件包出售,那么您可能不想创建数据模型来支持软件包,直到最终用户付费为止。
您应该能够使用实体框架来确保您的模块能够与基本应用程序模块共享实体。此外,根据您的要求,或者您的架构是否允许,您可能希望将模型/数据层抽象为与模块不完全一致的程序集。这将减少代码重复和依赖性。
在我当前正在开发的应用程序中,我们使用 WPF 与 MVVM、PRISM 与 MEF 以及 WCF 数据服务。我们的客户端模块共享一个数据组件,该数据组件与位于基本应用程序模型(身份验证/角色表、应用程序数据等)之上的主要数据服务端点进行通信。当我们的数据库中创建特定于模块域的表时,会在服务器上创建一个新的模型和服务端点,并在客户端上创建一个单独的程序集以与数据模型进行通信。
如果模块特定模型发生更改,则只需更改受影响的组件,因为模块特定数据封装在其自己的服务和客户端程序集中。从测试、安全等方面的隔离角度来看,这是一个更好的选择。当然,缺点是如果基本应用程序模型发生更改,则必须更新所有关联的模块特定实现。
但同样,这实际上取决于您的要求。如果您坚持使用带有 MEF、模块化设计模式和实体框架 4 的 PRISM 4,您应该能够提出一个好的模块化解决方案,而无需紧密耦合。
You seem to be asking two questions. The first is: How can I use PRISM to ensure that my module specific schema exists in the database, and if not, create it. The second question is: how can I best structure my data layer such that it is decoupled in a modular application.
To answer your first question about how to do the module schema check, I say this:
If you’ve been going through Prism, you’ve no doubt thought up a couple of ways to accomplish it. As with anything in programming, there are many ways to accomplish it. If I needed to do this with Prism, I’d probably do the following: Create a class (MyPlugInModule.cs) in my module assembly which implements the Microsoft.Practices.Prism.Modularity.IModule interface. I would then put code in either the constructor, or in the Initialize method, which checks the database to see if the module schema exists. If it does not, then create it.
To answer your second question about how to best structure your data modularity, I say this:
Like Goblin says, it really depends on what type of modularity you are trying to accomplish. If you are selling this application and you want to sell modules as independent packages, then you probably do not want to create a data model to support a package until the end user has paid for it.
You should be able to use Entity Framework to ensure that your modules are able to share entities with the base application modules. Additionally, depending on what your requirements are, or if your architecture will allow, you may want to abstract your model/data layer into assemblies that are not perfectly aligned with your modules. This will reduce code duplication and dependencies.
On an application that I am currently working on, we're using WPF with MVVM, PRISM with MEF, and WCF data services. Our client modules share a data assembly which communicates with our main data service endpoint that sits on top of the base application model (authentication/role tables, application data, etc). When tables in our database are created that are specific to the domain of a module, a new model and service endpoint are created on the server, and a separate assembly is created on the client to communicate with the data model.
If the module specific model changes, only the affected components have to be changed, since module specific data is encapsulated in its own service and client assembly. It is a better option from an isolation standpoint for testing, security, etc. The downside of course is if the base application model changes, all of the associated module specific implementations have to be updated.
But again, it really depends on your requirements. If you stick with PRISM 4 with MEF, modular design patterns, and entity framework 4, you should be able to come up with a good solution that is modular without being tightly coupled.