Output one C# class per file with protogen? - c#

Is it possible to have protogen output multiple files (one per class) based on a single .proto file?
I'm working with a very large .proto file that outputs approx 200 classes, currently all in a single file. One of the places where I need to use the generated classes is in a highly memory constrained environment (a Windows Phone background agent).
I'd like to be able to only include the necessary classes in the assembly loaded in the constrained environment but can't easily do this when the generated classes are all in a single file. If I could have them outputted to multiple files I could only link in the ones I need in the assembly for the constrained environment.
Is there a way to have protogen output the classes in separate files? I can't see an option for this and am currently only using the umbrella-classname option.
Manually editing the generated file is not an option so if protogen can't do it, is there another commandline tool available which can split up a file containing multiple classes? (To save reinventing the wheel.)
Update
I'm using Google.ProtocolBuffers.dll an inherited decision and not easily changable.
Editing/splitting the .proto file is also not a posibility. (Unless as a custom step.)

We have an option for this in csharp_options, but it's not implemented:
// Whether to generate a single file for everything within the
// .proto file (false), or one file per message (true).
// This option is not currently honored; please log a feature
// request if you really want it.
optional bool multiple_files = 4;
Given that you'd have to remove relevant files and make sure you got all the dependencies right it sounds like it wouldn't actually save you much work over the solution I'd suggest, which is to split your proto file into separate ones You say this is "also not a possibility" but basically that's all I can suggest at the moment - why is it not a possibility?
EDIT: I've just had another idea. You could potentially run the protoc step of protogen (which is now done automatically if you don't specify otherwise) to parse the .proto file into its descriptor. Then load the descriptor in another program, mutate it as you would any other protobuf message (create a builder from it, edit the message, build) and then save the descriptor. You can then use protogen on the remaining descriptor, and generate only the classes you want...

Related

How can I save a C# project in VisualStudio into a single .cs file instead of a whole project folder?

I have been practicing programming this way, but for my exam I will need to hand in a single script file, and I don't know how to do that. When I try to create a single file it's totally empty, is there a way of having all that starting structure but still saving only into a single .cs script?
A "project" (.csproj file plus one or more .cs files): no. (Unless you mean some archive format like Zip.)
But there is no limit on how many namespaces, classes, etc. can be in one source file. While good practice is to put one top level type in one file (member types and partial classes can modify this) this is not required by the compiler.
NB. the use of the term "script" in your question needs better definition here. Typically one refers to "source" files. Maybe your instructor means "project write up" as a single document, with the expectation you'll include the source in it?

Best way to make methods easily callable and configurable by third party

I have a solution with several self-contained classes and methods. For example, I have:
a FileDownloader class that has multiple different methods that download files based on passing in a URL or multiple URLs
a DataTransformations class that has multiple methods that transform data depending on what is necessary for a given operation
a FileWriter class that writes some data to some kind of file type or file format
etc.
I have all of these classes as .cs files under the same solution. I can consider the order of executions for some specific operation and call the methods from MAIN in the correct order and it produces the output that I expect. I will eventually, however, need to call some or all of these methods in many different configurations for several different processes and I don't know how to do that. I know how to pass in configuration through command line arguments, but even that requires the specific order and number of methods called stays the same between processes. This is not tenable because I will not need to download files in some instances and I will not need to transform data in some instances etc. I am very new to .NET development and I have not yet wrapped my head around how to truly decouple these classes from each other. Do I have to deploy a different solution for each class? I would like to just be able to say "call file downloader with these parameters" and then "perform data transformations based on these parameters" basically like steps in an execution job.
Dirty Answer, Compile as a library and then add a reference in whatever project you want to use those methods for. you can then call the methods by name (LibraryName).MethodName(Parameters). of course you will need to always have that DLL in whatever other project need access. if you have any questions on how to do this let me know.

"Interface" for .Net Resource files

