How to properly add comments to generated class properties? - c#

I have some classes generated by EF out of an Oracle database. Those class will be used to build a restful Web API with Help Pages. I wonder how do I add comments to the properties for those generated classes. I can edit those generated class files, but if I have to remap them, they'll be gone.
I tried creating DTO classes for the generated classes and use AutoMapper, but that quickly go out of hand since I have so many classes to create DTO for, and the worse is that the derived class will end up with two properties and that makes the Help Page not very helpful.
I hope C# lets me to redefine a property of a class, but I know that's not possible, I wonder what's the least painful way to add comments to generated classes.

You don't need to place comments in code: it can be done in an XML file.
This is an old MSDN article, but check the resulting XML document at the bottom of the article.
That is, you can create your own XML document and distribute it along with your assemblies as auto-generated ones.

Related

XSD deserialization generates a mess of partial classes

I have 12 quite complex schemas that I want to de-serialize. They form a kind of hierarchy, making use in each of them of the definitions of other(s). So I use the XML Schema Definition Tool like this, with the dependencies following the schema to be deserialized:
xsd.exe /c schema.xsd importedSchema1.xsd importedSchema2.xsd importedSchema3.xsd importedSchema4.xsd
and the result is something like:
schema.cs
schema_importedSchema1.cs
schema_importedSchema1_importedSchema2.cs
schema_importedSchema1_importedSchema2_importedSchema3.cs
schema_importedSchema1_importedSchema2_importedSchema3_importedSchema4.cs
And they're full of partial classes. There exists a way to avoid this?
I'm newbie with C# and it's the first time I play with serialization, so I thought that maybe I'm missing something out. All the examples I find are too simple, I can't find a complex situation like this. So I don't know what is exactly happening here, if there is a better way to make it or how I'm supposed to make use of these classes if they're splitted like this. Some suggestions?
Here there is a very clear explanation of what was happening to me:
XSD Gen Classes That Reference a Common Type
But the answer that best suited to me was this:
xsd.exe to generate classes as separate files?
Is not exactly what I wanted (I wanted all the types defined in a xsd in a single file), but anyway, that tool is a great help!
If you're asking 'why are they partial classes', the answer is here:
"Don’t forget that classes created by xsd.exe are all partial classes.
It’s a good idea to add default constructors and logic in a separate
partial class in a different file. It’s especially helpful for
initializing arrays since xsd.exe generated classes use arrays and not
ArrayLists or Generic Lists. This allows you to add logic, that won’t
be changed when the class is regenerated."
But, personally, I found the naming conventions of the auto-generated stuff to be ridiculous so I'm just going to need to tough it out in the future, using the auto-generated stuff as a guide to update my classes.

Autogenerate C# WebApi from Models

Does anyone know of any tools that can generate a C# WebApi REST Interface for a model?
What I would like is to define my model and add attributes that describe properties of the resource in the context of standard REST architecture. After defining the model and adding attributes, the hypothetical tool would generate all the code needed for implementation.
I'm not 100% sure if this tool will work for your very specific case, but it is a code generator nonetheless and appears to be relatively versatile.
http://www.codesmithtools.com/product/generator
I'm assuming you already searched for a code generator to suit your need so if this one doesn't work for you, you may have to face the music and write one yourself.
A co-worker I work with here wrote a generator in C# that pulled schema/data structure info from a SQL database, took that info, and simply output C# code to a .cs file.
I know this isn't the best answer, but hopefully it helps in at least some way. :)
Use OData to create simple rest services(GET, POST, PUT, DELETE). OData is being widely accepted by many UI components which allow us to use dynamic querying at request level.
You can simply replace the Model Names and DTO Names in an odata controller to use it to other model's controller.

Visual Studio Partial Class Per Method Downsides?

