.NETGURU
Code decides what objects to use
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-patterns' list.


Melvin Ram
-- Moved from [aspngvs] to [ngfx-patterns] by Charles M. Carroll <Click here to reveal e-mail address> --

I am developing an application in C#. A feature I want to insert is to have
the code choose what objects to use.

The following scenario should make the question more clearer:
Lets say I was developing a kiosk application for software retailers that
would allow instore shoppers to browse and find products faster. We will
assume this application will be a boxed package. Since its a prepackaged
program, it has to be set up with no customization to the program itself.
There will be only one release. (I understand that with the new model of
distributed programming, this wouldn't be appropriate, but just to get the
point across, lets take everything as face value).

The point is, the software can't be customized by me every time a new client
makes a purchase.

The retail industry is diverse in data storage models. This posses some
problems. One firm might be using a SQL Server connected to the retail chain
headquarters. Another firm might be using a proprietary program that just
tracks inventory.

Lets say the main function of my application is to act as a user friendly
kiosk. I don't want people to buy whole new systems when purchasing my
software. I want my software to adapt to whatever data source they have.

So when I ship the application, it would have "adapters" that would
translate what my application is requesting.

Lets assume we have a list of all the available adapters in an xml file.
This file can be updated using an admin panel if we introduce support for
more data sources.

Now comes the meat of the question: How do I get my main application to
communicate with these adapters?

Here are the key assumptions:
* The main application has a string variable with the name of the class as
the value.
* All the adapters support the same functions, i.e. getAll(), getByType(),
etc.
* All the adapters have a common namespace.
* The adapters themselves are complex business objects that perform stuff we
don't want to look at.

Any input on this problem would be appreciated. Please feel free to forward
this to anyone you thing who could help me. Thank a lot.

Melvin Ram

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

