VSIX. Add code to existing class - c#

I created vsix extension which have my custom templates. These templates create custom classes. After that I created a template that should write the pieces of code into another existing class (to supplement it), but in the end it overwrites it. I was looking for an answer on the Internet, but I did not find anything (maybe I was looking for information badly). So, the question is: Is there any opportunity to create vsix with such template and if it's possible how can I do this?
I will be glad for any help!

Related

How do I set common values (abstract) for a C# solution in Visual Studio with multiple sub projects?

I have a solution with multiple projects each of which connects to the same DB and uses overlapping constant values that I would like to set somewhere instead of replicating manually. I have tried a variety of things online like making a custom class and linking projects to it, setting constants in a project config file (which doesn't exist like the guides claim), and so on. I've been unable to figure this out after more than an hour of searching and experimenting so if you have any ideas, let me know. The structure looks like this (the blue-underlined stuff are some of the projects in the list):
You can make another project under the solution to contain your class.
All the other projects can then reference that project, meaning the same functionality will be available in all the other projects without having to duplicate anything.
I will extend the previous correct answer with some more information.
Your solution structure is something to think very carefully as it is a combination of application design/architecture and leads to extensibillity, scalability and future maintainability.
Take for example the following article Common web application architectures.
You can see the Clean Architecture (AKA Hexagonal) which leads to specific projects withing a solution
You can see older designs where the DB access would go into a project called ..DAL
Simple projects can use the second one, more business rich ones the first or something in between.
Check this this article on shared code projects to see about net standard projects
So the above was helpful, but far more complicated than it needed to be. Apparently other answers I'd seen actually work, but it took reading a bunch of other pages to figure out the whole puzzle. The working steps are:
Create a class with public parameters for your constants
Place that class somewhere in your solution space. When I created it on the solution, it was placed in "Solution Items" in my tree (which is the root folder of the solution on the file system).
Right click each project and ADD>Existing Item and point to the class. The KEY (that was missing from most things I read) was that the "add" button" has a drop-down arrow that lets you change it to "Add as link"
In each project (after adding as link to the file), you can directly reference the values as NAMEOFCLASS.NAMEOFCONST but ONLY if you declared them as public const SOMETYPE SOMENAME. Without the const, it's not able to directly reference the value
Note that this fix is in the .sln file itself and needs to be part of the commit or it won't have any effect. It would be nice if you could use "include" or something to bring in a file a folder one level up, but here we are.

.Net : Do I need to create a Custom Control in a different Project?

this question may be asked, but I am wanting to create Custom Controls, and for some reason, every tutorial online says we should create a special project and then reference the dll into the mother project.
Do we really need to do it like that? I'm trying to use ClickOnce and its simpler to deploy when its only one Project.
I want to know, is it necesary to do it like that, and if not can I just create a CustomControl o UserControl and have that called??
It is not necessary but recommended to define your custom control in a separate class library project, so reusing it is as easy as making a reference to that project.

How can I modify the default code generation strategy for edmx?

I want to modify the default code generation strategy, how can I do that?
I simply want to modify the class name from <#=code.Escape(container)#> to Entities and change the default connection string to name=Default.
(I don't want to create a template file for the project, I want to edit it so it will work globally)
I've searched for .tt files, I could only find the ItemTemplates. I don't know what generates the code by default, this is the one I want to edit.
Update: I still don't know how to do this.
You can see what generates the code if you click your EMDX file and check file properties in Visual Studio. Look for Custom Tool property that will tell you the class name of the generator that converts EDMX XML into compilable code.
But regarding model customization, I would still suggest you use T4 that takes your EDMX and generates the same code as original generator. The good thing is that you can then manipulate it 'till you drop dead if you will.
And if you intend to use the T4 on several EMDXs in your project then I suggest you rather create a .ttinclude file and reference it in every .tt file. This way you will reuse existing code and when you'd change it it will be reflected on all generated files.
One more question: What do you mean by globally? Globally for all EDMX files in your project or for all EDMX files on your machine or all EDMX files on your project team or what? Define globally.
Additional edit
Since you've defined global as all projects on a particular machine this is what I'd do.
First of all: using T4 allows you to adjust EDMX -> code conversion changes per project or better said per solution (all projects in a particular solution). On other projects/solutions on the same machine, you should include the same T4 template reference. So it's not actually global in your sense...
The best thing you could do is to create a custom Visual Studio item template with this T4 template so it'd be much much easier adding this default T4 template to your solutions/projects. That's as global as you can make it by T4.
Maybe you should read this MSDN entry that talks about your kind of customization:
How to: Customize Object-Layer Code Generation (Entity Data Model Designer)
I don't know whether it is even possible to alter the default code generation.
Instead of trying to modify the default code generation, I suppose you could create a .tt that generates a derived class from the ObjectContext. This way you can name it and implement the default constructor as you wish.
Something like:
<#=Accessibility.ForType(container)#> partial class Entities : <#=code.Escape(container)#>
{
public Entities()
: base("name=Default")
{ }
}
The downside to this approach is you will need to deploy this .tt file with every EDMX you create.
However, with Visual Studio's addin architecture you could look into creating a template that creates an EDMX and this .tt file by default. As a replacement for adding a plain "ADO.NET Entity Data Model"
Looking into the EntityModelCodeGenerator (the Custom Tool that is run by the default codegen strategy), it seems that it is registered with the SingleFileGenerator extensibility mechanism, which is a COM component. Some more info here.

Custom Common Library Classes in Visual Studio .NET

I wrote some classes that I use with many different projects.
For example, I use Library.Controls.FlatButton.cs almost in every project.
The problem is when I add this as an "existing item"; the class gets created/copied in my soultion folder everytime. And each time I edit/update the contents of that class, I have to update all the Library.Controls.FlatButton.cs files in every project folder.
I need to be able to edit a single source of FlatButton class and when I compile/build a project (that uses the class file) gets updated to the new version of that class.
Question 1: Is there a way to do this?
I know that I can gather all these classes in a library project (Library.Controls) and add it to each application solution as a dependency.
Question 2: Is this the only way to work from a single source of common library files? And if I do; will all the classes in the Library.Controls namespace get compiled with every application, even if I've only used this FlatButton class in the project?
Hope this is clear for you..
thanks
I'd rather go with the approach of the shared library and add them as references to your client project.
If you don't want to do this. You could add the file as "Link". In Add existing item, select Add as Link instead.
Yes, a class library is the way to go and yes, since the whole class library will be referenced from your applications, all the classes will be available to it.
However, the fact that all the classes are available is not a bad thing, since they're in a separate class library it won't make your applications harder to understand (since the amount of code in those applications will stay the same), it might just be that you use up a little bit more hard drive space, though if you really worry about that you could put the class library in the GAC so that all apps reference the same copy of the library, though you'd better research this first to make sure that it's suitable for you.
Alternative way is to add FlatButton.cs file "As Link":

Is it possible to override a method in a class in a dll file?

I've included a dll file into my windows form project.
1. Is it possible to override a particular class entirely?
2. Is it possible to add a new method to a particular class in the dll file?
3. Is it possible to override a method in a class in the dll?
Alternatives I would prefer to avoid:
I know I can use extension methods to create static new methods.
I can also inherit from a particular class and then add new methods to the derived class.
What i'm trying to achieve:
i have to create a project now and add it to a larger project as a dll file.
but we've been told that we'll need to add more functionality to the original project next month. I'm trying to figure out the best way to go about this.
the smaller project is based on mvc design.
You can certainly override a virtual method which you're inheriting from a class in a class library. You do this any time you override object.ToString(), for example!
You can't "override a class entirely" though - I don't know what that would even mean. Likewise you can't add a method to an existing class although you can:
Use extension methods to "pretend" to add another method (but no state, and no properties)
Derive from the class (assuming it's not sealed) and declare your own extra methods
If you could tell us what you're trying to achieve, it would be easier to advise you on how to proceed.
EDIT: When you need to add more functionality to the original project, just add it to the original project. Is there any reason you wouldn't be able to change that project later?
What i'm trying to achieve: i have to
create a project now and add it to a
larger project as a dll file. but
we've been told that we'll need to add
more functionality to the original
project next month. I'm trying to
figure out the best way to go about
this. the smaller project is based on
mvc design.
The current best practice is to use inversion of control (eg. ninject) for these kind of things, if you have a central place for all you dependencies you can wire them up however you like at runtime, intercepting bits and pieces as you wish.

Categories

Resources