How To: Code Generators and Code Snippets

Share

As you know, Snippets are available since Visual Studio 2005 and Add-Ins since Visual Studio 2003, but we rarely asked “Can these help our work?”. Yes, they do. It’s not important how they are created, for this are many tutorials available, important is how those could help us.

Code Generators

To create Code Generators is needed to do a laborious work but the tool will have a high degree of reusability. An easy method to create code generators in Visual Studio 2008 can be found on Sebastian Negomireanu blog. So if you have to make Rest Calls Services you need to write 20 lines of code for each group of methods BeginXXXX – EndXXXX. Because the code is always the same, except the types, we can make something clever that generates that code. So we can create code generators which works with a xml file as input, like:

<?xml version="1.0" encoding="utf-8"?>
   <Service name="CleintsService" namespace="MyCodeGenerator.Service">
 
      <Method name="GetClients" returnType="IEnumetable&amp;lt;ClientModel&amp;gt;"/>
 
      <Method name="AddCleint" returnType="ClientModel">
         <Param name="client" type="ClientModel"/>
      </Method>
 
</Service>

For instance, if we have the module called “Clients” and we need to create CRUD for this module, all we need do is to put all the methods name in the xml. The tool will create all code in accordance with the file. Second step is to access the methods that make asynchronous data transfer. For this is exposed an interface with the name like in xml file.

public interface IClientsService
{
        void BeginGetClients(AsyncCallback callback, object state);
        IList EndGetClients(IAsyncResult result);
 
        void BeginAddClients(AsyncCallback callback, object state, ClientModel client);
        ClientModel EndAddClient(IAsyncResult result);
}

Because the backing implementation in encapsulated, we can change it, whenever we want without affecting the entire application. An example of how we use it:

 IClientsService clientsService;
        public void GetClients()
        {
            clientsService.BeginGetClients(ReceiveClients, null);
        }
 
        public void ReceiveClient(IAsyncResult ar)
        {
            IEnumerable clients = clientsService.EndGetClients(ar);
        }

Snippets

As we know the vertical programming is better than horizontal programming. Besides that, we face the problem with repetition code. We always complain about the programmer’s life, the time spent in front of computer, but we never do anything. Anyway, Snippets really helps. Snippets allow you to type single/multiple key strokes to define blocks of code. If you want to learn how to build your own, check these links out (taken from this MSDN article)

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
      <Header>
         <Title>ocp</Title>
         <Shortcut>ocp</Shortcut>
      </Header>
      <Snippet>
         <Code Language="CSharp">
            <![CDATA[MessageBox.Show("Hello World");]]>
         </Code>
      </Snippet>
   </CodeSnippet>
</CodeSnippets>

This is a code for showing a MessageBox, but for instance take the IDisposable implementation. Imagine how it is to write the same code, the same comments every time …. we get crazy soon. You can find here 5 Useful Visual Studio C# Snippets or you can build your own according to your needs.


Both are a way to make our life easier than before. Remember: Kepp IT Simple.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

Tags:

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close