Reply to this message...
 
    
Louis-Philippe Alain
Hi,
There`s certainly many ways to do it but here is the first thing that
comes to my mind. I would use the 'Strategy' pattern with the 'Abstract
Factory' pattern (GoF). You say your 'adapters' all support the same
operations. Define an interface or abstract class with all the common
operations which will be the base for your 'strategies'. Then create a
concrete data access 'Strategy' class for each different data source you
want your application to support. Then, create an 'Abstract Factory'
class that has a 'IStrategy Create(strategyname:string)' operation that
takes the string variable with the name of the data access strategy you
want to use. The 'Abstract Factory' create the concrete 'Strategy'
class but return only the base interface to the client. This way, your
application is decoupled from any concrete data access strategy. The
point of coupling is to the base strategy interface.

Design Patterns : Element of Reusable OO Software
Gamma, Helm, Vlisside and Johnson.

Hope it helps!

Louis-Philippe Alain
Software Architect
9114-5656 Qu=E9bec Inc

-----Original Message-----
From: Melvin Ram [mailto:Click here to reveal e-mail address]=20
Sent: March 29, 2002 7:59 PM
To: ngfx-patterns
Subject: [ngfx-patterns] Code decides what objects to use

-- Moved from [aspngvs] to [ngfx-patterns] by Charles M. Carroll
<Click here to reveal e-mail address> --

I am developing an application in C#. A feature I want to insert is to
have
the code choose what objects to use.

The following scenario should make the question more clearer:
Lets say I was developing a kiosk application for software retailers
that
would allow instore shoppers to browse and find products faster. We will
assume this application will be a boxed package. Since its a prepackaged
program, it has to be set up with no customization to the program
itself.
There will be only one release. (I understand that with the new model of
distributed programming, this wouldn't be appropriate, but just to get
the
point across, lets take everything as face value).

The point is, the software can't be customized by me every time a new
client
makes a purchase.

The retail industry is diverse in data storage models. This posses some
problems. One firm might be using a SQL Server connected to the retail
chain
headquarters. Another firm might be using a proprietary program that
just
tracks inventory.

Lets say the main function of my application is to act as a user
friendly
kiosk. I don't want people to buy whole new systems when purchasing my
software. I want my software to adapt to whatever data source they have.

So when I ship the application, it would have "adapters" that would
translate what my application is requesting.

Lets assume we have a list of all the available adapters in an xml file.
This file can be updated using an admin panel if we introduce support
for
more data sources.

Now comes the meat of the question: How do I get my main application to
communicate with these adapters?

Here are the key assumptions:
* The main application has a string variable with the name of the class
as
the value.
* All the adapters support the same functions, i.e. getAll(),
getByType(),
etc.
* All the adapters have a common namespace.
* The adapters themselves are complex business objects that perform
stuff we
don't want to look at.

Any input on this problem would be appreciated. Please feel free to
forward
this to anyone you thing who could help me. Thank a lot.

Melvin Ram

_________________________________________________________________
Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp.

| [ngfx-patterns] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-patterns.asp =3D JOIN/QUIT

Reply to this message...
 
    
BlueRace

Here is the conversation I had with Louis-Philippe, just to keep
everyone updated on the issue:
********************************************************************
********************************************************************
Melvin says:
Can I ask you a little more on your reply or are you busy right now?

Louis-Philippe says:
I hope I helped you a little... can I do something else for you ?

Melvin says:
I was just writing out an email to you, i'll just paste it here

Louis-Philippe says:
as you wish

Melvin says:
The main are that I am getting stuck at is how to create the object
dynamically. What I am looking for is the usable version of the
following code.

Melvin says:
CreateObject("ObjectType", "ObjectName");

Louis-Philippe says:
That's what the Abstract Factory is for

Melvin says:
I didn't understand that. What is Abstract Factory or which namspace can
i find it in

Louis-Philippe says:
this is something you have to built... give 2 sec I'll give you more
info
Louis-Philippe says:
I juste sent you an email with an Abstract Factory class with a little
explanation... ask me questions if it's still not clear

Melvin says:
thanx, i'm gonna go look at it now

Louis-Philippe says:
Basically, your application will know the Abstract Factory and will call
it's create method with a parameter indicating which concrete data
access strategy to use, the create operation will create the concrete
strategy and return it to your application... the trick is that all your
data access strategies that can be created by your factory inherits from
the same base Strategy...

Louis-Philippe says:
so your application doesn't know which concrete strategy it is using

Louis-Philippe says:
Once you understand, it's actually quite simple...

Melvin says:
I hope so, i'm reading right now

Melvin says:
hey Louis, this would mean that I would have to update the Abstract
Factory everytime a new object is made available to my application,
right?

Louis-Philippe says:
yes that's right... but it's the only place you have to modify... I will
point you in other direction that may also help you...

Melvin says:
okay, that would be great

Louis-Philippe says:
Activator.CreateInstance(ClassName.GetType());

Louis-Philippe says:
this is a way to dynamically create objects in .Net

Louis-Philippe says:
the Activator class uses reflection

Melvin says:
i have been looking at that

Louis-Philippe says:
and it's still not what you need?

Louis-Philippe says:
maybe I'm misunderstanding what you're trying to accomplish

Melvin says:
hold on just a sec

Louis-Philippe says:
If you don't want to have to modify the Abstract Factory each time, you
could use the 'Activator.CreateInstance()' method to create your
concrete strategy... but you have to be sure that it exists otherwise
you'll have an exception

Melvin says:
I think it is what i'm looking for. i'm going to try it out and test.

Melvin says:
thanx for your help

Louis-Philippe says:
no prob.

Melvin says:
i'm going to post what we talked about here to the asp list, is that
okay

Louis-Philippe says:
yeah sure!

-----Original Message-----
From: Hadi Hassan [mailto:Click here to reveal e-mail address]
Sent: Saturday, March 30, 2002 1:57 AM
To: aspngclient
Subject: [aspngclient] RE: [aspngarchitecture] RE: Code decides what
objects to use

-- Moved from [aspngarchitecture] to [aspngclient] by Chaz
<Click here to reveal e-mail address> --

I am new to Asp , I m facing some problems in ASP

I want to keep track of the back button of browser, so that whenever a =
user
tries to go back to start(login) page(by using back button) the user =
should
redirect to the other page on which user have to re-enter the password =
or
change the user.
I have written a script which is checking the session variables and
redirecting the user accordingly but when I press the back button this
script not fired and login page appears again.
So anybody wanna help me????
Thanks in advance

    -----Original Message-----
    From:    Merak [SMTP:Click here to reveal e-mail address]
    Sent:    30 =E5=C7=D1=D3, 2002 12:11 =E5
    To:    aspngarchitecture
    Subject:    [aspngarchitecture] RE: Code decides what
objects to
use

    It looks like the ideal scenario for either an interface-based
approach
    or for inheriting from a "BaseAdapter" class=20

    In the interface appraoch you define an interface that adapters
need
to
    implement
    Your code then calls methods on this interface

    Alternatively you could use a base class (BaseAdapter) that your
    adapters inherit from
    The base class defines all the methods your code calls, some if
not
all
    will be overriden by the adapter subclasses

    Which you would choose would depend on your design, and I'm not
sure
how
    to direct you from there (I'm from a VB background, so I've only
had
the
    one choice up til now <g> )

    Merak=20

    > I am developing an application in C#. A feature I want to=20
    > insert is to have=20
    > the code choose what objects to use.
    <snip>

    > Now comes the meat of the question: How do I get my main=20
    > application to=20
    > communicate with these adapters?
    >=20
    > Here are the key assumptions:
    > * The main application has a string variable with the name
of=20
    > the class as=20
    > the value.
    > * All the adapters support the same functions, i.e.
getAll(),=20
    > getByType(),=20
    > etc.
    > * All the adapters have a common namespace.
    > * The adapters themselves are complex business objects that=20
    > perform stuff we=20
    > don't want to look at.
    >=20
    > Any input on this problem would be appreciated. Please feel=20
    > free to forward=20
    > this to anyone you thing who could help me. Thank a lot.

Reply to this message...
 
    
BlueRace
Here is another conversation I had regarding the "code decides what
objects to use" topic. If anyone has a better approach, please let us
know. I think this approach will work just great, but I haven't tested
yet. I'll keep you guys posted on what the results were.

===================
Melvin says:
after you have created an instance, how do you access the object
Melvin says:
i was trying this:
Melvin says:
ObjectHandle hdlSample;
object myObject;

hdlSample = Activator.CreateInstance("dealerfront",
    "dealerfront.module1");

myObject = hdlSample.Unwrap();
return myObject.GiveMe();
Melvin says:
but obviously it didn't work
Melvin says:
i tryed using the code they provided but they didn't give all the
namespaces they used
Melvin says:
This is using their code:
Louis-Philippe says:
I'm not sure I understand what you want to do... You create an instance
of an object with CreateInstance and want to return that object ?
Melvin says:
ObjectHandle hdlSample;
IMyExtenderInterface myExtenderInterface;

// Activates an object for this client.
hdlSample = Activator.CreateInstance("dealerfront",
    "dealerfront.module1",
    true,
    BindingFlags.Instance|BindingFlags.Public,
    null,
    null,
    null,
    null,
    null);
myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap();
return myExtenderInterface.GiveMe();
Melvin says:
No, i want to use functions in that object.
Louis-Philippe says:
in the object you created with CreateInstance ?
Melvin says:
yea
Louis-Philippe says:
the code example you have do more than this, it's why you have
troubles...
Louis-Philippe says:
all you need is :
Louis-Philippe says:
YouObjectClass myObject = Activator.CreateInstance(... your needed
parameters ...);
myObject.AMethod();
Louis-Philippe says:
CreateInstance create an instance of a class and return it... so the
object you get from the method is already the object you want to use
Louis-Philippe says:
YouObjectClass myObject =
Activator.CreateInstance(Type.GetType("YourObjectClass"));
myObject.AMethod();
Melvin says:
maybe i'm understanding correctly by that defeats the purpose of the
Activator.CreateInstance method doesn't it.
Melvin says:
i mean the purpose is to not know what class you are going instanciate
Melvin says:
so i need like a temporary object
Louis-Philippe says:
"YourObjectClass" can be any string variable that could come from an
external XML file for example
Melvin says:
so is YouObjectClass a valid object?
Louis-Philippe says:
what do you mean by a valid object
Louis-Philippe says:
?
Melvin says:

YouObjectClass myObject = Activator...
/\
||
This is were i'm getting alittle confused. where did this YouObjectClass
come from? what namespace
Louis-Philippe says:
YourObjectClass myObject = new YourObjectClass(); // This is the usual
way of creating an instance of a class... The instance of a class is an
object
Louis-Philippe says:
ah
Louis-Philippe says:
ok
Louis-Philippe says:
so in your case, what you want to do is define a base class that all the
objects you create with the CreateInstance method will derive from...
Melvin says:
ohh, that makes sense
Louis-Philippe says:
So let's say you have a base class 'MyBaseClass' and another class
'MyInheritedClass' you can do :

MyBaseClass myObject =
Activator.CreateInstance(Type.GetType("MyInheritedClass"));
myObject.ABaseClassMethod();
((MyInheritedClass)myObject).AnInheritedClassMethod();
Louis-Philippe says:
// the third line shouldn't be used unless you really need to know your
concrete class... otherwise, always talk to your object from the base
type in order to not couple your code to the sub class.
Louis-Philippe says:
i'm sorry to switch terms every lines.. i'm a bit tired I guess...
Melvin says:
It all makes sense now, great, i think this will do the trick.
Louis-Philippe says:
I'm happy if it helped you! CC me on the reply to the asp list, I
might follow it up on the Patterns List which is where I catched and
also replied to your initial post

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

Reply to this message...
 
    
Philip Nelson

--- Louis-Philippe Alain <Click here to reveal e-mail address> wrote:
[Original message clipped]

I wouldn't start with Abstract Factory. A simple Factory would suffice. And
since the calling class would only know about the shared interface, use of
strategy or any other pattern to build the adapter would be a choice to be made
when you code it.

__________________________________________________
Do You Yahoo!?
Yahoo! Greetings - send holiday greetings for Easter, Passover
http://greetings.yahoo.com/

Reply to this message...
 
 
System.Activator
System.Reflection.BindingFlags
System.Runtime.Remoting.ObjectHandle
System.Type




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification