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

Dynamicaly implement interfaces

Hi,

I've seen that there are implementations of Mock objects, which
implement interfaces on-the-fly during execution. Could somebody point
me to the direction on how to do that in .Net?

I would to use that to wrap an object and to store the calls to the
object into a file. Later I want to simulate the object by using this
file as "playback data".

Any hints?

regards,
Achim
Nov 17 '05 #1
12 1976
"Achim Domma (Procoders)" <do***@procoders.net> wrote in message
news:dd*************@news.t-online.com...
Hi,

I've seen that there are implementations of Mock objects, which
implement interfaces on-the-fly during execution. Could somebody point
me to the direction on how to do that in .Net?


Two possibilities spring immediately to mind, there are almost certainly
others.

You could you the Reflection Emit namespace to dynamically interrogate a
class, then emit a proxy/mock on the fly.

Secondly, you could derive your object from ContextBoundObject, intercept
the calls to your instances, then record them to the file. This is
essentially an exercise in Aspect Oriented Programming. Off the top of my
head, I'm guessing that your call intercepts could also read from the file,
then forge a return or change of state. It's worth noting that any class
derived from ContextBoundObject will have poor performance.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
Achim,

Not sure why you want to "wrap an object". If you want to store some
object state and "play it back", then serialize/deserialize the object.

Best Regards
Johann Blake

Nov 17 '05 #3
Tim Haughton wrote:
Secondly, you could derive your object from ContextBoundObject, intercept
the calls to your instances, then record them to the file. This is
essentially an exercise in Aspect Oriented Programming. Off the top of my
head, I'm guessing that your call intercepts could also read from the file,
then forge a return or change of state. It's worth noting that any class
derived from ContextBoundObject will have poor performance.


Thanks for your response! Performance should be not problem in that
case, but documentation for ContextBoundObject seems to be a problem.
The reference in MSDN tells me not too much about how to use it. Do you
know a tutorial on the web or a book which is available via Safari?

regards,
Achim
Nov 17 '05 #4
"Achim Domma (SyynX Solutions GmbH)" <ac*********@syynx.de> wrote in message
news:dd************@news.t-online.com...
Thanks for your response! Performance should be not problem in that
case, but documentation for ContextBoundObject seems to be a problem.
The reference in MSDN tells me not too much about how to use it. Do you
know a tutorial on the web or a book which is available via Safari?


You could try this article...

http://www.codeproject.com/csharp/AO...stScenario.asp

After that, trawl this...

http://www.google.co.uk/search?hl=en...ndobject&meta=

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #5
Tim Haughton wrote:
Two possibilities spring immediately to mind, there are almost certainly
others.

You could you the Reflection Emit namespace to dynamically interrogate a
class, then emit a proxy/mock on the fly.


I would like to have a look at this option too. Do you have some
starting points for me?

And one basic question: Do I have to emit the code as intermediate
language? Or is there a posibility to compile C# on the fly?

regards,
Achim
Nov 17 '05 #6
Johann Blake wrote:
Not sure why you want to "wrap an object". If you want to store some
object state and "play it back", then serialize/deserialize the object.


I don't wont to store the state only. I have the following in mind:

I have an interface like

interface ISearch
{
void set_option(string key, string val);
void search(string query, string param1, string param2);
}

and and real object which implements this interace. If I develop code
which uses this interface and processes the results, it's hard do write
tests. The data which the search is based on changes often. The wrapper
would also implement ISearch but would pass all calls to the real
object. It would also store the calling parameters and the results. In a
second run, I would not call the real object, but return the saved
results. So my tests would always get the same results.

Hope my explanation is somewhat understandable. I could of course create
such an object by hand, but I though this could be solved in a reusable
manner.

regards,
Achim
Nov 17 '05 #7
"Achim Domma (SyynX Solutions GmbH)" <ac*********@syynx.de> wrote in message
news:dd*************@news.t-online.com...
You could you the Reflection Emit namespace to dynamically interrogate a
class, then emit a proxy/mock on the fly.
I would like to have a look at this option too. Do you have some
starting points for me?


Start with "Hello, world!", where else?

http://blogs.msdn.com/joelpob/archiv.../21/61411.aspx

And one basic question: Do I have to emit the code as intermediate
language? Or is there a posibility to compile C# on the fly?


When you 'Emit', you emit IL, but you could just as easily drop C# source,
compile it, then load it on the fly. That's kind of like a poor man's emit.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #8
Achim

It still isn't clear what you are trying to accomplish. Without
discussing coding things (interfaces, methods, etc), can you simply
explain what you want to accomplish in a non-technical way? Perhaps I
can then understand what you are getting at.

Thanks
Johann Blake

Nov 17 '05 #9
Johann Blake wrote:
It still isn't clear what you are trying to accomplish. Without
discussing coding things (interfaces, methods, etc), can you simply
explain what you want to accomplish in a non-technical way? Perhaps I
can then understand what you are getting at.


I have an object representing an search engine. I want to call this
engine multiple times with different parameters and want to record the
incoming parameters and the outgoing results.

After that I want to replace the search object with a wrapper, which
behaves like the original search object, but simply accepts and returns
the data from the recorded session.

I want this for testing purposes to have a of 'frozen' search engine,
returning always the same results without having the real engine.

regards,
Achim
Nov 17 '05 #10
When you say that you want to replace the search engine object with a
wrapper, do you mean that you are actually replacing the real search
engine with a sort of "offline" version of that component, or is the
wrapper still using the same search engine object?

Even so, there are some unclear issues here:

* is it important that the "wrapped" object executes the same code as
when the data was originally retrieved? or,
* is the data simply stored in a file or database and retrieved based
on some criteria?

How about adding a parameter to your search engine "Search" method that
indicates whether to return live or stored data? I guess I have a
problem understanding why you feel you need a wrapper.

Best Regards
Johann Blake

Nov 17 '05 #11
Johann Blake wrote:
When you say that you want to replace the search engine object with a
wrapper, do you mean that you are actually replacing the real search
engine with a sort of "offline" version of that component, or is the
wrapper still using the same search engine object?
In the first run, the wrapper would call the real engine, but only to
create test data. After this initialization step, the data would be
stored on disc and the wrapper should work without an installed search
engine.

My target is to test code which depends on the search engine without
having a search engine! ;-)
* is it important that the "wrapped" object executes the same code as
when the data was originally retrieved? or,
No the wrapper should simply return a fixed result for a given set of
input parameters.
* is the data simply stored in a file or database and retrieved based
on some criteria?
Yes, the data is stored in a file an the wrapper should not depend on
the search engine.
How about adding a parameter to your search engine "Search" method that
indicates whether to return live or stored data?
Changing the search engine is not an option.
I guess I have a
problem understanding why you feel you need a wrapper.


I'm open to alternatives!

regards,
Achim
Nov 17 '05 #12
I assume that you want to use various types of search engine objects and
that you cannot modify these objects, in which case using a wrapper
would make sense.

I would then create an interface and implement this interface in a
class. This interface would implement your Search method. You can then
call it with a parameter that tells your Search method to use either
live or offline data. This class in turn would create a specific
instance of your search engine object and call it when the parameter
indicates live data. For offline data, your class simply retrieves it
from your file/database. Any problem with that?
Best Regards
Johann Blake

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #13

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

Similar topics

4
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
14
by: Jim | last post by:
I am using VB.Net 2.0 and I am completely new to the concept of implementing interfaces. Can anyone explain "implementing interfaces" to me and perhaps give me an example of implementing an...
2
by: Lucian Wischik | last post by:
Does ReadOnlyCollection<T> really implement IList<T>, like it claims to do? ... When I right-click on ReadOnlyCollection and look at its definition, it says this: public class...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
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: 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: 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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.