473,387 Members | 1,515 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,387 software developers and data experts.

Adding new classes without recompiling old code ?

Hi,

Have a requirement where new items may be added later to an existing
list of items. These new items may have new features but will need to
contain minimum basic features like description and price. These items
should be added to the existing code without the code being needed to
be recompiled or making minimum changes to the existing code i.e. it
should be OCP compliant. Not sure how to go about this . The only
things which come to my mind are that each new item class should derive
from an IItem interface and that each new class could be placed in a
seperate assembly. Is there some way to use reflection or any other
feature to accomplish this ?

thanks in advance.

May 10 '06 #1
9 1778
You could use reflection, but it's not the right way to go on this.

You should do what you said, create an interface, or a base class which
you derive from which exposes the properties common to all of the classes.
Then, have your list work with that base class/interface.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<fr***********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi,

Have a requirement where new items may be added later to an existing
list of items. These new items may have new features but will need to
contain minimum basic features like description and price. These items
should be added to the existing code without the code being needed to
be recompiled or making minimum changes to the existing code i.e. it
should be OCP compliant. Not sure how to go about this . The only
things which come to my mind are that each new item class should derive
from an IItem interface and that each new class could be placed in a
seperate assembly. Is there some way to use reflection or any other
feature to accomplish this ?

thanks in advance.

May 10 '06 #2

Nicholas Paldino [.NET/C# MVP] wrote:
You could use reflection, but it's not the right way to go on this.

You should do what you said, create an interface, or a base class which
you derive from which exposes the properties common to all of the classes.
Then, have your list work with that base class/interface.

Hope this helps.


Yes .. though wondering how to actually go about compiling the new
class without recompilng the existing code and how to add an object
from this class to the list of items.

thanks.

May 10 '06 #3
What is the big deal about recompiling the existing code?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<fr***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

Nicholas Paldino [.NET/C# MVP] wrote:
You could use reflection, but it's not the right way to go on this.

You should do what you said, create an interface, or a base class
which
you derive from which exposes the properties common to all of the
classes.
Then, have your list work with that base class/interface.

Hope this helps.


Yes .. though wondering how to actually go about compiling the new
class without recompilng the existing code and how to add an object
from this class to the list of items.

thanks.

May 10 '06 #4

Nicholas Paldino [.NET/C# MVP] wrote:
What is the big deal about recompiling the existing code?


Ok.. trying to keep minimal changes to the existing code .. so maybe
recompiling is okay .. but still keeping modifications to existing code
minimal ..

thanks.

May 10 '06 #5
Basically you would go through and create a shared .dll with your abstract
contract (consider it a "public" dll) ..

as an example .. in my public dll I have an interface IFoo
public interface IFoo {
public void Bar();
}

now in my code I reference the public interface ...

When you want to create your own implementation .. you create your own .dll
.... referencing the public dll and write your own class implementing IFoo.

public class YourFoo : IFoo {
public void Bar() {
Console.WriteLine("YourFoo::Bar()");
}
}

You compile your .dll and place it in my bin folder.

My application simply needs some way of dynamically loading items into its
space, a very common method of doing this is with the factory pattern. Here
is a naive implementation ..

public class FooFactory {
public static IFoo GetFoo(string _AssemblyName, string _TypeName) {
Assembly a = Assembly.LoadFrom(_AssemblyName) ;
return a.CreateInstance(_TypeName) as IFoo;
}
}

So now, we can given an assembly name and a type name dynamically load items
into our process ... now its just a matter of maintaining those. Another
alternative is to use attributes to allow us to discover the types in the
assembly, avoids having to define th assemblyname/typename as we can instead
assume that anything with a FooPlugin attribute is something that we are
interested in.

Hope this helps a bit,

Greg Young
MVP - C#
<fr***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

Nicholas Paldino [.NET/C# MVP] wrote:
You could use reflection, but it's not the right way to go on this.

You should do what you said, create an interface, or a base class
which
you derive from which exposes the properties common to all of the
classes.
Then, have your list work with that base class/interface.

Hope this helps.


Yes .. though wondering how to actually go about compiling the new
class without recompilng the existing code and how to add an object
from this class to the list of items.

thanks.

May 10 '06 #6

I have several examples of the "Factory Design Pattern" at my blog:

http://spaces.msn.com/sholliday/ 12/1/2005 entry

The one you're interested in is the reflection method.

Take GREAT care in designing your Interface or Abstract base class.... as
this is the key to keeping the recompile to a minimum.
Please leave a comment if you find it useful.


<fr***********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi,

Have a requirement where new items may be added later to an existing
list of items. These new items may have new features but will need to
contain minimum basic features like description and price. These items
should be added to the existing code without the code being needed to
be recompiled or making minimum changes to the existing code i.e. it
should be OCP compliant. Not sure how to go about this . The only
things which come to my mind are that each new item class should derive
from an IItem interface and that each new class could be placed in a
seperate assembly. Is there some way to use reflection or any other
feature to accomplish this ?

thanks in advance.

May 10 '06 #7

Greg Young wrote:
Basically you would go through and create a shared .dll with your abstract
contract (consider it a "public" dll) ..

as an example .. in my public dll I have an interface IFoo
public interface IFoo {
public void Bar();
}

now in my code I reference the public interface ...

When you want to create your own implementation .. you create your own .dll
... referencing the public dll and write your own class implementing IFoo.

public class YourFoo : IFoo {
public void Bar() {
Console.WriteLine("YourFoo::Bar()");
}
}

You compile your .dll and place it in my bin folder.

My application simply needs some way of dynamically loading items into its
space, a very common method of doing this is with the factory pattern. Here
is a naive implementation ..

public class FooFactory {
public static IFoo GetFoo(string _AssemblyName, string _TypeName) {
Assembly a = Assembly.LoadFrom(_AssemblyName) ;
return a.CreateInstance(_TypeName) as IFoo;
}
}

So now, we can given an assembly name and a type name dynamically load items
into our process ... now its just a matter of maintaining those. Another
alternative is to use attributes to allow us to discover the types in the
assembly, avoids having to define th assemblyname/typename as we can instead
assume that anything with a FooPlugin attribute is something that we are
interested in.

Hope this helps a bit,


Yes it does, in a big way. Just one more question , is there a way to
discover all the assembly names in the bin folder .

thanks.

May 10 '06 #8
Yes .. Directory.GetFiles will give you all of the file names in the
directory.

Also if you poke around you can find numerous things doing just this as
plugin libraries. I'm sure a google on "C# plugin library" will bring up a
good deal of them.

Cheers,

Greg Young
MVP - C#
<fr***********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...

Greg Young wrote:
Basically you would go through and create a shared .dll with your
abstract
contract (consider it a "public" dll) ..

as an example .. in my public dll I have an interface IFoo
public interface IFoo {
public void Bar();
}

now in my code I reference the public interface ...

When you want to create your own implementation .. you create your own
.dll
... referencing the public dll and write your own class implementing
IFoo.

public class YourFoo : IFoo {
public void Bar() {
Console.WriteLine("YourFoo::Bar()");
}
}

You compile your .dll and place it in my bin folder.

My application simply needs some way of dynamically loading items into
its
space, a very common method of doing this is with the factory pattern.
Here
is a naive implementation ..

public class FooFactory {
public static IFoo GetFoo(string _AssemblyName, string _TypeName) {
Assembly a = Assembly.LoadFrom(_AssemblyName) ;
return a.CreateInstance(_TypeName) as IFoo;
}
}

So now, we can given an assembly name and a type name dynamically load
items
into our process ... now its just a matter of maintaining those. Another
alternative is to use attributes to allow us to discover the types in the
assembly, avoids having to define th assemblyname/typename as we can
instead
assume that anything with a FooPlugin attribute is something that we are
interested in.

Hope this helps a bit,


Yes it does, in a big way. Just one more question , is there a way to
discover all the assembly names in the bin folder .

thanks.

May 10 '06 #9
Hey greg ,

thanks a ton :)

Greg Young wrote:
Yes .. Directory.GetFiles will give you all of the file names in the
directory.

Also if you poke around you can find numerous things doing just this as
plugin libraries. I'm sure a google on "C# plugin library" will bring up a
good deal of them.

Cheers,

Greg Young
MVP - C#
<fr***********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...

Greg Young wrote:
Basically you would go through and create a shared .dll with your
abstract
contract (consider it a "public" dll) ..

as an example .. in my public dll I have an interface IFoo
public interface IFoo {
public void Bar();
}

now in my code I reference the public interface ...

When you want to create your own implementation .. you create your own
.dll
... referencing the public dll and write your own class implementing
IFoo.

public class YourFoo : IFoo {
public void Bar() {
Console.WriteLine("YourFoo::Bar()");
}
}

You compile your .dll and place it in my bin folder.

My application simply needs some way of dynamically loading items into
its
space, a very common method of doing this is with the factory pattern.
Here
is a naive implementation ..

public class FooFactory {
public static IFoo GetFoo(string _AssemblyName, string _TypeName) {
Assembly a = Assembly.LoadFrom(_AssemblyName) ;
return a.CreateInstance(_TypeName) as IFoo;
}
}

So now, we can given an assembly name and a type name dynamically load
items
into our process ... now its just a matter of maintaining those. Another
alternative is to use attributes to allow us to discover the types in the
assembly, avoids having to define th assemblyname/typename as we can
instead
assume that anything with a FooPlugin attribute is something that we are
interested in.

Hope this helps a bit,


Yes it does, in a big way. Just one more question , is there a way to
discover all the assembly names in the bin folder .

thanks.


May 10 '06 #10

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

Similar topics

5
by: Sue | last post by:
On code-behind page: (attributes set programatically for each of these elements) linkbutton added to tablecell textbox added to tablecell tablecells added to tablerow tablerow added to table...
5
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I...
6
by: Sezai Altınok | last post by:
is it possible to build a project without recompiling the forms like in FoxPro? i mean in foxpro when you make some changes even in the code part of a form you can see the effect without a...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Christopher Kimbell | last post by:
In ASP.NET, is it possible to add new pages without having to recompile the whole website? Can the site span several dlls? Chris -- Using Opera's revolutionary e-mail client:...
12
by: Wardeaux | last post by:
All, Wanting to find a way to create web pages to add to my website without having to recompile the codebehind everytime I want to add a new one... Here's the deal: I have a web app that takes...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.