In Visual Studio, using a c# project, instead of placing a class that contains multiple methods and properties in a single file would there be any downsides to using multiple files with the partial keyword and nested file linking?
For example, if I have a class called Customer that has some properties and two methods: GetOrders and GetAddress. Instead of creating one file called Customer.cs and placing all the code for the properties and two methods in that file I would create a Customer.cs and place only the properties in that file. I would mark the class as partial. I would then create each method in a new file called Customer_GetOrders.cs and Customer_GetAddress.cs, each containing a Customer class marked as partial and only the code for that method. In Visual Studio I would nest the Customer_GetOrders.cs and Customer_GetAddress.cs files under the Customer.cs file.
The upsides I can see are less code in a file to look at so instead of scrolling up and down in a big file you would only see the code dealing with the method you are working on. Also if you are using source control merges would be easier since you would only have to deal with the code in each method. And since methods are bound by physical files you could easily see the change history of a method by looking at the change history of the file.
The downsides I can see are having a lot of small files but I don't think that would be so bad. Are there any other downsides with this line of thought?
Thanks,
Frank
The upsides I can see are less code in a file to look at so instead of scrolling up and down in a big file you would only see the code dealing with the method you are working on. Also if you are using source control merges would be easier since you would only have to deal with the code in each method. And since methods are bound by physical files you could easily see the change history of a method by looking at the change history of the file.
All of these upsides, in my opinion, are only "upsides" if the class is too big. If your class adheres to the Single Responsibility Principle, the file should never be "too big" to manage.
Also, most IDEs (such as Visual Studio) already provide a huge amount of functionality to navigate the files quickly (such as the pulldowns that jump directly to members).
The downsides I can see are having a lot of small files but I don't think that would be so bad. Are there any other downsides with this line of thought?
You're splitting your types up across multiple files, which makes it far less maintainable and more difficult to follow, as the data used by the type is no longer near the methods that use it.
You also add extra maintenance cost to refactoring, as method renames, for example, now would require additional work (file renames) which would break your "history" of that method within that file.
Overall, I'd find this a bad practice. Partial classes are great if you have generated code, and want to be able to add other logic to a generated code file, but otherwise, they tend to be something I'd personally avoid.
It is a Code Smell to me: a class sufficiently large that something is gained in understanding by partitioning into multiple parts is an indication that it has too many responsibilities. It's probably a poorly thought out abstraction that should be partitioned into multiple classes.
For example, if I have a class called Customer that has some properties and two methods: GetOrders and GetAddress. Instead of creating one file called Customer.cs and placing all the code for the properties and two methods in that file I would create a Customer.cs and place only the properties in that file. I would mark the class as partial. I would then create each method in a new file called Customer_GetOrders.cs and Customer_GetAddress.cs, each containing a Customer class marked as partial and only the code for that method. In Visual Studio I would nest the Customer_GetOrders.cs and Customer_GetAddress.cs files under the Customer.cs file.
From a modelling perspective, GetAddress() and GetOrders() shouldn't be methods, at least, not on the Customer object. A Customer probably has 1 or more Address properties and single, collection-like property, Orders, that represents the customer's order history.
I think your abstraction is missing some classes. Perhaps you need an OrderFactory, that given a Customer (and possibly other criteria), knows how to find 1 or more of the customer's orders.
There is actually a big problem with visual studio. The more files that you have the longer it takes to compile and even load a solution. Give it a go sometime with lots of text files, imagine 7 or 8 extra files per class a standard solution would explode in size in no time and .
From a .net compiler perspective there is nothing wrong with this.
The only other problem is maintainability ie code navigation knowing what goes where.

T4 Templates - Generates Unit Test classes from the classes which implement an interface