I am building a multi-language MVC application and have a series of resource files with translated strings for messages that will be displayed to the user.
Is there any way of ensuring that any resource files added in the future have all required keys and are spelled correctly?
As an analogy, if the resource file was a regular class, you could provide an interface to ensure that all required method and properties were present in the implementing class. Is there a similar concept for resource files?
I've been unable to find a supported way to enforce an explicit contract upon a .resx file. Since your goal is ultimately to catch implementation errors before they show up at runtime (and compile time checking isn't possible), I recommend falling back to static code analysis. Luckily, .NET makes this trivially easy:
Use the System.Resources.ResXResourceReader class to read the contents of the resx files to be validated.
Implement a test that asserts against all required keys in the "contract" you'd like to enforce on the resx.
Test should run as part of an existing test suite, and failure will warn a developer of the implicit contract before encountering the problem at runtime.
Since your resource files will exist in a known location, you can trivially ensure that the tests run against all resx files in that directory. In this way, you don't even need to update the test when new resource files are added, only if the contract changes.
I've used a similar approach to help with maintenance of stored procedure names kept in (an extensive number of) resx files. Since the resource files are spread across dozens of projects, manual maintenance is tedious and error-prone -- in other words, it doesn't get done. The static code analysis approach has yielded few downsides, and I think it would work well in your case as well.
Landing page for resource files on MSDN
ResXResourceReader on MSDN
System.Resources.ResXResourceReader requires a reference to System.Windows.Forms. It's available on both .NET and Mono.

How to use environment dependent app.config file

We are a group of C#/.NET 4.5 developers working on the same application.
The application has a set of configurations related to each developer machine, like the connection string to the DB, network related settings (proxies, IPs, credentials) and a LOT MORE.
Because the application has grown we are incurring in a lot of environment related configurations like for example:
If this is MyPC then load the connection string for my PC.
If this is the XDeveloperPC then specify proxy’s settings.
Also if new developers leaves or join the group, then the process to update the file becomes a total head ache. Maintaining the file has become very hard and is a possible source of bug and errors.
I was thinking in having specific app.config files related to each developer environment like:
app_MyPC.config
app_XDeveloperPC.config
And when the application builds or executes then specify which one to load as if it where the default app.config of the application. Then, when the application or any class or method refers to a given configuration (like the connection string) is access to this configuration file as if it where accessing to the app.config default file.
I would not want to create a Configuration class that builds immediately when the application starts because then I should have references from every place to this class and the application is quite large, with a bunch of projects and dlls.
I rather prefer to hear some opinions and what do you think should be the best way to achieve this.
Is it possible to achieve this?
How?
Do you know a better approach?
FYI, please note that .NET only loads one config file for the whole application. You could try multiple config files something as like specified here,
Multiple App.Config Files in .NET Class library project
Hope this helps...
You can specify sections of app.config to be loaded from another file. See this answer
However, you might have to customize the above solution, the app.config files and the way configs are organized.
Another approach is to use a custom settings file instead of app.config. This will need some code change to use the config file. This config can either be an XML or a JSON file (JSON is easy to deal with). You can use an inheritance model wherein generic settings come from one file, specific settings come from another file and so on. Also, you can load settings from such file at runtime and change application behavior on the fly.
If you decide to use custom config file, and if you already have lot of code written considering App.config file, you can abstract the app.config calls to a method and let that method deal with where to pull the settings value from. That way you can minimize the code change and keep everything tidy.
Maybe you can use the machine.config file (C:\Windows\Microsoft.NET\Framework\your Framework version\Config\machine.config)

F# Data CsvProvider use from a C# application Interop

Extreme noob learning question ahead:
I have a module that I'd like to use in my C# (universal) application. The C# app will download and unzip a file containing 12 CSVs that will always follow the same format. So what I've done is download the CSVs ahead of time, and added them to my solution so that the file referenced in CsvProvider<"thefile.csv"> will be there at compile time.
namespace ExperimentalFSLibrary
module CsvHelper =
open FSharp.Data
let GetCsvA path =
CsvProvider<"thefileA.csv">.Load(path)
Then call this from my c# application like so:
var ReceivedCsvA = ExperimentalFSLibrary.CsvHelper.GetCsvA
I haven't had any success getting the data from the F# library
There's also the issue of how to deal with twelve different files, since I have to specify the template file for the CsvProvider, I imagine I'd have to write 12 different functions?
I've searched around SO and have found things that have gotten me this far but I've hit a wall. Any help including a sanity check would be very appreciated.
The CsvProvider in FSharp.Data is an erasing type provider. This means there is no type to consume and use from within C#.
Many F# type providers, such as this, will only be beneficial when used from within F#.
In this case, you'd most likely be better off parsing the CSV data and returning the results you need via some API consumable from your C# code.
There's also the issue of how to deal with twelve different files, since I have to specify the template file for the CsvProvider, I imagine I'd have to write 12 different functions?
In general, you'd need a separate function per file structure, not per file. If the 12 files all use the same columns/headers/etc, then a single type provider type will work across each of them. The static filename provided is used only to determine the column structure of the generated types.

Categories

Resources