473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reflecting Factory Interfaces across Assemblies

This is a technically challenging question but it would help me significantly
if anyone can answer it.

In essence I have the following:

A factory class, which can create producer classes which inherit an
interface (standard factory design pattern).

I would like to make this more versatile and do the following:

I would like to be able to specify the producer classes in a config file and
then call them at runtime. This would allow me to create the producer class
in a separate assembly and then use reflection to call the functions.

Does anyone have a Factory class which could do this?

The bit I am finding most difficult is the reflection. So any help on this
would be appreciated.

Mark
Jul 21 '05 #1
2 1708
> I would like to be able to specify the producer classes in a config file and
then call them at runtime. This would allow me to create the producer class
in a separate assembly and then use reflection to call the functions.


In your config:

<add key="FactoryInstance" value="SomeNameSpace.SomeType,
SomeNameSpace.dll" />

Your factory interface:

public interface IMyFactoryInterface {
IProducer CreateInstance();
}
Your instance - in the assembly SomeNameSpace.dll:

public class Factory : IMyFactoryInterface {
public IProducer CreateInstence() {
return new Producer();
}
}
Get the factory:

[..]
IMyFactoryInterface currentFactroy = null;
string[] factorySettings =
System.Configuration.ConfigurationSettings.AppSett ings["FactoryInstance"].ToString().Split(',');
string type = factorySettings[0].Trim();
string assembly = factorySettings[1].Trim();
currentFactory = Assembly.Load(assembly).CreateInstance(type) as
IMyFactoryInterface;

//Get the producer instance
if (currentFactory != null) {
IProducer producer = currentFactory.CreateInstance();
}
[..]

You'll need to add exception handling your self.

/B.factoring

Jul 21 '05 #2
Bjarke Lindberg wrote:
I would like to be able to specify the producer classes in a config
file and then call them at runtime. This would allow me to create the
producer class in a separate assembly and then use reflection to call
the functions.


Ups.. I misread your question - it wasn't the factory class you wanted
to be configurable.. (Yeah - I know I should read twice <hrmpf.wav>)

Anyway - it's almost the same principle. But why would you call the
methods via reflection?

Anyway - in your factory, just do the same, as we did in the previous
post, when getting the Factory.

public class Factory {
public object CreateProducer() {
//get the configured procucer class / assembly
//load the assembly
return
MyAssembly.CreateInstance(configuredProducerName);
}
}
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
4
by: Robert Zurer | last post by:
I notice that Microsoft puts their interfaces and classes in the same assembly. For example System.Data contains OleDbConnection and IDbConnection etc. I have found it useful to keep my...
2
by: Julia | last post by:
Hi, I have an application composed from layers like the following A --B ---C A is the top layer C uses an Abstract Factory to Create Concrete classes
5
by: Michael McCarthy | last post by:
I want to develop plugin support for a system.montitor module I am working on. A lot of the modules will do mostly interop stuff for an older system, but I want to use it myself as well to monitor...
2
by: mag31 | last post by:
This is a technically challenging question but it would help me significantly if anyone can answer it. In essence I have the following: A factory class, which can create producer classes which...
7
by: Jeff S | last post by:
I'm relatively new to non trivial OOP programming and recently stumbled across the idea of Factory classes that can create objects at runtime. I know we can hard-code class definitions for use by...
6
by: GarrettD78 | last post by:
Accidently posted this to the wrong group so I am reposting. This is probably a newbie question but I am a little confused about how to go next with my code. I think I want to use a factory pattern...
7
by: Steven T. Hatton | last post by:
I have a couple questions about the design pattern presented in the example quoted below. I can appreciate why the destructor is protected, but why is it not virtual? I am forced to assume that I...
6
by: GroupReader | last post by:
In my app, I have two very similar static classes. After long thought, I've decided *yes - keep them static*. - Sometimes I will want to use Static Class A, and somtimes I will want to use...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.