Home Managed Extensibility Framework (MEF) and Asynchronous imports?
Reply: 0

Managed Extensibility Framework (MEF) and Asynchronous imports?

user2025 Published in April 22, 2018, 10:07 pm

Let me first explain my original implementation WITHOUT MEF, and how I plan to change it using MEF.

In an MVC controller I must return a serialized json object. This object is built from first running an asynchronous database query. I then parse the results of that query and build an object, and return it. Easy.

But there are actually multiple queries. And the parsing procedure is complex. Traversing the many steps that are taken to build the return object is hard and if the order of operations isn't performed correctly then it leads to errors.

Then I thought, what if I could use MEF to make my code more "declarative" and less procedural. At a high level I would simply instantiate a POCO class in the controller:

public dynamic Get(int id)
     WebForm form = container.GetExport(typeof(WebForm));

     return Json(form);

And WebForm would look like this:

internal class WebForm : DataEntryForm
    public Field1 field1 { get; set; }


And "field1" One would look like this:

public class Field1 : TextBox

    public Field1() {
        label = **[DO DATABASE QUERY TO GET VALUE]**;

    public string label { get; set; }

By organizing my code this way, I feel like the developer could visualize the end result much better. And also you don't have to worry about order of operations as much with MEF.

The trouble is using asynchronous database queries in this model. As you can see by this simplified example, I'm running a query in the constructor because I want "label" property to be set before the import is satisfied. That way it's serialized properly.

C# doesn't support "asynchronous constructors" so I can't really do it this way.

So what I'm wondering is, can I somehow delay the import until an asynchronous method completes somehow while still retaining this declarative design?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30508 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO