Instantiation via Class Name

C

Chris Marsh

All

Good day to everyone!

I am developing a system which interfaces with various external systems. The
set of external systems will change and grow over time. I will produce a new
class to represent each of these external systems, and each of these
classes will implement an interface that I have designed (IExternalAccount).
What I want to do is ensure that a) I need to make minimal code changes in
order to introduce a new implementation of the interface IExternalAccount to
the system, and b) I can control which external systems are included in my
system via my config file.

The approach that I'm taking is to have an entry in the config file, the
value for which is a comma delimited list of class names. I access this
value from my code, then create an object instance of each of the classes
listed. These are then added to a collection, and then processed.

My questions are:

1. Is this a good approach?

2. Can someone suggest how to implement this design (or point me in the
right direction)?

Many thanks in advance for your assistance!
 
M

Mattias Sjögren

Chris,
1. Is this a good approach?

The general idea is fine and quite common solution for a dynamic,
pluggable architecture.

I'm not sure I'd use a single comma-separated string to hold all class
names though, at least if you need fully qualified type names (if the
classes are implemented in different assemblies). It could easily
become very unreadable, and you can do better with XML.

2. Can someone suggest how to implement this design (or point me in the
right direction)?

I'm sure you can find examples if you search for "add-in" or "plug-in"
(along with ".NET" and/or your programming language of choice).


Mattias
 
S

Steven Cheng[MSFT]

Hi Chris,

Yes, I agree with Mattias that your plan on using reflection to dynamically
load assembly and activate class instances(for pluggable system) is
reasonable. Most managed add-in like application adopt this design. Here
are some reference articles discussing on built pluggable /add-in system
through .net managed code:


#NET Based Add-in/Plug-in Framework with Dynamic Toolbars and Menus
http://www.codeproject.com/cs/library/Net_AddinProjFrmwork.asp

#Managed AddIn Framework (MAF)
http://blogs.msdn.com/jackg/archive/2005/09/15/468068.aspx

and you can also look for some .NET reflection and dynamici code execution
related reference articles which are also helpful.

#Using reflection to extend .NET programs
http://www.codeproject.com/csharp/reflection.asp

#Programming with Application Domains and Assemblies
http://msdn2.microsoft.com/en-us/library/dah4cwez(VS.71).aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sloan

Most likely you'll want to write a custom configuration section.

12/1/2005
Understanding the Simple Factory Pattern
http://sholliday.spaces.live.com/blog/

You want #3
3. Using reflection. You can setup the concrete class in a configuration
file.

Code sample/download available at the blog.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top