Asp.net MVC - 如何实现灵活的模块跨项目使用?
我不是直接寻找代码,而是寻找一些如何最好地解决我的问题的想法。
我正在开发这个 asp.net mvc 应用程序。它应该是“高度模块化”的,并且许多部分必须在不同的项目中重复使用。
我们当前的方法是使用托管可扩展性框架在运行时导入程序集。这些通常包括工作所需的一切;模型、视图和控制器。导入时会注册路线和导航/主菜单按钮。 到目前为止,这有效;例如,我可以简单地将“新闻栏”程序集复制到任何其他项目中,包括 MEF 内容,并且“神奇地”新项目提供可在 /News/List 访问的新闻功能。
然而,问题是,虽然在大多数情况下,程序集中提供的默认视图适合,但我有时希望导入的控制器以不同的视图显示,以自定义布局显示其他字段。我当前的方法是将模块中的操作方法虚拟化。如果另一个项目需要使用自定义视图呈现列表,我只需重写列表方法,调用基本方法来填充 ViewData,然后只需调用我想要的任何视图。然而,这在某种程度上感觉很脏,如果有人知道更好的解决方案,我将非常感激。
我面临的另一个问题是我可能希望导入的模型能够与不同的表一起使用。我们将使用 Fluent NHibernate,其中目标表在 ClassMap - Table("News") 中定义。 映射是这样导入的:
foreach(Assembly assembly in assemblies)
configuration.Mappings(m => m.FluentMappings.AddFromAssembly(assembly));
我不知道如何更改导入的映射的表,但我想有一个简单的方法吗?
感谢您至少阅读本文:)
I´m not looking for code directly, but for some ideas how to solve my problem the best.
There is this asp.net mvc application I´m working on. It should be "highly modular" and many parts have to be reused across different projects.
Our current approach is using the Managed Extensibility Framework to import assemblies at runtime. These usually consist of everything necessary to work; models, views and controllers. Routes and navigation/main menu buttons are registered when being imported.
This works so far; For example, I can simply copy the "news-column" assembly into any other project, include the MEF things, and "magically" the new project serves a news-feature accessible at /News/List.
However, the problem is, while in most cases the default view delivered within the assembly fits, I sometimes would like the imported controller to show up with a different view, displaying other fields in a custom layout. My current approach is to make the action methods in the modules virtual. If another project needs to render the list with a custom view, I simply override the list method, call the base method to fill the ViewData and then simply call whatever view I want. However, this somehow feels dirty and if anyone knows a better solution, I would really appreciate it.
Another problem I´m facing is that I may want an imported model to work with a different table. We are going with Fluent NHibernate where the target table is defined within the ClassMap - Table("News").
Mappings are imported like this:
foreach(Assembly assembly in assemblies)
configuration.Mappings(m => m.FluentMappings.AddFromAssembly(assembly));
I could not figure out how to change the table of an imported Mapping, but I guess there is a simple way?
Thanks for at least reading this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为重写你的控制器操作应该感觉“肮脏”。事实上,我在公共库中使用基本控制器操作来处理可以轻松抽象的内容,例如用户身份验证。另一方面,身份验证通常非常特定于 UI,因此我并不真正费心创建可重用的视图。
我还在我的应用程序中创建基本控制器类来处理创建/处置我的 EF 上下文等。
您可能需要考虑抽象您的数据访问。即使您使用 NHibernate 并且您的标准实现使用特定的数据库模式,您仍然会遇到代码重用的一个非常经典的问题:在可重用包中放入太多特定的逻辑。作为一般规则,我尝试将任何数据库细节排除在可重用代码之外。我使用 POCO 对象和接口,因此我可以使用任何类型的数据源来创建我的对象。然后,我可能会拥有另一个使用 SQL Server、EF、我首选的数据库模式等进行标准实现的程序集。但是,如果我需要将其连接到其他东西,我只需在新版本中实现接口即可。
希望能回答您的问题。
I don't think overriding your controller actions should feel "dirty". In fact, I use base controller actions in a common library for things that can be easily abstracted like user authentication. On the flip side, authentication is usually very specific to the UI so I don't really bother creating reusable views.
I also create base controller classes within my apps to handle creating/disposing my EF context, etc.
You may want to consider abstracting your data access. Even though you are using NHibernate and your standard implementation uses a certain DB schema, you are running into a pretty classic problem of code reuse: putting too much specific logic in your reusable package. As a general rule, I try to keep any database specifics out of my reusable code. I use use POCO objects and interfaces so I can use any type of data source to create my objects. I may then have another assembly with a standard implementation using SQL server, EF, my preferred DB schema, etc. However, in the event I need to wire it up to something else I simple implement interface(s) in a new version.
Hope that answers your question.