When is it OK to inject the injector? - c++

If you take a look at the library I've been working on for dependency injection in C++, I recently added an example that mimics something I created for a real project: the ability to inject application configuration directly.
This all works fine as the constructor just asks for a ConfigItem<ConfigTag> type and that is magically delivered.
A problem occurs when I need to access all of them at the same time (say for a configuration dialog).
The stupid solution I came up with was to generate, using the preprocessor, a class that gets all the configuration items in the constructor as shown here.
This problem would be "nicely" solved if I could inject the injector as I would then only have to generate the code to collect all the ConfigItem instances as shown here.
Note that all the ConfigItems are within a singleton scope.
Hope this question makes sense: how would you do this?

Sounds like you have re-invented the Service Locator Pattern

Related

Unit Testing Bot Framework

I'm trying to figure out how to unit test a basic MS Bot Framework dialog and cannot get it to work the way everything on the internet says it should work.
Everything I find basically says follow this:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Tests/Microsoft.Bot.Sample.Tests/EchoBotTests.cs
Well, here's the problem with that:
await Conversation.SendAsync(scope, toBot);
That is defined as internal so it is not accessible outside of the bot.builder code. So it is totally useless unless you are programming tests for internal bot.builder stuff.
Is there a new way to get around this?
Bot Framework is an open source project, you can download the code and modify it as you need. In your case removing the internal keyword. Another option would be to create a new class that inherits from the class you are trying to use and making your own access levels on the methods you need to override. This blog post describes how to use the code locally.

Does AspectJ support modifying the JDK bytecode?

I want to intercept ClassLoader's loadClass method. In order to show the process of loading class by ClassLoader. But I don't know the working principle of aspectj. It can modify the source code of the JDK?
You could just use a debugger and step through the process in order to understand it.
AspectJ can weave aspects into existing class files during compilation (CTW, compile-time weaving) or during class-loading (LTW, load-time weaving).
LTW does not work for JDK classes because those classes are always loaded before the weaving agent. So you have a bootstrapping problem there.
What you can do, however, is to apply CTW to the class files in e.g. rt.jar from your JRE/JDK distribution, zip the woven class files into another JAR and replace the original one with it. Then if you start the JVM with aspectjrt.jar on the boot classpath, you see the aspects in action. I have done that before and it works. But really, this is not what AspectJ was designed for. But you asked, so I told you. It is a very advanced topic though, and I doubt that it is the road you should take as a complete AspectJ greenhorn - no offense meant.
Update: Because I have been asked this question so often, I created a little demo project showing how to weave aspects into the JRE/JDK. I still do not think it makes sense to use it under normal circumstances, but what the heck: Why do people climb the Mount Everest? Because it exists. ;-)

How to ignore generated code from code coverage data

I am using Visual Studio 2010 and would like to exclude the generated service reference code from my code coverage statistics.
I found an article pre 2010 that mentions using DebuggerNonUserCode and DebuggerHidden attributes. I have tried this an it works as advertised. DebuggerNonUserCode is set at the class level, but with 50+ classes generated in each of the generated service reference code files, this is not an attractive option.
Does anyone have any alternative solutions?
The generated classes are partial. If you create a new class in your project with the same namespace and class declaration you can add the [ExcludeFromCodeCoverage] attribute to your partial class. That way you don't have to go back and edit the Reference.cs file whenever you refresh your reference.
In Reference.cs, you can find an existing attribute, like [System.Diagnostics.DebuggerStepThroughAttribute()] and do a search and replace with [System.Diagnostics.DebuggerStepThroughAttribute()][System.Diagnostics.DebuggerNonUserCode()].
The major drawback is that you have to redo this each time you update the reference.
I don't understand why MS does not make the code coverage tool smart enough to skip service reference generated code.
System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage can be used on top of the class. This is a poor option since you need to redo this anytime you regenerate your code. Maybe Microsoft could do this for us automagically when creating service references, entity framework types, etc...
You could create a code generator that emits partial classes with the DebuggerNonUserCode attribute.

Is there a C++ or Qt library available to measure feature use of an application

I would like to be able to measure the features in our application that are being used.
For example how much certain windows are opened, certain controls are clicked.
I can imagine a tool that measures this and sends a report to a web server, that can further process it to create meaningful data from it
First question : should you do it ? People don't like when their software phones home without their consent. But assuming they are ok with it then:
It's technically possible, with two approaches: automatic or manual. Of course, given your question, I assume that you are using Qt.
Automatic:
give a proper name to all the QObject that you want to trace
install an event filter on your application to catch all the ChildEvent about objects that are created and destroyed.
from the ChildEvent, you can extract the object's name
then you can already log how often that object is created. You can also use the opportunity to add an event listener to that specific object, to be notified when it is shown or hidden or track other kind of usage
log everything to a log file
Manual :
add log statements to relevant part of your code that you want to track.
Final :
send the log file on a regular basis
I guess, your answer is "No". I don't think there are such libraries.
I also think, the best solution here is logging, meaning you should manually introduce some log functions into your main program features and send back the log file. When it comes to logging, you may consider using aspect-oriented programming (and there are such tools for C++), it may simplify your task...

MFC Container based application

I have a Doc-View architecture based project completely ready. But now i want to convert it in container based application to provide OLE support to existing project.
if anyone know how to convert, please reply as soon as possible. i just need a way without copy paste of existing code to new container based application- i tried - not worked even after settings change.
There is a good worked example of this here. Basically all your CDocument derived classes must be now be derived from COLEDocument, and you need some extra code in your WinApp.InitInstance.
If you post the code that you tried, you may get a more detailed response as to what is going wrong.