472,991 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

OOP Question - Pattern?


I have a multi team solution being built where teams are segmented off
into seperate projects. One project is an over all framework containing
a Broker [worries about which db to access for data] class responsible
for instantiating different subclasses of type Connector [connects to
database - db specific] - the class only contains the abstract Connector
class and the Broker.

A seperate project contains the actual subclasses of the Connector that
are database specific. Another team will work exclusivley in that
project implementing these things.

Now the Broker class needs to be able to open the subclass dynamically.
Is there a CreateObject function that lets me pass an assembly name as
a string to some create method in order to instantiate the appropriate
object? And will I have a problem with this set up because project B
[Connector Implementations] will need to reference Project A [connector
abstract] and project A will need to know about the sub classes. Am i
missing a pattern here? The reason I need to instantiate via a string
name is so that project A doesn't need references to the subclasses.

This is my first OOP project so I might be missing something obvious.

--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #1
8 2404
Certainly I would suggest that instead of having an abstract Connector class
you could have an IConnector interface. This will help with your decoupling
because there is no abstract class which must be inherited, it allows
Project B to design its class hierarchy without referencing yours and can
implement the IConnector interface on any class it chooses.

You will need to use reflection to get at the classes to instantiate,
presumably there will be a configuration file somewhere naming the
classes/assemblies, once you have that you can use calls such as
System.Activator to create instances.

There are some issues with dynamically loading assemblies that using
interfaces also helps to ameliorate, the main thing is that type identity is
often not preserved between assemblies (i.e. two instances created by two
different assemblies may not be considered to be the same type.)

S.

The broker would not need to know about
"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...

I have a multi team solution being built where teams are segmented off
into seperate projects. One project is an over all framework containing
a Broker [worries about which db to access for data] class responsible
for instantiating different subclasses of type Connector [connects to
database - db specific] - the class only contains the abstract Connector
class and the Broker.

A seperate project contains the actual subclasses of the Connector that
are database specific. Another team will work exclusivley in that
project implementing these things.

Now the Broker class needs to be able to open the subclass dynamically.
Is there a CreateObject function that lets me pass an assembly name as
a string to some create method in order to instantiate the appropriate
object? And will I have a problem with this set up because project B
[Connector Implementations] will need to reference Project A [connector
abstract] and project A will need to know about the sub classes. Am i
missing a pattern here? The reason I need to instantiate via a string
name is so that project A doesn't need references to the subclasses.

This is my first OOP project so I might be missing something obvious.

--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #2
Shaun,

It shouldn't be a problem. As long as your main assembly loads all of
the types that are needed by the base classs, you should be fine. So, the
things that you need to make sure you load are:

- The assembly that has the type that is the base.
- The assembly that has any interfaces that the base might implement.
- The assemblies that have the types of all the parameters, return values,
and properties.

If your derived class uses a type that is in another assembly, but the
base class doesn't expose that type, then you don't have to worry about it.

So, that being said, you can load an assembly dynamically using the
static Load method on the Assembly class.

Once you have that, you can create an instance of a type using the
static CreateInstance method on the Activator class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...

I have a multi team solution being built where teams are segmented off
into seperate projects. One project is an over all framework containing
a Broker [worries about which db to access for data] class responsible
for instantiating different subclasses of type Connector [connects to
database - db specific] - the class only contains the abstract Connector
class and the Broker.

A seperate project contains the actual subclasses of the Connector that
are database specific. Another team will work exclusivley in that
project implementing these things.

Now the Broker class needs to be able to open the subclass dynamically.
Is there a CreateObject function that lets me pass an assembly name as
a string to some create method in order to instantiate the appropriate
object? And will I have a problem with this set up because project B
[Connector Implementations] will need to reference Project A [connector
abstract] and project A will need to know about the sub classes. Am i
missing a pattern here? The reason I need to instantiate via a string
name is so that project A doesn't need references to the subclasses.

This is my first OOP project so I might be missing something obvious.

--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #3
Where do you suggest the interface reside? Project A or B?

There will be a database table that the broker will look up the object
it needs to open up using parameters it recieves from the caller.

So then it's

GetNameOfAssembly/Class to open
Load Assembly [using system..]
Instantiate Class [using reflection]
unload Assembly when done.
Simon Trew wrote:
Certainly I would suggest that instead of having an abstract Connector class
you could have an IConnector interface. This will help with your decoupling
because there is no abstract class which must be inherited, it allows
Project B to design its class hierarchy without referencing yours and can
implement the IConnector interface on any class it chooses.

