Creating a data driven resource manager while keeping the VS2010 designer features - visual-studio-2010

I'm quite used to consume resx designer generated classes, for a clean coding and to avoid mistakes.
However, in a specific project, I need to rely to a database table, containing all labels and their translations in several languages.
I could simply create my own class and get the translated label, however I'd also like to take advantage of VS2010 resource manager and autogenerated *.Designer.cs classes.
I didn't figure out how to do that.
The *.Designer.cs file contains the following comment:
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
So, I suppose I must use the StronglyTypedResourceBuilder (or my own version of this class).
But how to automatically build the class during the design?
And how to save/retrieve data from the DB?

Related

Correct way to use multiple .IDL (MIDL) files within a single "Windows Runtime Component C++/WinRT" project

Visual Studio 2019. Windows Runtime Component C++/WinRT project.
How do I follow and implement the recommendation: "We recommend that you declare each runtime class in its own Interface Definition Language (IDL) (.idl) file, in order to optimize build performance when you edit an IDL file, and for logical correspondence of an IDL file to its generated source code files. Visual Studio merges all of the resulting .winmd files into a single file with the same name as the root namespace. That final .winmd file will be the one that the consumers of your component will reference."
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-apis
Within the same project I can add a second (third and so on) .IDL file using Project->Add New Item->Midl File (.idl). This seems to work ok?! I then manually add associated .h and .cpp files for each .IDL file. Build to generate stub files then manually copy and "fill in" these files before a final build which is successful. In this way I have a single IDL file per runtime class with its associated headers and implementation files including any other files (c++ .h and .cpp files) needed for the implementation. As I need to add functionality to the interface I just edit the IDL file, rebuild and add functionality to the header and implementation C++ files.
Is this what was recommended?!?
When I add the .IDL file and then manually add an associated .h and .cpp they do not appear "under" the .IDL file as the original ones did when I created the new project. Is this just a visual nicety of Solution Explorer OR is this indicating that I have something wrong?!?
I plan to have multiple "C++/WinRT WRC" projects (one for each namespace, each consisting of multiple runtimeclass interfaces with separate .IDL/.h/.cpp files as described above. Then I reference all of the projects (project to project) in my single C#/UWP App.
I am actually trying to implement a user interface to allow me to utilise a large code base of math/engineering C++ classes that implement a very specific & proprietary type of electronic circuit simulation already existing and programmed in C++ and compiled as a console app. The user data in and out is relatively small compared to the processing that takes place to generate the output.
It looks like you are handling the idl files correctly and that behavior is the same as I experienced when just adding a new Midl file directly.
If you like the look of having the .h and .cpp files "under" the .idl file in the tree you can try adding a new View Model instead of a new Midl file. This will create the .idl/.h/.cpp files all with the same name and place the .h and .cpp files visually under the Midl file. The idl template you get this way is for a basic runtime class.
When creating a new Windows Runtime Component, the .idl and the .h are usually grouped under the .cpp, not the .idl, but whatever your preference. There isn't a template to add a Runtime Class, which is pretty dumb. One fix for this could be as follows:
This is not a perfect solution, but is the easiest. Create a new Windows Runtime Component. Open Project>Export Template. Select Export Item. Select the Runtime Class under the new component. Export with Automatic Import option checked. Save your project and exit VS. Open your project. You can now add a fully templated Windows Runtime Class by simply adding item>Runtime Class. However, there will still be no dependencies (there may be a tool for custom nesting, but not in community as far as I can see). To fix this, after adding the files, save the project. Edit the project file (use notepad++ or even unload project, edit) and add the property <DependentUpon>Runtime Class.idl</DependentUpon> between your .cpp and .h file delimiters. Save the project file and reload. Probably want to do this in batches.
The alternative is to write a .vsix to either add the files and edit the project file, or have a right click command that runs on a .cpp and edits the project file, adding the DependentUpon directive. I can't find a 2022 functional .vsix for this. I will post one if I make one.

How can we get the T4 template to generate code based on a .cs file that the user is editing?

I'm trying to create a T4 template that will save our developers from creating a lot of boilerplate code that's necessary in our framework. Let's say the developer creates an interface and marks it with our custom attribute. I would like it so that any interface marked with that custom attribute is enhanced by additional methods, which means my T4 template would have to generate partial classes on the fly. However, I would like it so that this automatic generation happens on the fly and seamlessly, preferably when the internal automatic compilation that's used for intellisense happens. You know how when you create a new class in Visual Studio and you switch to another source file and start using that class you didn't have to save or compile it, Intellisense was able to see the new class you created right away? I'd like the same automatic behavior with the code generated from my T4 template. Any thoughts?
You cannot do what you want to do easily, but here are some options ordered from easiest to most likely what you want (hardest).
Create code
snippets
Create a Visual Studio Item Template
Use Castle DynamicProxy to create the extra bits at run time.
Create a separate project to hold the T4 generated classes as described in my answer here
As a pre-step to your project build (modify .csproj file to do this), you can compile the source code from which you want to generated code and then reflect on that, generate the code and then add it to the project before the real compile step. This is what the MSR Orleans project does. You can read their source code here. This is really cool! :-)

