.NET Framework Developer Center >
.NET Development Forums
>
Managed Extensibility Framework
>
What is MEF in clear words that every newbie will catch ??
What is MEF in clear words that every newbie will catch ??
- Dear all,
I have read some styuff about MEF but I did not have a clear view about what all those terms used are lie IOC, injection ...
IN addition I could find real world example project that could ring my bel and telling me "humm right then I catch and will use it when I have the use for it"
To used it we need to catch the idea behind and what are the scenarios where it can be used, becasue sometimes we are in a project and we used how old style to make things until you heard about something new which is so unclear that you skip it.
Anyone could explain in clear words what is all about with real world sample project scenario ?
thnaks for help
serge
Your experience is build from the one of others
All Replies
- Taking a few shortcuts here but its basically a plug-in system. The system scans a folder for plugins and makes sure if plugin A needs interface B to work properly that it will be given that (if its available). Real world sample uhhh have you looked at the samples in the latest code drop?
- Where are thos sample code ?
Your experience is build from the one of others - If you get the latest preview release (5), they're in the "Samples" folder.
- What is it?
It's a namespace filled with lovely classes that allow you to write your code more dynamically.
How much more dynamically?
O, a lot lot lot more dynamically. Ofcourse, you have to prepare your code for the injection. This means you have to anticipate the injection.
Say whoooot?
Let's pretend we want to write a certain program to show you what MEF could do. Actually, it's not pretending, I wrote the program a couple of weeks ago.
The program is a client-server program installed at a school. In each classroom, the students have a program (client) on their pc, and the teacher has another program (server) on his. With the server program, the teacher can communicate with the client programs. This means, the server can ask the client information (who is logged in, what processes is the student running, ...), or ask the client to do something (pop up a message saying: "pay attention", start a process, close that internet explorer window that has "facebook" in the title, ...), or...
To communicate with the client, the server sends a Request (using a networkstream). At first, I had two kinds of Requests, being: InformationRequest and ActionRequest. When the client receives a Request, he has to do whatever is asked, and send a response. To code this, I wrote something called a RequestHandler. As the name reveales, this is an object that is able to handle a Request, do whatever is needed, and generate a response. The client keeps a list of his RequestHandlers (and as you might suspect, I had in my list only two handlers: one that handles InformationRequests, and one that handles ActionRequests), and when any Request arrives at the client, it goes trough that list to find the correct handler.
Basic flow:
SERVER => InformationRequest / ActionRequest => CLIENT => finds a handler in the list => the handler handles the request => Response => SERVER => everybody happy.
If everybody's happy, why do we need MEF?
All of the sudden a teacher came to me and to says: "yea that's pretty nice that I'm (server) now able to ask information from, or do something on, the student's pc (client), but what I'd really really want is that I can SEE the student's pc, so I can help him/her out, from behind my desk."
So I'm thinking... Hmm ok, how will I code this??? What do I need? I came up with this:
1. I need a new kind of Request. InformationRequest and ActionRequest will get too crowded (too many methods, properties, ...) if I implement this , I'll create a TakeOverDesktopRequest.
2. If the client recieves this TakeOverDesktopRequest, he'll have no idea what to do because there's no handler for it. So I need something, a new RequestHandler that is able to do the nessecary work.
3. I need to re-install the new version of my program on all 950 computers available in the school.
So I'm thinking: step1 & step 2 are easy, I've done it a couple of times before. Step3? "O man, it's going to take me aaaaaggges... 950 computers... And if some other schools that use my program hear about this they'll want it too... O man o man!!!"
But then I remembered the client keeps a list of RequestHandlers. Wouldn't it be nice if I could just write the new TakeOverDesktopHandler, and poof, that handler would appear in the client's list? No recompiling, or re-installing needed. Just write the new handler, compile it in a DLL and then take the client in one hand, a big needle in the other, and inject my new handler in the list?
Because the above has happened to me more then one times too many before, I've learned to MEFify my applications. MEF, is the needle. It allows me to put a DLL on a computer, and a program, even if it was already running, even if it doesn't know the classes in that DLL, and MEF will inject what I want, where I want it.
New flow:
1. SERVER => Request => CLIENT => finds a handler in the list => the handler handles the request => Response => SERVER => everybody happy.
2. SERVER => NewRequest => CLIENT => finds no handler => tells the server...
SERVER => Request: Install Plugin => CLIENT...
SERVER => NewRequest => CLIENT => finds a handler in the list => the handler handles the request => Response => SERVER => everybody happy.
Hope you understand the example and MEF makes more sence to you now. Ofcourse, I had to simplify the real program a bit, in reality, your program will need some extra lines to be dynamic. But it's worth the extra effort. Just think about these other real life examples:
1. Windows Vista sidebar, you can download extra components, but when the sidebar was created, it didn't know every available component... => Plugin
2. MSN Messenger. You can play a game with the person you are chatting with. MSN didn't know every kind of game, but still you can download and play new games together => plugin.
3. World of warcraft (it's a game) => Hundreds or thousands of "addons" available. Just download, put them in your WOW folder, start the game, and they work... (most of the times...)
Happy coding!
JR
"The improbable we do, the impossible just takes a little longer." (Steven Parker)