I have a large number of classes which all use the same interface. This is the first time this code will have unit tests so I'm trying to think of the best way to generate all of these tests, or the structure of the classes at least.
I know T4 Templates are used in EF to generate multiple files based on the model. Is there a way I can specify in the T4 Template that I want it to look at all of the classes which implement an interface and generate a unit test class for each. Alternatively, if it's easier, all of the classes are within the same folder so if I could just point it at that folder it would probably be fine.
Once that is done I can fill in the details of each unit test class with the specfics. I'd obviously have to remove the T4 file so it doesn't overwrite the code I've added to the classes, maybe there is a way to do that too?
Take a look a this blog post:
http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html
It should enable you to generate multiple outputs with a single .tt file. I use it in (almost) the same way you do, I generate a huge class of methods (3000 lines) based on an xml file and then I generate another file containing unit tests for those methods. Ironically, the unit tests pointed out several bugs in my big generated class, even though the unit tests were generated also.
A T4 template always generates only one piece of output, be it one output file for the design time template or one string for the runtime template. The template itself will therefore not be able to generate you multiple files. Nonetheless, with a little code, you can use a single runtime template multiple times, to create multiple pieces of output as you like.
I suggest to create a simple utility, separate from the main project, which will contain the T4 runtime template and the code which will, for example, inspect a given assembly or bunch of given source files and generate the appropriate test skeleton using the T4 runtime template and save it to a file for each of the classes of interest. I guess it depends on the number of classes in question, if it is worth it to spend a little time to create such a tool.
If it's not clear enough, just let me know and I will try to provide an example.
I'm doing something similar in a new project, where I want to use a T4 template to generate some code that pertains to all the classes that inherit from a particular class. (Basically write a factory generator class to create a new instance of each derived class given only a string containing the class name, and also to collect a list of the public properties in each class without using reflection at runtime. That way, other developers can just add the new class to the project without worrying about tying it into our framework.)
I'm also keeping the source classes in a common subfolder.
While I'm only starting to design how the template works, the template will run before the code is compiled, so my process will be to:
For each source code file in the subfolder:
Load the file into memory.
Use regular expressions to verify the class in the file inherits from the desired base class.
Use regular expressions to extract the data I need.
Write the output class using the data I've gathered (class names, public property names)
This is something where Roslyn may be useful in analyzing the code in order to extract class and property names, but I'm not going to get too fancy just yet. My approach will limit us to one class per file, and will only work with the one language we're writing in, but that's convention over configuration that we can live with.
You may find this MSDN article useful, as well as Oleg Sych's blog and toolkit. Oleg's T4 Toolbox includes a template for generating multiple output files from a single T4 template.

Add a property to already created Type at runtime C# ASP.NET

I had a requirement of generating classes and its objects at runtime. Hence, looking at this article I created the same. (using )
I am storing all created types in a list.
But now the other requirement is to add properties to already created Types.
This is for the reason, if i want to use say Class A as a property Type in Class B and say Both in Class C.
I read a lot of articles on the same but have not yet come to a solution
Any Help will be appreciated.
Thanks
Actually, i am in process of developing a multitenant application like LitwareHR by Microsoft.
This will be a system where admin can make sub sites with same escalation management functionality (like MS sharepoint)
Everything is done except workflows!
For data to be stored in tables, i am storing it in XML format..
Eg:
<root tablename="UserInfo">
<column name=\"Name\">Miron</column>
<column name=\"Company\">IBM</column>
</root>"
Everything from controls on the page to events to validators to web parts gets created on runtime using XSLT.
Here, the challenge comes when i need to use expression evaluator to apply workflows to it.
Eg: If UserInfo.Name == "Miron"
Everything gets created on runtime, so have to retrieve table info as an object.
Let me know if i am not clear!
If the types exist then this gets very tricky; you can't add actual properties to an existing type, but if the code that *inspects *the values uses TypeDescriptor (which most data-binding does) then you can add properties sort of via custom PropertyDescriptors - either by implementing ICustomTypeDescriptor (which requires that you do something at build), or TypeDescriptionProvider.
Both are very complex, and both also demand that you have somewhere handy to put the extra data (a property-bag).
Note that in 4.0, dynamic may have some usefulness here.
If you want to avoid this, then just wrap the types in something that looks similar but with extra properties. It'll get the job done while retaining sanity.
Yes, you can use Composition as you described to do this, but classically one would use inheritence for adding functionality to an existing type.
It is difficult to answer your question without more detail about how these classes are to be used, what will be calling them and how.
I believe you will have to derive your classes from single base. Also, to be able to:
use say Class A as a property Type in
Class B and say Both in Class C.
you will have to prepare class A, in case of it being a property of B; and classes A and B ready for them to be a property in Class C.
It would be helpful if you can add more information to your question.

Categories