You will need to use reflection to get at the classes to instantiate,
presumably there will be a configuration file somewhere naming the
classes/assemblies, once you have that you can use calls such as
System.Activator to create instances.

There are some issues with dynamically loading assemblies that using
interfaces also helps to ameliorate, the main thing is that type identity is
often not preserved between assemblies (i.e. two instances created by two
different assemblies may not be considered to be the same type.)

S.

The broker would not need to know about
"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
I have a multi team solution being built where teams are segmented off
into seperate projects. One project is an over all framework containing
a Broker [worries about which db to access for data] class responsible
for instantiating different subclasses of type Connector [connects to
database - db specific] - the class only contains the abstract Connector
class and the Broker.

A seperate project contains the actual subclasses of the Connector that
are database specific. Another team will work exclusivley in that
project implementing these things.

Now the Broker class needs to be able to open the subclass dynamically.
Is there a CreateObject function that lets me pass an assembly name as
a string to some create method in order to instantiate the appropriate
object? And will I have a problem with this set up because project B
[Connector Implementations] will need to reference Project A [connector
abstract] and project A will need to know about the sub classes. Am i
missing a pattern here? The reason I need to instantiate via a string
name is so that project A doesn't need references to the subclasses.

This is my first OOP project so I might be missing something obvious.

--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com



--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #4
Shaun,

You should place the interface in another assembly, project C and have
both assemblies reference it.

Also, the last step of the process can not be done, as you can not
unload assemblies from an app domain once you have loaded them into it. You
could create a separate app domain and access it through there, but that
might be too much overhead.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:3F**************@dteenergy.com...
Where do you suggest the interface reside? Project A or B?

There will be a database table that the broker will look up the object
it needs to open up using parameters it recieves from the caller.

So then it's

GetNameOfAssembly/Class to open
Load Assembly [using system..]
Instantiate Class [using reflection]
unload Assembly when done.
Simon Trew wrote:
Certainly I would suggest that instead of having an abstract Connector class you could have an IConnector interface. This will help with your decoupling because there is no abstract class which must be inherited, it allows
Project B to design its class hierarchy without referencing yours and can implement the IConnector interface on any class it chooses.

You will need to use reflection to get at the classes to instantiate,
presumably there will be a configuration file somewhere naming the
classes/assemblies, once you have that you can use calls such as
System.Activator to create instances.

There are some issues with dynamically loading assemblies that using
interfaces also helps to ameliorate, the main thing is that type identity is often not preserved between assemblies (i.e. two instances created by two different assemblies may not be considered to be the same type.)

S.

The broker would not need to know about
"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
I have a multi team solution being built where teams are segmented off
into seperate projects. One project is an over all framework containing
a Broker [worries about which db to access for data] class responsible
for instantiating different subclasses of type Connector [connects to
database - db specific] - the class only contains the abstract Connector
class and the Broker.

A seperate project contains the actual subclasses of the Connector that
are database specific. Another team will work exclusivley in that
project implementing these things.

Now the Broker class needs to be able to open the subclass dynamically.
Is there a CreateObject function that lets me pass an assembly name as
a string to some create method in order to instantiate the appropriate
object? And will I have a problem with this set up because project B
[Connector Implementations] will need to reference Project A [connector
abstract] and project A will need to know about the sub classes. Am i
missing a pattern here? The reason I need to instantiate via a string
name is so that project A doesn't need references to the subclasses.

This is my first OOP project so I might be missing something obvious.

--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com



--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #5
Shaun,

See inline:

"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Unsure about what you're saying here.

Are you calling my project a, which contains all the base classes which
are inherited from, the main assembly? Project B classes is inheriting
from these classes in Project A.
The main assembly is the assembly that has the base classes, so yes,
project A.

So projA.DLL will be loaded. ProjA has the Broker class that will call
classes in ProjB.DLL
> - The assembly that has the type that is the base. This is projA.DLL
> - The assembly that has any interfaces that the base might implement.

The base class isn't implementing anything - it being a base class. Am
i misunderstanding you here?


I am just thinking that the base class might have interface
implementations as well.

> - The assemblies that have the types of all the parameters, return values, and properties.

You mean the implemented subclasses? Not sure of what you're referring
to here either.


