简体   繁体   English

如何使用MEF导出和导入应用程序服务?

[英]How do I export and import application services with say MEF?

I'm working with MEF right now, but the answer I'm looking for probably is irrelevant to MEF -- it's all dependency injection -- I'm just using MEF terminology as an example here. 我现在正在和MEF合作,但我正在寻找的答案可能与MEF无关 - 这都是依赖注入 - 我只是在这里使用MEF术语作为例子。

Short background story, I read this article over at MSDN with focus on Composite Applications 简短的背景故事,我在MSDN上阅读了这篇文章,重点是复合应用程序

In this figure there's three things, the shell, the application services and the modules. 在这个图中有三个东西,shell,应用程序服务和模块。 So that's a composite application. 这是一个复合应用程序。

替代文字
(source: microsoft.com ) (来源: microsoft.com

What I don't fully get is the application services part. 我没有完全得到的是应用服务部分。 What's the service, what does it look like? 什么是服务,它是什么样的? How do you expose a service through a module and how do you consume a service from a different module? 如何通过模块公开服务以及如何使用不同模块的服务?

I'd really like to see some neat small code examples, nothing fancy but something to illustrate how all this comes to life (the application services part). 我真的很想看到一些简洁的小代码示例,没有任何花哨的东西可以用来说明这一切是如何实现的(应用程序服务部分)。

Application Services, as far as MEF is concerned, are just another composable part. 就MEF而言,应用服务只是另一个可组合的部分。 Any interface or class you can compose can act like a service. 您可以编写的任何接口或类都可以充当服务。

Each service will have some interface or base class you want to implement. 每个服务都有一些您想要实现的接口或基类。 You can do these en masse via some type of IService interface (and use [ImportMany] to import them all), but often, you'll want different service types. 您可以通过某种类型的IService接口进行这些[ImportMany] (并使用[ImportMany]将它们全部导入),但通常,您需要不同的服务类型。

You'd then import this, as required, into your classes. 然后,您可以根据需要将其导入到您的课程中。 For example, say you have a common interface library for services, and you provide: 例如,假设您有一个用于服务的通用接口库,并且您提供:

public interface IDataRepostory
{
     public IList<MyType> MyTypes { get; }
}

You can then have a separate library export specific types: 然后,您可以使用单独的库导出特定类型:

[Export(typeof(IDataRepository))]
public class Repository: IDataRepostory
{
    // implement interface for this specific "service"
}

Your main program would then be able to import this as needed, and write code against it. 然后,您的主程序将能够根据需要导入它,并针对它编写代码。 For example, say you wanted to display customers, you'd need to load the customers from your data layer. 例如,假设您想要显示客户,则需要从数据层加载客户。 If you wanted to load via your repository, you could import the repository into a specific portion of your application: 如果要通过存储库加载,可以将存储库导入应用程序的特定部分:

public class CustomersViewModel
{
     [Import]
     public IDataRepository
     {
         get; set;
     }

     // ...
}

You'd then get this service composed directly into your application. 然后,您将直接将此服务组合到您的应用程序中。

This is considered an "Application Service" because it's an application specific implementation of some generic service - it's not a view related component, and it may be used throughout your application. 这被视为“应用程序服务”,因为它是某些通用服务的特定于应用程序的实现 - 它不是与视图相关的组件,并且可以在整个应用程序中使用。

Declare interface for service and export class implementing this interface. 声明实现此接口的服务和导出类的接口。 For instance you might have IPersonBuilder , declared in shared assembly. 例如,您可能拥有在共享程序IPersonBuilder声明的IPersonBuilder You're main module has MyPersonBuilder implementing interface and export this. 你的主模块有MyPersonBuilder实现接口并导出它。 All Views uses imports IPersonBuilder to call method on it, and using MEF composition they will be able to call say CreatePerson() on your MyPersonBuilder from your main module. 所有视图都使用导入IPersonBuilder来调用方法,并使用MEF组合,他们将能够从您的主模块调用MyPersonBuilder上的说CreatePerson()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM