I have two processes (A and B), both have a method Foo(SomeClass paramA, SomeOtherClass paramB).
The processes communicate using Windows Pipes (not WCF) and can send and receive messages of type:
public class PipeMessageArgs
{
public PipeMessageArgs(string i_MethodName, List<object> i_Args)
{
MethodName = i_MethodName;
Args = i_Args;
}
public string MethodName { get; private set; }
public List<object> Args { get; private set; }
}
When calling Foo on A, I want to invoke Foo on B, with the same values.
This is the calling code in A:
public void Foo(SomeClass paramA, SomeOtherClass paramB)
{
var args = new List<object> { paramA, paramB };
m_Server.PushMessage(new PipeMessageArgs(MethodBase.GetCurrentMethod().Name, args));
}
This is the invoking code in B:
void ClientOnReceiveMessage(NamedPipeConnection i_Connection, object i_Message)
{
var pipeMessageArgs = i_Message as PipeMessageArgs;
GetType().GetMethod(pipeMessageArgs.MethodName).Invoke(this, pipeMessageArgs.Args.ToArray());
}
But as you can see, I have to manually create a list of parameters for each call so if I forget a parameter or get the order wrong, things will not work. Given that I cannot use reflection to get the values, and I do not want to use the profiler (performance is an issue), what is the best way to make it more generic?
Edit: I cannot use WCF for too many reasons (actually, I am moving away from WCF). I am using pipes, namely PipeStream.
Edit2: What I want is a solution that does not rely on manually creating arrays of parameters; something that can create this array automatically for me.
I ended up using RealProxy. RealProxy is mainly used for remoting but can allow you to create proxy for classes. You can then add functionality before each method call (also properties calls). I used this very nice blog post to implement it.
This is my proxy:
public class InvokingProxy : RealProxy
{
private readonly INamedPipe _namedPipe;
InvokingProxy(object i_Target, INamedPipe i_NamedPipe) : base(i_Target.GetType())
{
_namedPipe = i_NamedPipe;
}
public override IMessage Invoke(IMessage i_Msg)
{
var methodCall = i_Msg as IMethodCallMessage;
if (methodCall != null)
{
return HandleMethodCall(methodCall);
}
return null;
}
IMessage HandleMethodCall(IMethodCallMessage i_MethodCall)
{
_namedPipe.PushMessage(new PipeMessageArgs(i_MethodCall.MethodName, i_MethodCall.InArgs));
return new ReturnMessage(null, null, 0, i_MethodCall.LogicalCallContext, i_MethodCall);
}
public static T Wrap<T>(T i_Target, INamedPipe i_NamedPipe) where T : MarshalByRefObject
{
return (T)new InvokingProxy(i_Target, i_NamedPipe).GetTransparentProxy();
}
}
I use i_MethodCall.InArgs to get the in-arguments, that's the hear of the issue. Also notice how HandleMethodCall uses the pipe to push a message instead of actually calling the requested method. This means that my 'API' class is actually just empty methods with no implementation (I can always add more implementation there and invoke the method before/after the pipe action):
class Api : MarshalByRefObject, IApi
{
public void Foo(SomeClass paramA, SomeOtherClass paramB)
{
}
public void Bar(SomeClassX paramA, SomeOtherClassY paramB)
{
}
}
}
Also, as a requirement of RealProxy. the class has to inherit from MarshalByRefObject which was fine by me as it has no other functionality. Read more about it in the blog post I linked.
Related
In my code i need to allow only class EchoProcess call method GetStorePrivateData from class MainData.I thnk it not better way but in this code it work done.How should i do?
** In future class MainData will have more protected class and allow specific other class to call it.
internal sealed class EchoProcess : MainData
{
private EchoProcess()
{
}
public static string EchoPrivate(string someCondition)
{
var result = GetStorePrivateData<EchoProcess, Key>();
//Condition here >
return "";
}
private class Key
{
}
}
internal class MainData
{
protected MainData()
{
}
private static readonly List<string> StorePrivateData = new List<string>();
protected static List<string> GetStorePrivateData<TEcho, TKey>() where TEcho : class where TKey : class
{
return CheckAllowGetStorePrivateDataClassAccess<TEcho, TKey>() ? StorePrivateData : null;
}
private static bool CheckAllowGetStorePrivateDataClassAccess<TEcho, TKey>()
{
var thisClass = MethodBase.GetCurrentMethod().DeclaringType;
var echoProcessType = typeof(TEcho);
var isEchoProcess = echoProcessType.Name == "EchoProcess";
var keyType = typeof(TKey);
var isKey = keyType.Name == "Key";
var isEqualNameSpace = thisClass.Namespace.Equals(echoProcessType.Namespace) &&
keyType.Namespace.Equals(echoProcessType.Namespace);
var keyTypeFullName = $"{echoProcessType.FullName}+{keyType.Name}";
var isEqulaKeyTypeNameSpace = keyType.FullName.Equals(keyTypeFullName);
return isEqualNameSpace && isEqulaKeyTypeNameSpace && isEchoProcess && isKey;
}
}
In my code I need to allow only class EchoProcess call method GetStorePrivateData from class MainData.
Do not do any of this stuff with reflection or stack traces.
Both types are internal. They're in the same assembly. Mark the method you wish to restrict callers on as internal. Now any code in your assembly can call that method. Who cares? You wrote that code; if you don't like it, you can change it.
This is a problem that should be solved by communicating with the coders who are writing the assembly about what the correct protocols are for using assembly implementation details. This is an interpersonal team relationships problem, so don't try to solve it by writing code. Mark the method as internal, and if there's a call site that you don't like, then talk to the developer who wrote it during code review to find out why they thought that was a good idea.
In future class MainData will have more protected class and allow specific other class to call it.
Glad to hear it... the solution I provide below isn't ideal (e.g. you won't get any errors at compile time), and there are object models that are probably better suited for this.
In my code i need to allow only class EchoProcess call method GetStorePrivateData from class MainData.I thnk it not better way but in this code it work done.How should i do?
It is pretty easy to write code that inspects the call stack (you can obtain a copy with var s = new StackTrace() and walk the stack with s.GetFrames()). You can walk the call stack and see if EchoProcess was the caller or if the call came from somewhere else. Here is a simple example:
static public class CallPermissionHelper
{
static public bool IsAllowed<T>() where T : class
{
var callers = new StackTrace()
.GetFrames()
.Select
(
f => f.GetMethod().DeclaringType
);
var immediateCaller = callers.ElementAt(1);
var firstOutsideCaller = callers
.Skip(2)
.Where
(
t => t != immediateCaller
)
.FirstOrDefault();
return (firstOutsideCaller == typeof(T));
}
}
The logic is straightforward:
Obtain a list of callers, derived from the stack trace.
Ignore the first frame (which references the current code location, not very helpful)
Check the second frame to determine the class that called our helper class. In our case it will be MainData. We can ignore any stack frames from this class, e.g. if it is calling itself or using utility functions, since obviously it has permission to call itself.
Check the rest of the frames for any other classes, working down the stack. If the first class we run into is EchoProcess, we return true. In all other cases we return false.
Here is how to use it in your example:
internal sealed class EchoProcess : MainData
{
private class Key {}
public static string GetPrivateFromMainData()
{
return MainData.GetStorePrivateData<EchoProcess, Key>();
}
}
internal class MainData
{
public static string GetStorePrivateData<TEcho, TKey>() where TEcho : class where TKey : class
{
var allowed = CallPermissionHelper.IsAllowed<EchoProcess>(); //Magic!!!
return allowed ? "Allowed" : "Blocked";
}
}
public class Program
{
public static void Main()
{
var a = EchoProcess.GetPrivateFromMainData();
var b = MainData.GetStorePrivateData<object, object>();
Console.WriteLine("a={0}", a);
Console.WriteLine("b={0}", b);
}
}
Output:
a=Allowed
b=Blocked
See my code on DotNetFiddle
I am a novice/intermediate C# developer trying to accomplish a task I would be comfortable doing in C.
Basically, I'm getting XML requests that I need to respond to with a corresponding response. The request/response pairs are well defined in a structure like:
"Method1Request" receives "Method1Response"
"Method2Request" receives "Method2Response"
...
So, I'm going to create functions in my class called "Method1", "Method2",... that take parameters "Method1Request", "Method2Request",... and return "Method1Response", "Method2Response",...
In code, the function declarations would look like this, and the accompanying code is varied:
Mtehod1Response Method1(Method1Request);
Method2Response Method2(Method2Request);
...
All of the function responses will immediately be serialized into XML, so, theoretically, the functions could all be like this, and could write the result to the private string variable "response":
void Method1(Method1Request)
void Method2(Method2Request)
...
Anyway, what I would like to do is "sample" the XML (already have this working), to find out what type of request it is and use a case statement to, essentially, run the appropriate function on the request and , therefore, generate the correct response.
So, I need to create a function that does all of the steps that remain the same around calling those specific functions. It would basically be structured like this (!PSEUDO CODE!):
void ProcessRequestResponse(Type RequestType, Type ResponseType, Method MethodToRun)
{
...create serializers for RequestType and ResponseType...
...deserialize XML into RequestType...
[ResponseType] [MethodToRun]([RequestType]);
...serialize ResponseType into XML...
...store XML as variable in class...
}
I just have no idea how to actually create that function and have it know what function to run with which types. I looked into using the delegate "Func" methods, but I don't see a way to define them with types that are unknown at the time of creation.
Any direction on how to solve this would be appreciated. I don't think I need to type all of that code for each of my 15+ cases (hopefully!) and manage near identical instances of the almost same code.
I would go the Generics route, implementing some helper class and resolving the request-handling strategy using a configuration object (can be a simple Dictionary).
If you have 15+ request types, I would prefer having 15+ alternative classes, each implementing the corresponding handling strategy.
WARNING: just some throwaway example code, but should give you an head start.
// Define a weakly typed interface, to be called
// by the invoking code.
public interface IRequestHandler
{
public string HandleRequest(string xmlRequest);
}
// Defines a generic handler, accepting two type parameters, one
// for the request, one for the response.
public abstract class RequestHandler<RequestType, ResponseType> : IRequestHandler
{
public XmlSerializer GetRequestSerializer()
{
return GetSerializer(typeof(RequestType));
}
public XmlSerializer GetResponseSerializer()
{
return GetSerializer(typeof(ResponseType));
// an alternative, depending upon your deserialization library,
// could be:
// return GetSerializer<ResponseType>();
}
public XmlSerializer GetSerializer(Type dataType)
{
... resolve based on type.
}
public string HandleRequest(string xmlRequest)
{
if (request == null) throw new ArgumentNullException("request");
var requestSerializer = GetRequestSerializer();
var typedRequest = requestSerializer.Deserialize(xmlRequest) as RequestType;
// response is a ResponseType
var response = ProcessRequest(typedRequest);
var responseSerializer = GetResponseSerializer();
return responseSerializer.Serialize(response);
}
protected abstract ResponseType ProcessRequest(RequestType request);
}
// One handler implementation
// you can just declare the class and RequestHandler inheritance,
// and then right click and ask Visual Studio to "Implement abstract members"
public class ActualRequestHandler : RequestHandler<ActualRequest, ActualResponse>
{
protected ActualResponse ProcessRequest(ActualRequest request)
{
// ... do your processing
}
}
// One handler implementation
public class AnotherRequestHandler : RequestHandler<AnotherRequest, AnotherResponse>
{
protected AnotherResponse ProcessRequest(AnotherRequest request)
{
// ... do your processing
}
}
A simple configuration could be a static dictionary in your external processor class:
private static readonly Dictionary<Type, Func<IRequestHandler>> _strategyGetter = new Dictionary<Type, IRequestHandler>()
{
{typeof(ActualRequest), () => new ActualRequestHandler()},
{typeof(AnotherRequest), () => new AnotherRequestHandler()}
};
And finally, your global code:
Type requestType = ... your existing code processing the string request ...
Func<IRequestHandler> handlerFactoryMethod;
if(_strategyGetters.TryGetValue(requestType, out handlerFactoryMethod))
{
var handler = handlerFactoryMethod();
var responseString = handler(request);
// ... do what you want with responseString ...
}
else
{
// seems the request was not recognized.
}
I think Reflection is a way to solve your problem. Possible example:
Here are your different functions:
class Methodes
{
bool func1(bool a) { ...}
int func2(double b) { ... }
}
First you have to get all MethodInfo (all available methodes) via reflection:
MethodInfo[] AvailableMethodes = typeof(Methodes).GetMethods();
Now you can search the methode you want using a loop through your available methodes. Use the GetParameters() and ReturnType to check if the parameters an types are correct. If you have a match, call a the methode with the Invoke:
foreach (MethodInfo AvailableMethode in AvailableMethodes)
{
if (AvaliableMethode.ReturnType.ToString() == "TheReturnTypeYouWished") //Check your parameters also (use GetParameters)
{
//Call the method if you have a match
AvaliableMethode.Invoke(AInstanceOfMethodes, YourParameter(s));
}
}
C# and most other strongly-typed OO languages implement method overloading. The runtime will automatically dispatch to the correct method of a class based on parameter type, e.g.
ResponseType1 handleRequest(RequestType1 request) { ... }
ResponseType2 handleRequest(RequestType2 request) { ... }
I believe it will work in this case, but can be problematic if there is ambiguity in parameter types (i.e. one method's parameter is a superclass of another's).
Reflection will work but it bypasses compile-time checks and should generally be used only as a last resort.
You can declare a generic class or delegate e.g. RequestHandler<RequestType, ReturnType> and extend it with concrete implementations but the problem of dispatching based on the parameter type is still left as an exercise to the reader.
If design patterns interests you then you can use flavor of Chain of Responsibility(COR) with Command Pattern to redirect the request to it's typed handler.
Design base skeletons -
abstract class RequestBase { }
abstract class ResponseBase { }
/// <summary>
/// Base class which will handle the request if the derived type is responsible otherwise
/// send request to success handler in chain.
/// </summary>
internal abstract class HandlerBase<TRequest, TResponse> // contraints
where TResponse : ResponseBase
where TRequest : RequestBase
{
HandlerBase<TRequest, TResponse> nextHandler;
protected HandlerBase(HandlerBase<TRequest, TResponse> nextHandler)
{
this.nextHandler = nextHandler;
}
public TResponse Execute(TRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
try
{
if (this.IsResponsible(request))
return this.InternalExecute(request);
else
return this.nextHandler.InternalExecute(request);
}
catch (Exception exception)
{
// log exception and rethrow or convert then throw.
throw;
}
}
protected abstract TResponse InternalExecute(TRequest request);
protected abstract bool IsResponsible(TRequest request);
}
Now realize your concrete requests and their respective handlers e.g.
class RequestA : RequestBase { }
class ResponseA : ResponseBase { }
class RequestB : RequestBase { }
class ResponseB : ResponseBase { }
internal class RequestAHandler : HandlerBase<RequestBase, ResponseBase>
{
public RequestAHandler(HandlerBase<RequestBase, ResponseBase> nextHandler) : base(nextHandler) { }
protected override RequestB InternalExecute(RequestBase request)
{
// do what ever RequestA handler shall do
throw new NotImplementedException();
}
protected override bool IsResponsible(RequestBase request)
{
return request is RequestA;
}
}
internal class RequestBHandler : HandlerBase<RequestBase, ResponseBase>
{
public RequestBHandler(HandlerBase<RequestBase, ResponseBase> nextHandler) : base(nextHandler) { }
protected override RequestB InternalExecute(RequestA request)
{
// do what ever RequestB handler shall do
throw new NotImplementedException();
}
protected override bool IsResponsible(RequestBase request)
{
return request is RequestB;
}
}
Finally putting everything together. We have a COR base class which would invoke the responsible handler to manipulate the request. But we need a dispatcher which knows the chain or handlers and can pass the incoming request.
class Dispatcher
{
// cache chained instances of handlers
private static HandlerBase<RequestBase, ResponseBase> handler = RegisterHandlers();
public static ResponseBase Dispatch(RequestBase request)
{
return handler.Execute(request);
}
private static HandlerBase<RequestBase, ResponseBase> RegisterHandlers()
{
// Build chain
HandlerBase<RequestBase, ResponseBase> contextChain = new RequestAHandler(null);
contextChain = new RequestBHandler(contextChain);
// register new handlers here e.g.
// contextChain = new RequestXHandler(contextChain);
return contextChain;
}
}
Usage -
var response = (ResponseA)Dispatcher.Dispatch(new RequestA());
you might be thinking it's too much of boiler plate code. But that's how the frameworks are built(with boilerplating). Now if you want to add a new Handler of request type of X you just need implement RequestX, ResponseX, RequestXHandler and Register it with dispatcher.
Note- Write unit tests to validate the system. I wrote this code on an online C# editor ;) Cheers!!
C#. I have a base class called FileProcessor:
class FileProcessor {
public Path {get {return m_sPath;}}
public FileProcessor(string path)
{
m_sPath = path;
}
public virtual Process() {}
protected string m_sath;
}
Now I'd like to create to other classes ExcelProcessor & PDFProcessor:
class Excelprocessor: FileProcessor
{
public void ProcessFile()
{
//do different stuff from PDFProcessor
}
}
Same for PDFProcessor, a file is Excel if Path ends with ".xlsx" and pdf if it ends with ".pdf". I could have a ProcessingManager class:
class ProcessingManager
{
public void AddProcessJob(string path)
{
m_list.Add(Path;)
}
public ProcessingManager()
{
m_list = new BlockingQueue();
m_thread = new Thread(ThreadFunc);
m_thread.Start(this);
}
public static void ThreadFunc(var param) //this is a thread func
{
ProcessingManager _this = (ProcessingManager )var;
while(some_condition) {
string fPath= _this.m_list.Dequeue();
if(fPath.EndsWith(".pdf")) {
new PDFProcessor().Process();
}
if(fPath.EndsWith(".xlsx")) {
new ExcelProcessor().Process();
}
}
}
protected BlockingQueue m_list;
protected Thread m_thread;
}
I am trying to make this as modular as possible, let's suppose for example that I would like to add a ".doc" processing, I'd have to do a check inside the manager and implement another DOCProcessor.
How could I do this without the modification of ProcessingManager? and I really don't know if my manager is ok enough, please tell me all your suggestions on this.
I'm not really aware of your problem but I'll try to give it a shot.
You could be using the Factory pattern.
class FileProcessorFactory {
public FileProcessor getFileProcessor(string extension){
switch (extension){
case ".pdf":
return new PdfFileProcessor();
case ".xls":
return new ExcelFileProcessor();
}
}
}
class IFileProcessor{
public Object processFile(Stream inputFile);
}
class PdfFileProcessor : IFileProcessor {
public Object processFile(Stream inputFile){
// do things with your inputFile
}
}
class ExcelFileProcessor : IFileProcessor {
public Object processFile(Stream inputFile){
// do things with your inputFile
}
}
This should make sure you are using the FileProcessorFactory to get the correct processor, and the IFileProcessor will make sure you're not implementing different things for each processor.
and implement another DOCProcessor
Just add a new case to the FileProcessorFactory, and a new class which implements the interface IFileProcessor called DocFileProcessor.
You could decorate your processors with custom attributes like this:
[FileProcessorExtension(".doc")]
public class DocProcessor()
{
}
Then your processing manager could find the processor whose FileProcessorExtension property matches your extension, and instantiate it reflexively.
I agree with Highmastdon, his factory is a good solution. The core idea is not to have any FileProcessor implementation reference in your ProcessingManager anymore, only a reference to IFileProcessor interface, thus ProcessingManager does not know which type of file it deals with, it just knows it is an IFileProcessor which implements processFile(Stream inputFile).
In the long run, you'll just have to write new FileProcessor implementations, and voila. ProcessingManager does not change over time.
Use one more method called CanHandle for example:
abstract class FileProcessor
{
public FileProcessor()
{
}
public abstract Process(string path);
public abstract bool CanHandle(string path);
}
With excel file, you can implement CanHandle as below:
class Excelprocessor: FileProcessor
{
public override void Process(string path)
{
}
public override bool CanHandle(string path)
{
return path.EndsWith(".xlsx");
}
}
In ProcessingManager, you need a list of processor which you can add in runtime by method RegisterProcessor:
class ProcessingManager
{
private List<FileProcessor> _processors;
public void RegisterProcessor(FileProcessor processor)
{
_processors.Add(processor)
}
....
So LINQ can be used in here to find appropriate processor:
while(some_condition)
{
string fPath= _this.m_list.Dequeue();
var proccessor = _processors.SingleOrDefault(p => p.CanHandle(fPath));
if (proccessor != null)
proccessor.Process(proccessor);
}
If you want to add more processor, just define and add it into ProcessingManager by using
RegisterProcessor method. You also don't change any code from other classes even FileProcessorFactory like #Highmastdon's answer.
You could use the Factory pattern (a good choice)
In Factory pattern there is the possibility not to change the existing code (Follow SOLID Principle).
In future if a new Doc file support is to be added, you could use the concept of Dictionaries. (instead of modifying the switch statement)
//Some Abstract Code to get you started (Its 2 am... not a good time to give a working code)
1. Define a new dictionary with {FileType, IFileProcessor)
2. Add to the dictionary the available classes.
3. Tomorrow if you come across a new requirement simply do this.
Dictionary.Add(FileType.Docx, new DocFileProcessor());
4. Tryparse an enum for a userinput value.
5. Get the enum instance and then get that object that does your work!
Otherwise an option: It is better to go with MEF (Managed Extensibility Framework!)
That way, you dynamically discover the classes.
For example if the support for .doc needs to be implemented you could use something like below:
Export[typeof(IFileProcessor)]
class DocFileProcessor : IFileProcessor
{
DocFileProcessor(FileType type);
/// Implement the functionality if Document type is .docx in processFile() here
}
Advantages of this method:
Your DocFileProcessor class is identified automatically since it implements IFileProcessor
Application is always Extensible. (You do an importOnce of all parts, get the matching parts and Execute.. Its that simple!)
I built a .NET ASMX web service connecting to an SQL Server database. There is a web service call GetAllQuestions().
var myService = new SATService();
var serviceQuestions = myService.GetAllQuestions();
I saved the result of GetAllQuestions to GetAllQuestions.xml in the local application folder
Is there any way to fake the web service call and use the local xml result?
I just want to take the contents of my entire sql table and have the array of objects with correlating property names automatically generated for me just like with LINQ to SQL web services.
Please keep in mind that I am building a standalone Monotouch iPhone application.
Use dependency injection.
//GetSATService returns the fake service during testing
var myService = GetSATService();
var serviceQuestions = myService.GetAllQuestions();
Or, preferably, in the constructor for the object set the SATService field (so the constructor requires the SATService to be set. If you do this, it will be easier to test.
Edit: Sorry, I'll elaborate here. What you have in your code above is a coupled dependency, where your code creates the object it is using. Dependency injection or the Inversion of Control(IOC) pattern, would have you uncouple that dependency. (Or simply, don't call "new" - let something else do that - something you can control outside the consumer.)
There are several ways to do this, and they are shown in the code below (comments explain):
class Program
{
static void Main(string[] args)
{
//ACTUAL usage
//Setting up the interface injection
IInjectableFactory.StaticInjectable = new ConcreteInjectable(1);
//Injecting via the constructor
EverythingsInjected injected =
new EverythingsInjected(new ConcreteInjectable(100));
//Injecting via the property
injected.PropertyInjected = new ConcreteInjectable(1000);
//using the injected items
injected.PrintInjectables();
Console.WriteLine();
//FOR TESTING (normally done in a unit testing framework)
IInjectableFactory.StaticInjectable = new TestInjectable();
EverythingsInjected testInjected =
new EverythingsInjected(new TestInjectable());
testInjected.PropertyInjected = new TestInjectable();
//this would be an assert of some kind
testInjected.PrintInjectables();
Console.Read();
}
//the inteface you want to represent the decoupled class
public interface IInjectable { void DoSomething(string myStr); }
//the "real" injectable
public class ConcreteInjectable : IInjectable
{
private int _myId;
public ConcreteInjectable(int myId) { _myId = myId; }
public void DoSomething(string myStr)
{
Console.WriteLine("Id:{0} Data:{1}", _myId, myStr);
}
}
//the place to get the IInjectable (not in consuming class)
public static class IInjectableFactory
{
public static IInjectable StaticInjectable { get; set; }
}
//the consuming class - with three types of injection used
public class EverythingsInjected
{
private IInjectable _interfaceInjected;
private IInjectable _constructorInjected;
private IInjectable _propertyInjected;
//property allows the setting of a different injectable
public IInjectable PropertyInjected
{
get { return _propertyInjected; }
set { _propertyInjected = value; }
}
//constructor requires the loosely coupled injectable
public EverythingsInjected(IInjectable constructorInjected)
{
//have to set the default with property injected
_propertyInjected = GetIInjectable();
//retain the constructor injected injectable
_constructorInjected = constructorInjected;
//using basic interface injection
_interfaceInjected = GetIInjectable();
}
//retrieves the loosely coupled injectable
private IInjectable GetIInjectable()
{
return IInjectableFactory.StaticInjectable;
}
//method that consumes the injectables
public void PrintInjectables()
{
_interfaceInjected.DoSomething("Interface Injected");
_constructorInjected.DoSomething("Constructor Injected");
_propertyInjected.DoSomething("PropertyInjected");
}
}
//the "fake" injectable
public class TestInjectable : IInjectable
{
public void DoSomething(string myStr)
{
Console.WriteLine("Id:{0} Data:{1}", -10000, myStr + " For TEST");
}
}
The above is a complete console program that you can run and play with to see how this works. I tried to keep it simple, but feel free to ask me any questions you have.
Second Edit:
From the comments, it became clear that this was an operational need, not a testing need, so in effect it was a cache. Here is some code that will work for the intended purpose. Again, the below code is a full working console program.
class Program
{
static void Main(string[] args)
{
ServiceFactory factory = new ServiceFactory(false);
//first call hits the webservice
GetServiceQuestions(factory);
//hists the cache next time
GetServiceQuestions(factory);
//can refresh on demand
factory.ResetCache = true;
GetServiceQuestions(factory);
Console.Read();
}
//where the call to the "service" happens
private static List<Question> GetServiceQuestions(ServiceFactory factory)
{
var myFirstService = factory.GetSATService();
var firstServiceQuestions = myFirstService.GetAllQuestions();
foreach (Question question in firstServiceQuestions)
{
Console.WriteLine(question.Text);
}
return firstServiceQuestions;
}
}
//this stands in place of your xml file
public static class DataStore
{
public static List<Question> Questions;
}
//a simple question
public struct Question
{
private string _text;
public string Text { get { return _text; } }
public Question(string text)
{
_text = text;
}
}
//the contract for the real and fake "service"
public interface ISATService
{
List<Question> GetAllQuestions();
}
//hits the webservice and refreshes the store
public class ServiceWrapper : ISATService
{
public List<Question> GetAllQuestions()
{
Console.WriteLine("From WebService");
//this would be your webservice call
DataStore.Questions = new List<Question>()
{
new Question("How do you do?"),
new Question("How is the weather?")
};
//always return from your local datastore
return DataStore.Questions;
}
}
//accesses the data store for the questions
public class FakeService : ISATService
{
public List<Question> GetAllQuestions()
{
Console.WriteLine("From Fake Service (cache):");
return DataStore.Questions;
}
}
//The object that decides on using the cache or not
public class ServiceFactory
{
public bool ResetCache{ get; set;}
public ServiceFactory(bool resetCache)
{
ResetCache = resetCache;
}
public ISATService GetSATService()
{
if (DataStore.Questions == null || ResetCache)
return new ServiceWrapper();
else
return new FakeService();
}
}
Hope this helps. Good luck!
when you say fake the call, are you just testing the client side?
you could use fiddler, intercept the request and return the local xml file to the client. No messing around with your client code then.
To elaborate on Audie's answer
Using DI would get you what you want. Very simply you would create an interface that your real object and your mock object both implement
public interface IFoo
{}
Then you would have your GetSATService method return either a MockSATSerivce or the real SATService object based on your needs.
This is where you would use a DI container (some object that stores interface to concrete type mappings) You would bootstrap the container with the types you want. So, for a unit test, you could contrstruct a mock container that registers the MockSATService as the implementer of the IFoo interface.
Then you would as the container for the concrete type but interface
IFoo mySATService = Container.Resolve<IFoo>();
Then at runtime you would just change out the container so that it bootstraps with the runtime types instead of the mock types but you code would stay the same (Because you are treating everything as IFoo instead SATService)
Does that make sense?
Over time I found that an interesting way to do this is by extracting an interface and creating a wrapper class. This adapts well to a IoC container and also works fine without one.
When testing, create the class passing a fake service. When using it normally, just call the empty constructor, which might simply construct a provider or resolve one using a config file.
public DataService : IDataService
{
private IDataService _provider;
public DataService()
{
_provider = new RealService();
}
public DataService(IDataService provider)
{
_provider = provider;
}
public object GetAllQuestions()
{
return _provider.GetAllQuestions();
}
}
I have run into a bit of a desgin issue with some code that I have been working on:
My code basic looks like this:
Main COM wrapper:
public class MapinfoWrapper
{
public MapinfoWrapper()
{
Publics.InternalMapinfo = new MapinfoWrapper();
}
public void Do(string cmd)
{
//Call COM do command
}
public string Eval(string cmd)
{
//Return value from COM eval command
}
}
Public static class to hold internal referance to wrapper:
internal static class Publics
{
private static MapinfoWrapper _internalwrapper;
internal static MapinfoWrapper InternalMapinfo
{
get
{
return _internalwrapper;
}
set
{
_internalwrapper = value;
}
}
}
Code that uses internal wrapper instance:
public class TableInfo
{
public string Name {
get { return Publics.InternalMapinfo.Eval("String comman to get the name"); }
set { Publics.InternalMapinfo.Do("String command to set the name"); }
}
}
Does this smell bad to anyone? Should I be using a internal property to hold a reference to the main wrapper object or should I be using a different design here?
Note: The MapinfoWrapper object will be used by the outside world, so I don't really want to make that a singleton.
You are reducing the testability of your TableInfo class by not injecting the MapInfoWrapper into the class itself. Whether you use a global cache of these MapInfoWrapper classes depends on the class -- you need to decide whether it is necessary or not, but it would improve your design to pass a wrapper into TableInfo and use it there rather than referencing a global copy directly inside TableInfo methods. Do this in conjunction with the definition of an interface (i.e., "refactor to interfaces").
I would also do lazy instantiation in the getter(s) of Publics to make sure the object is available if it hasn't already been created rather than setting it in the constructor of MapInfoWrapper.
public class TableInfo
{
private IMapinfoWrapper wrapper;
public TableInfo() : this(null) {}
public TableInfo( IMapinfoWrapper wrapper )
{
// use from cache if not supplied, could create new here
this.wrapper = wrapper ?? Publics.InternalMapInfo;
}
public string Name {
get { return wrapper.Eval("String comman to get the name"); }
set { wrapper.Do("String command to set the name"); }
}
}
public interface IMapinfoWrapper
{
void Do( string cmd );
void Eval( string cmd );
}
public class MapinfoWrapper
{
public MapinfoWrapper()
{
}
public void Do(string cmd)
{
//Call COM do command
}
public string Eval(string cmd)
{
//Return value from COM eval command
}
}
internal static class Publics
{
private static MapinfoWrapper _internalwrapper;
internal static MapinfoWrapper InternalMapinfo
{
get
{
if (_internalwrapper == null)
{
_internalwrapper = new MapinfoWrapper();
}
return _internalwrapper;
}
}
}
Now, when you test the TableInfo methods, you can mock out the MapInfoWrapper easily by providing your own implementation to the constructor. Ex (assuming a hand mock):
[TestMethod]
[ExpectedException(typeof(ApplicationException))]
public void TestTableInfoName()
{
IMapinfoWrapper mockWrapper = new MockMapinfoWrapper();
mockWrapper.ThrowDoException(typeof(ApplicationException));
TableInfo info = new TableInfo( mockWrapper );
info.Do( "invalid command" );
}
I thought about adding this to my original response, but it is really a different issue.
You might want to consider whether the MapinfoWrapper class needs to be thread-safe if you store and use a cached copy. Anytime you use a single, global copy you need to consider if it will be used by more than one thread at a time and build it so that any critical sections (anywhere data may be changed or must be assumed to not change) are thread-safe. If a multithreaded environment must be supported -- say in a web site -- then this might argue against using a single, global copy unless the cost of creating the class is very high. Of course, if your class relies on other classes that are also not thread-safe, then you may need to make your class thread-safe anyway.