What I mean is that say the Broker base class returns an object of type
X. You have to make sure that the controller for this loads not only
project A, but the assembly containing X as well.

> If your derived class uses a type that is in another assembly, but the
> base class doesn't expose that type, then you don't have to worry

about it.

The derived class derives from a class in ProjA.DLL. So doesn't my
class expose whatever type is needed because it has to be visible to the
outside world.


Only if it is visible to the outside world. However, when I say
"doesn't expose that type", it means that you are using it to implement
something internally, without exposing it to the outside world. So if your
derived class uses classes in System.Management.dll internally, you don't
need it in your controller.


I might have class and type confused here.

I am using class and type interchangably here.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com


Nicholas Paldino [.NET/C# MVP] wrote:
Shaun,

It shouldn't be a problem. As long as your main assembly loads all of the types that are needed by the base classs, you should be fine. So, the things that you need to make sure you load are:

- The assembly that has the type that is the base.
- The assembly that has any interfaces that the base might implement.
- The assemblies that have the types of all the parameters, return values, and properties.

If your derived class uses a type that is in another assembly, but the base class doesn't expose that type, then you don't have to worry about it.
So, that being said, you can load an assembly dynamically using the
static Load method on the Assembly class.

Once you have that, you can create an instance of a type using the
static CreateInstance method on the Activator class.

Hope this helps.


--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #6
Ok project A will not have to implement IConnector at all - only project
B will implement. Project A will be instancing types in Project B which
implement IConnector. So Project A can still have the IConnector
interface right since he really doesn't care about implementing, he just
wants to instance up things of type IConnector [located in B], wahtever
they may be. Do i still need to have project C?
Nicholas Paldino [.NET/C# MVP] wrote:
Shaun,

You should place the interface in another assembly, project C and have
both assemblies reference it.

Also, the last step of the process can not be done, as you can not
unload assemblies from an app domain once you have loaded them into it. You
could create a separate app domain and access it through there, but that
might be too much overhead.

Hope this helps.


--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #7
Shaun,

You don't NEED it, as project B can always reference project A to get
the interface.

While it won't apply in this case, if you have a situation where project
A needs to reference project B directly, you won't be able to do it, because
it would create a circular reference between assemblies, which is
disallowed. Placing the type definitions shared between the two assemblies
in another assembly will elimate this.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Shaun C Farrugia" <fa*******@dteenergy.com> wrote in message
news:3F************@dteenergy.com...
Ok project A will not have to implement IConnector at all - only project
B will implement. Project A will be instancing types in Project B which
implement IConnector. So Project A can still have the IConnector
interface right since he really doesn't care about implementing, he just
wants to instance up things of type IConnector [located in B], wahtever
they may be. Do i still need to have project C?
Nicholas Paldino [.NET/C# MVP] wrote:
Shaun,

You should place the interface in another assembly, project C and have both assemblies reference it.

Also, the last step of the process can not be done, as you can not
unload assemblies from an app domain once you have loaded them into it. You could create a separate app domain and access it through there, but that
might be too much overhead.

Hope this helps.


--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #8
so just to clarify...
Project C contains Interface definition [no refs, it just gets referenced].
Project A needs to instantiate types that implement said interface [refs
C for the interface and B for the implementations OR just B]
Project B has types that actually implement the said interface [refs
project C for the definition].

Would ProjA need to reference both B and C, or is a reference to B good
enough?


Nicholas Paldino [.NET/C# MVP] wrote:
Shaun,

You don't NEED it, as project B can always reference project A to get
the interface.

While it won't apply in this case, if you have a situation where project
A needs to reference project B directly, you won't be able to do it, because
it would create a circular reference between assemblies, which is
disallowed. Placing the type definitions shared between the two assemblies
in another assembly will elimate this.


--
Shaun Farrugia o DTE Energy Trading
Sr Programmer/Analyst
desk o 734-887-2148 oo email o fa*******@dteenergy.com

Nov 13 '05 #9

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
1
by: Sea Sharper | last post by:
Hi, C#, from a FileSystemWatcher I would like to catch all files with a *.* filter but then inside the event handler compare against a list of wildcards (eg '*.abc;*.def') Is there anywhere...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
1
by: ltruett | last post by:
Last week I continued my series of design patterns examples using PHP 5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern. Here now is my 20th PHP 5 design pattern example, the...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.