在 Orchard 内消费非 Orchard 服务类别
我们最近开发了一个在 MVC3 应用程序内部使用的类库。 MVC3 应用程序使用 Autofac 在运行时向控制器注入所需的服务类。该库使用自己的一组表进行数据存储,这些表可能与主机应用程序位于同一数据库中,也可能不同。
这些类遵循依赖注入模式,作为示例,看起来像这样(是的,您之前已经见过一百万次了)...
public class PackageService : ServiceBase, IPackageService
{
private readonly IRepository _db;
private readonly IClientService _clientSvc;
public PackageService(IRepository db, IClientService clientService)
{
_db = db;
_clientSvc = clientService;
}
public ServiceResult<IEnumerable<Package>> FindPackagesBy(string searchTerm, out int totalRecords, int pageSize, int pageIndex)
{
//...
}
public ServiceResult<Package> GetPackage(string packageRef)
{
//...
}
}
我们希望在 Orchard CMS 中运行的模块中使用这些类。从我读到的果园如何工作看来我需要装饰我打算在 Orchard 中使用 IDependency 接口的每个类 - 考虑到这将创建对 Orchard 本身的依赖,这看起来并不是一个好主意。我们的类管理自己的事务/工作单元,因此不需要由果园自己的事务机制来管理。这可能吗?或者我们是否面临着对现有库进行 Orchard 特定实现?
很高兴听到任何已经经历过这种情况的人的来信。
We have recently developed a library of classes that are used within an MVC3 app internally. The MVC3 app uses Autofac to inject the controllers with the required service classes at runtime. The library uses its own set of tables for data storage that may or may not be in the same database as the host application.
The classes follow the dependency injection pattern, and as an example look something like this (yes, you've seen it a million times before)...
public class PackageService : ServiceBase, IPackageService
{
private readonly IRepository _db;
private readonly IClientService _clientSvc;
public PackageService(IRepository db, IClientService clientService)
{
_db = db;
_clientSvc = clientService;
}
public ServiceResult<IEnumerable<Package>> FindPackagesBy(string searchTerm, out int totalRecords, int pageSize, int pageIndex)
{
//...
}
public ServiceResult<Package> GetPackage(string packageRef)
{
//...
}
}
We would like to consume these classes within modules that run in Orchard CMS. From what i have read on how orchard works it would seem that i need to decorate each class that i intend to use within Orchard with the IDependency interface - which doesnt really seem like a great idea considering this will create a dependency on orchard itself. Our classes manage their own transactions / unit of work, so do not need to be governed by orchards own transaction mechanism. Is this possible? or are we faced with making Orchard specific implementations of our existing library?
Would be good to hear from anyone who has been through this already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要依赖 Orchard 并“实现” IDependency 以将它们注入 AFAIK。但这并不是一个失败的提议,在我看来,制作一个特定于 Orchard 的版本并不需要你所描述的那么多工作,特别是如果你想将数据移动到 Orchard。
Your going to need to take dependency on Orchard and "implement" IDependency to get them injected AFAIK. But that isn't a losing proposition, it doesn't sound to me like making an Orchard specific version is going to be that much work from what you describe, especially if you looking to move the data to Orchard.
我自己解决了这个问题...
在使用“外部库”的模块中,您创建一个 Autofac 文件夹,并添加一个继承自 Autofac 模块的类,并在其中添加您的绑定(这是在 Autofac 中设置绑定的标准过程) ) - Orchard 将在运行时发现这一点,并为您的模块控制器注入所需的依赖项。
但是,默认情况下,对外部库的调用包含在请求事务中,因为这就是 orchard 的工作方式。因为我的库通过 EF4 调用 SQL Server,所以我收到异常“MSDTC 不可用”。您需要明确声明您不希望这些操作包含在事务中,因此您的控制器最终看起来像这样......
Figured this out myself...
Within the module that consumes the 'external library' you create an Autofac folder, and add a class that inherits from the Autofac Module and add your bindings in there (this is standard procedure for setting up bindings in Autofac) - Orchard will discover this at runtime and inject your module controllers with the required dependencies.
However, the calls to the external library are included within the request transaction by default, as this is how orchard works. Because my library was calling SQL Server via EF4 i was getting an exception 'MSDTC is unavailable on '. You need to explicitly state that you do not want these operations included in the transaction, so your controller ends up looking like this...