eWorld.UI - Matt Hawley

Ramblings of Matt

MEF + Factories Using an Export Provider

November 29, 2008 10:25 by matthaw

After my last post about MEF + Factories, I was chatting with Glenn Block (PM for MEF) about my approach. One of the first things he mentioned is why I hadn't used an ExportProvider. As you know, ExportProvider's are new in the latest drop, and provide an interception point for resolving an import to an export.

 

Taking a look back at the documentation on ComposablePart, I can see why Glenn mentioned this. In it, it states that a ComposoblePart must adhere to to Import/Export contracts. If you've looked at the prior example, you'll see that I'm explicitly violating that rule as I'm finding interfaces based on a base interface, not by Export! At this point, my mind started churning - mainly because there's not a lot of examples or descriptions of what an ExportProvider actually is - but because I wanted to do things "correctly" according to the framework provided. (As an aside, Glenn stated that a "What is an Export Provider" post or documentation is coming, maybe this'll boost that necessity!) What I ultimately came up with was a much cleaner solution than using a FactoryPartCatalog.

 

Introducing FactoryExportProvider:

   1:  public class FactoryExportProvider<T> : ExportProvider {
   2:     public FactoryExportProvider(Func<Type, T> resolutionMethod) { }
   3:     public FactoryExportProvider(Assembly assembly, Func<Type, T> resolutionMethod) { }
   4:     public FactoryExportProvider(IEnumerable<Type> types, Func<Type, T> resolutionMethod { }
   5:  }

What you'll see is again, it's very straight forward and provides a clean implementation in usage, just as the part catalog example did. Each constructor does just as it had done in the part catalog, so I'll not explain that again. One thing that I discussed with Glenn about was, is it appropriate to look for certain types within an Export Provider? His response was "absolutely you can do that". good, I think I've found the correct implementation, both from less code/object graph standpoint, but also from an intention standpoint.

 

Internally, the code is very simplistic. Since I'm finding these interfaces on-the-fly, and need more information than just the contract name, I needed to use a FactoryExportDefinition to store this information. I've you've looked at the prior example, you'll see this came back out of necessity.

   1:  public class FactoryExportDefinition<T> : ExportDefinition {
   2:     public FactoryExportDefinition(string contractName, Type type, Func<Type, T> resolutionMethod) { }
   3:   
   4:     public override string ContractName { get { ... } }
   5:     public Type ServiceType { get; private set; }
   6:     public Func<Type, T> ResolutionMethod { get; private set; }
   7:  }

When finding all of the interfaces that implement the base interface specified in the FactoryExportProvider, I convert those into a list of FactoryExportDefinition objects. Reason being, is that the export provider compares an ImportDefinition to an ExportDefinition when finding all available exports. This comparison is done by implementing the GetExportsCore method. The idea of export providers, is that when resolving all dependencies, MEF will call into all of the registered ExportProviders to determine if they can supply the Export and will do a bunch of cardinality matching for you. Out of the box, MEF provides an export provider for it's registered part catalogs. Here's the FactoryExportProvider's implementation of GetExportsCore.

   1:  protected override IEnumerable<Export> GetExportsCore(ImportDefinition importDefinition) {
   2:     IList<Export> exports = new List<Export>();
   3:     var constraint = importDefinition.Constraint.Compile();
   4:     var foundExports = from d in definitions
   5:                        where constraint(d)
   6:                        select new Export(d, () => d.ResolutionMethod(d.ServiceType));
   7:   
   8:     if (importDefinition.Cardinality == ImportCardinality.ZeroOrMore)
   9:        exports = foundExports.ToList();
  10:     else if (foundExports.Count() == 1)
  11:        exports.Add(foundExports.First());
  12:   
  13:     return exports;
  14:  }

It's that simple. The Export's that are returned will have the resolution method called when the actual object is needed. When it comes down to including this within your application, it's just as easy as it was for the part catalog, you just register things a bit differently.

   1:  public interface IService { }
   2:  public interface IUserService : IService { }
   3:   
   4:  [Export]
   5:  public class UserController {
   6:     [ImportingConstructor]
   7:     public UserController(IUserService userService) { }
   8:  }
   9:   
  10:  // in your application
  11:  private void Compose() {
  12:     var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
  13:     var factoryProvider = new FactoryExportProvider<IService>(GetService);
  14:     var container = new CompositionContainer(catalog, factoryProvider);
  15:     container.AddPart(this);
  16:     container.Compose();
  17:  }
  18:   
  19:  public IService GetService(Type type) { return ... }

And that's it. Ultimately, this leads to a cleaner implementation that uses less types that you have to manage, and, adheres to the correct intentions of the framework. Much thanks to Glenn who I chatted with for several hours last night! You can get the downloadable source here. Enjoy!

 

kick it on DotNetKicks.com


Currently rated 3.6 by 7 people

  • Currently 3.571429/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: .NET | MEF | Programming
Actions: E-mail | Permalink | Comments (74) | Comment RSSRSS comment feed

Comments

December 1. 2008 22:29

Pingback from weblogs.asp.net

MEF + Factories Using an Export Provider - eWorld.UI - Matt Hawley

weblogs.asp.net

December 2. 2008 13:46

Pingback from blogs.msdn.com

My Technobabble : Export Providers and Custom factories with MEF

blogs.msdn.com

December 2. 2008 14:26

Pingback from feeds.feedburner.com

Export Providers and Custom factories with MEF - Glenn Block

feeds.feedburner.com

December 2. 2008 14:43

Pingback from codebetter.com

Export Providers and Custom factories with MEF - Glenn Block

codebetter.com

December 4. 2008 17:15

Pingback from taccato.com

Export Providers and Custom factories with MEF - taccato! trend tracker, cool hunting, new business ideas

taccato.com

April 23. 2009 14:08

very goods another too be me.

saç ekimi

May 22. 2009 05:47

I have been working on MEF for couple on year now and i dint find anything like this before.Thanks a lot

Define

May 22. 2009 05:51

Very nice post.I was looking for it for quite a sometime now

Lennox Furnace Filters

May 22. 2009 23:33

Thanks for the very informative post!It's good somebody takes real interests in these things and shares them.

Bakersfield dating

June 4. 2009 14:19

I n the internet, it really is a wonder how there will always be people to make things easier when things are not that easy yet. Just like this post here.

Thanks for sharing. It was helpful.

Milan Flights

June 5. 2009 23:55

A nice article on Factories using Export Provider. Thanks for this helpful post.

Website Design

June 22. 2009 12:48

Wow nice info you have here. I hope this will help a lot of people. I will tell my friends to read this. Thanks

Allan

June 29. 2009 00:59

Very Informative. One hell of a code you got here. This is worth reading. Thanks!

Star Trampoline

July 16. 2009 10:03

I guess they never really run out of codes huh.

Frankfurt Flights

July 16. 2009 11:57

I love reading this post! It updates me with the technique and style. The codes are very logical for positive result. Thanks for sharing.

Philadelphia Plastic Surgery

July 16. 2009 12:04

These simple coding could maximize business opportunity. This could make another logical reason why we need pursue in every goal since there is technology that could guide as towards success. Very useful source! Thanks for posting...

Receptionist

July 23. 2009 01:06

I dint find anything like this before.Thanks a lot

Civil Engineering Diploma

July 23. 2009 01:06

nice post.

PhD history

July 23. 2009 01:07

Thanks for sharing.

Journalism degree

July 23. 2009 01:07

This is really a great stuff for sharing.keep it up

Nursing degree

July 23. 2009 01:07

Information is truly extraordinary.

Law degree

August 1. 2009 03:30

Great codes you got here. These a are just simple codes but it can really make business processes more efficient. Thanks for sharing this very useful information.

Virtual assistants

August 1. 2009 07:35

MEF offers a great way for the .net developers to reuse applications and components thus it cause a great impact to lessen development time. Thanks for sharing.

Copywriting services

August 1. 2009 19:56

Nice post! My cousin has been looking for this kinds of posts and I am glad I could help him thru your post. I hope you could make a series on this. Thank you.

hunting

August 10. 2009 00:56

This is really a great stuff for sharing.keep it up

tiffany

August 11. 2009 18:41

Thank you admin for this perfect article

sildenafil 100mg

August 12. 2009 20:31

Learn how to use the new SubDataItems within the ASP.NET MVC Preview 3 bits with your View User Control. Also explains a small patch to the framework to get this to work correctly

Phd Thesis

August 14. 2009 23:12

I just used ExportProvider and I found it to be very useful.

Online Master's Degrees

August 24. 2009 01:55

i work for a place that uses export providers

web hosting

August 26. 2009 13:20

Very interesting post. Might be old, but it was new to me. Good thing you have come up with such blog. I thought it was going to be some boring old post, but it really compensated for my time. Nice blog. I just bookmarked it for later reference. Good job.

travel nursing

August 30. 2009 17:08

Where can I get the documentation of export provider

Daniel

September 3. 2009 18:15

I just used ExportProvider and I found it to be very useful.

hemmeroid treatment

September 3. 2009 18:15

Learn how to use the new SubDataItems within the ASP.NET MVC Preview 3 bits with your View User Control. Also explains a small patch to the framework to get this to work correctly

transmission jack

September 3. 2009 18:16

MEF offers a great way for the .net developers to reuse applications and components thus it cause a great impact to lessen development time. Thanks for sharing.

car floor jack

September 9. 2009 05:02

Very interesting post. Might be old, but it was new to me. Good thing you have come up with such blog. I thought it was going to be some boring old post, but it really compensated for my time. Nice blog. I just bookmarked it for later reference. Good job.

eunq

September 9. 2009 05:05

good, thanks

online tv

September 11. 2009 15:26

lazer epilasyon and laser hair removal webpage: http://www.lazerepilasyon.org/

lazer epilasyon

September 16. 2009 15:13

We need to learn more from this. These a are just simple codes but it can really make business processes more efficient. Thanks for sharing this very useful information.
Regards,

Dissertation Online

September 16. 2009 15:14

It is wanna a great thing I had it here. It is really an quality posting.
Regards,

Doctoral Thesis

September 17. 2009 22:40

it really compensated for my time. Nice blog. I just bookmarked it for later reference. Good job.

Persuasive Essay

September 17. 2009 22:41

I hope you could make a series on this. Thank you for sharing.

College Term Paper

September 18. 2009 04:41

I'm explicitly violating that rule as I'm finding interfaces based on a base interface, not by Export! At this point, my mind started churning - mainly because there's not a lot of examples or descriptions of what an ExportProvider actually is - but because I wanted to do things "correctly" according to the framework provided

Free online games

September 22. 2009 08:35

very nice is the code free to use for public use.

van leasing

September 25. 2009 14:42

Thank you for the code, i have learned a lot from it! thanks once again.

Ameda ultra

September 25. 2009 23:22

Export Providers and Custom factories with MEF - taccato! trend tracker, cool hunting, new business ideas

how to quit smoking

September 26. 2009 18:15

Thanks for the article, You are really a master of what you're doing. This problem is not an easy one to solve and its great to see you came around to make it work perfectly.

Free Internet Advertising

October 1. 2009 10:10

very informative.These simple coding could maximize business opportunity. This could make another logical reason why we need pursue in every goal since there is technology that could guide as towards success. Very useful source! Thanks for posting...

Custom Thesis

October 2. 2009 05:07

My mind started churning - mainly because there's not a lot of examples or descriptions of what an ExportProvider actually is - but because I wanted to do things "correctly" according to the framework provided.Thanks for shareing.
Regards,

forex trading software

October 3. 2009 23:56

Thanks for the code, if only all bloggers offered the same content as you, the internet would be a much better place.

Buy Dissertation

October 6. 2009 12:54

I have been working on MEF for couple on year now and i dint find anything like this before.Thanks a lot

auto insurance

October 8. 2009 12:37

Generic Viagra Online Pharmacy. Buy Viagra Online and Order Cheap Viagra Prescription with Satisfaction Guarantee.

Buy Viagra

October 9. 2009 22:33

Thanks for the code, if only all bloggers offered the same content as you, the internet would be a much better place.

live score board

October 9. 2009 22:34

very nice is the code free to use for public use.

live football results

October 9. 2009 22:35

It is wanna a great thing I had it here. It is really an quality posting.

online cheap Viagra

October 9. 2009 22:36

good, thanks

kamagra uk

October 9. 2009 22:36

We need to learn more from this

kamagra jelly

October 10. 2009 19:19

This is the best post on this topic i have ever read.


Regards

Morgan

cell phone watch

October 11. 2009 07:14

Great stuff here, really thought that was interesting.

poor credit loans

October 12. 2009 20:20

I added your post to my college Report


Regards

Lusty

watch glee

October 13. 2009 21:57

thanks for the post

medifast coupon

October 15. 2009 17:48

I always wanted to write in my site something like that but I guess you'r faster Smile

personal loans

October 15. 2009 17:54

I guess there's always an easier way ...

personal loans

October 16. 2009 23:38

great

vigrx plus

October 17. 2009 00:14

We need to learn more from this. These a are just simple codes but it can really make business processes more efficient. Thanks for sharing this very useful information.

free online games

October 17. 2009 01:36

Yes

Thanks for the very informative post. It's good someone takes real interests in these things and shares them us.

Air Filters

October 17. 2009 02:02

I can't find these info useful for me but thanks for your work!

book reports

October 20. 2009 12:42

I added your post to my college Report


Regards

Welson

beat a marijuana drug test

October 21. 2009 02:44

Yap I'm explicitly violating that rule as I'm finding interfaces based on a base interface, not by Export! At this point, my mind started churning - mainly because there's not a lot of examples or descriptions of what an ExportProvider actually is - but because I wanted to do things "correctly" according to the framework provided

free online games

October 21. 2009 03:34

It's useful

ed hardy

October 21. 2009 06:41

At this point, my mind started churning - mainly because there's not a lot of examples or descriptions of what an ExportProvider actually is - but because I wanted to do things "correctly" according to the framework provided.Thanks for sharing the posts.
Regards,

Apex Professionals LLC

October 21. 2009 17:16

Another helpful post on your blog, you have been quite a source!

underground electric dog fence

October 21. 2009 22:01

I'm not surprised that factories are using it too.

Legitimate Work At Home Jobs Opportunity

October 21. 2009 22:37

thanks for sharing this
<a href="http://www.zimbio.com/Print+Place/articles/138/Business+Cards+Which+Company">cheap business cards</a>

cheap business cards

October 22. 2009 02:39

Good post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.
cervical incompetence

order epson ink cartridges

Comments are closed

Copyright © 2000 - 2010 , Excentrics World