Add Helper Classes to Project By Default

Is there a way to have visual studio add my helper classes (preferably with the correct namespace) anytime I create a new project. For example all of the extension methods, conversions etc. Its a pain to add them every time I start a new project.
Thanks
I'm in VS2012
There are 2 ways:
create a project template
create a set of library
My preferred way is to create a set of library I reuse in all of my project.
I dividet it with different scope for different kind of projects:
MyLib.Core, MyLib.Web, MyLib.Winforms, MyLib.Nhibernate etc etc
I distribute them with nuget so I can easily handle update and versioning

Dynamic DbContext Generation

I need to write a web site content management system that makes use of dynamic Entity Framework DbContext generation. As an input I get a custom data types declaration (in xml) that my C# code knows how to parse. On the output, I need to obtain the code for the DbContext that would provide a facility to store the provided data type instances as DbSet's in a database.
I can see there is .tt -> .cs Generator in a form of a plugin for Visual Studio. I can surely create code that would generate an xml-based (I assume it is) .tt template from my custom declaration and then generate a .cs DbContext for this .tt. But how do I run .tt->.cs conversion manually from code?
Usually T4 templates are transformed at design time inside Visual Studio. If you want to use a T4 template at runtime, change the value of the "Custom Tool" property of the .tt's Project item to "TextTemplatingFilePreprocessor".
This will make Visual Studio create a C# class from your T4 template that can be instantiated at runtime, where you can pass parameters to it (e.g. your xml type declaration). Calling the "TransformText()" will return the actual output code of the T4 template containing the data classes.
In order to use those classes at runtime, too, you would need to compile the output code in memory.

MvcScaffolding a new project and add it to the solution (Large Scale Generation)

If I want to Large Scale Generation and define
Application = Framework (binary core components) + Generated Code + Custom Code
How would I go about creating code generation framework using scaffolding to generate multiple projects and associated files from some metadata (let's say a DSL model defined in a solution folder)
I know that I can use MvcScaffolding powershell cmdlets to add files to the current on another project.
I need to know if I can add a new project (Class library, Web appication) to the current solution from some kind of project template, apply source transformation and possibly merge some custom data. That would allow additional files to be added and I would prefer that both creation of the project and adding some files initially be done in one powershell line based on some input parameters (let say the name of some DSL model, XSD schema, XML data)
Could I just create a new solution and invoke some scaffolders? Are there scaffolders at a solution level?
I would like to have a scaffodling framework resembling software factories (Web service software factory). Any samples, ideas, articles?
Thanks
Rad
I don't see any reason why not.
Your T4 templates can access EnvDTE and so do all sorts of fun VS automation, and of course the .ps1 powershell scripts can (I guess - I am no powershell guru) do pretty much anything you yourself can do on your box.
But out of interest why would you want to generate whole projects? i.e. are you sure that is time saving?

Resources