Circular Reference

  • Thread starter Thread starter Mercede
  • Start date Start date
M

Mercede

Hi,

I've a certain problem. I've created a Class Library project that contains
the following

- A Factory Class
- A base Object

A factory can create the Base objects or objects derived from base objects.
The base object Constructor "New" is Friend and Protected. it's protected
sp that Child Classes can call it. and it's friend so that Factory Class can
Create objects of Type Base. I don't want anuone to directly create objects
of type Base or derived classes by calling New.

I've other components that are in seperate DLL(Let's say X). These
components derive from Base object. I've to add a refrence of Factory Class
Library. This goes Fine but as soon as I add a refrence of the X DLL so that
Factory can create the object of type X it fails. I start getting errors in
Factory like

Add a refrence to factory Assembly
New is not accesible

etc etc

I could do it by having classes implement an interface rather than deriving
from the base buit then there is so much code in Base that will be
duplicated. Any idea.

Mercede
 
Mercede,
I could do it by having classes implement an interface rather than deriving
from the base buit then there is so much code in Base that will be
duplicated. Any idea.
You were close to the right idea. ;-)

I would use the Separated Interface Pattern:
http://www.martinfowler.com/eaaCatalog/separatedInterface.html

To implement it in your case I would change Base to be an abstract class
(MustInherit class). Which prevents others from instantiating it. I would
change the constructor to only Protected, as you need to derive from the
class to create an instance of the class.

I would move Base into its own assembly so that other assemblies in addition
to the Factory assembly can reference it.

In the Factory assembly , which can reference all the assemblies including
the Base assembly I would have a BaseImpl class that inherits from Base,
BaseImpl would be Friend as the class itself is not visible outside the
assembly. This would be the actual class that the Factory returns when asked
to create Base objects (depending on how you ask Factory to create objects
this may entail special logic). Factory can return instance of BaseImpl as
a Base type, although BaseImpl is friend to the Factory Assembly.
The base object Constructor "New" is Friend and Protected. it's protected
sp that Child Classes can call it. and it's friend so that Factory Class can
Create objects of Type Base. I don't want anuone to directly create objects
of type Base or derived classes by calling New.
Huh? a protected new allows derived classes.

Hope this helps
Jay
 
Back
Top