[PT] Revista PROGRAMAR Edição 47 – Dezembro de 2014

Revista PROGRAMAR 
Edição 47 – Dezembro de 2014


(clica para download)

A poucas horas do Natal, aqui temos mais um edição da Revista PROGRAMAR! Nesta edição trazemos até si, como artigo de capa, o artigo Integrar a Cortana numa aplicação Windows Phone, de Sara Silva. Nesta 47ª edição pode ainda encontrar os seguintes artigos:

    • Estruturas de Dados Genéricas
    • Sencha Touch – Parte I
    • Registo de Todos os Comandos Executados num Sistema Informático
    • Como Suportar Diferentes Idiomas numa App Android
    • SandCastlle Help File Builder
    • Desenvolvimento de Aplicações Profissionais em Android
    • rojetos de POO em JAVA
    • Windows 10: A Nova Geração Windows
    • Xamarin: Sem custos para estudantes
    • Que tipo de BD és tu?

Continuaremos também a premiar os autores dos três melhores artigos, dado o sucesso nas edições anteriores. E os leitores devem dar a sua opinião para que possamos premiar correctamente. Para isso vote em http://tinyurl.com/ProgramarED47-V.

Azure Mobile Services: How to creating a Dev and Prod environments

Scope

This article has the goal to show how to create a development and production environment for a .Net Backend from Azure Mobile Services.

Introduction

According the MSDN Documentation,

Microsoft Azure Mobile Services is an Azure service offering designed to make it easy to create highly-functional mobile apps using Azure. Mobile Services brings together a set of Azure services that enable backend capabilities for your apps. Mobile Services provides the following backend capabilities in Azure to support your apps:

  • Client libraries support mobile app development on various devices, including Windows 8, Windows Phone 8, iPhone, and iPad.
  • Simple provisioning and management of tables for storing app data. 
  • Integration with notification services to deliver push notifications to your app.
  • Integration with well-known identity providers for authentication.
  • Precise control for authorizing access to tables.
  • Supports scripts to inject business logic into data access operations.
  • Integration with other cloud services.
  • Supports the ability to scale a mobile service instance.
  • Service monitoring and logging.

This way, Azure Mobile Services will increase the mobile application development and the fact it is available for the main mobile platform is a plus.

In some scenarios, more specific while new features are provided to the mobile applications that is published in the store and used by users, different environment from the Azure Mobile Service can be required, because each version can change or even break the last backend version from Azure Mobile Service and is important to test and keep the production running. Azure Mobile Services allow to test the services in localhost, but not provides a way to have different environments and to create different environments is required to have one Azure Mobile Service for each environment and then create configuration for each one.

Description

To create a development and production environment for a .Net Backend – Azure Mobile Services is required to create a Dev and Prod Backend and then is possible to create the Dev and Prod profiles based in transform files.

Let’s see it in more detail.

Creating the Azure Mobile Services

In Azure Portal, create two .Net Backend – Azure Mobile Service, let’s calling it as following

  • For the development environment:
    • MyAMSDev – the Azure Mobile Service name
    • MyAMSDev_db – the database name
  • For the production environment:
    • MyAMSProd – the Azure Mobile Service name
    • MyAMSProd_db – the database name

In Azure Portal, the result will be azure portal

And

azure portal

Is possible to use the same database, but for it is necessary to use different schemas and if different databases are provided is easier for manage each environment (update or even delete). A Backend that uses Entity Framework migrations will recreate the service from the scrats and with it any time it can be delete, deployed and recreated.

To read more about the steps required to create an Azure Mobile Service, read the following article

How to create the MyMenuApp Azure Mobile Service.

Creating Transform File

In Visual Studio, select the solution and open the context menu as following

azure mobile service

After it, click in Configuration Manager as following

azure mobile service

And then create a new configuration as following

azure mobile service

azure mobile service

In this case, a Dev configuration is defined based in Debug configuration and to the Prod configuration should be used the Release configuration as following

azure mobile service

At this moment, is possible to define Debug, Dev, Prod and Release configuration

azure mobile service

For each configuration is possible to define the conditional compilation. For example, in Dev is possible to have

azure mobile service

In reality it mean, we can do

#if Dev

// for example can fill the database with fake data

#else

// in other environment different from Dev

#endif

To have advantages of this configurations using the Web.Config, let’s add the config transform as following

azure mobile service

Which the result will be

azure mobile service

Now is possible

– To set in the Web.Config, the connection string to the local database, used by the localhost

 <connectionStrings>
    <add name="MS_TableConnectionString1" connectionString="Data Source=LT114925;Initial Catalog=MyAMSLocal;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
   </connectionStrings>

– To set in the Web.Dev.Config, the connection string to the MyAMSDev_db database, used by the Dev environment (or even the localhost!)

      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

– To set in the Web.Prod.Config, the connection string to the MyAMSProd_db database, used by the Prod environment

   <connectionStrings>
      <add name="MS_TableConnectionString1"
        connectionString="<connection string from MyAMSProd_db>"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>

And in each deploy the respective configs will be used.

Let’s publish the service to the Azure.

Select the project and using the context menu click in Publish, as following

azure mobile service

Then connect to Azure Mobile Service, using the Azure Account, and select the MyAMSDev as following

azure mobile service

In Settings, set the configuration and the file publish options (only!)

azure mobile service

And then publish it!

Do the same process to Prod, as following

azure mobile service

And then publish it!

Is possible to see the Web Publish Activity that show the publish process

azure mobile service

At this moment, both environment are deployed and running and is not required to define or change each environment in future, only is needed to have attention each one is set before deploy.

Notes

    1. In this case was use the MS_TableConnectionString1 name for define the connection string and the default value MS_TableConnectionString, defined in Azure Portal, will be ignored.

    A developer can to set different connect string or even change the name, but needs to be defined with the same name in all environments.

    1. With the Visual Studio 2013 – update 4 is possible to change the database in Settings separator but in this case it will be ignored!

For more information about the transform file, read the following article

How to: Transform Web.config When Deploying a Web Application Project

Conclusion

In conclusion, creating different environments for a .Net Backend – Azure Mobile Service is an easy task that is a plus in the development, which allow to test new features before it go to production and it allow to make sure the tests done in localhost will keep working in the Azure Mobile Service without to affect the production environment.

 

Class diagram: a easy way to understand code

Scope

This article has the goal to show how to create a class diagram and how we can use it to understand code.

Introduction

According to MSDN, a “Class diagrams help you understand the class structure of projects others have written (or that you wrote a long time ago). You can use them to customize, share and present project information with others.”

In this article we will use the sample Field Engineer from MSDN Samples which is an Azure Mobile Service sample, but where is not required to know about Azure Mobile Service and we will not talk about it. We only focus in the model classes as a sample.

Description

After open the Field Engineer Sample in Visual Studio, we will find a folder with the name “Model”, here are an image with it

model folderIn this case we do not have so many classes, that can be great because can be easy to understand the model. Maybe a lot of developers will start open all files to see the code, but why not create a class diagram?

Let’s create first a class diagram for see an overview about the model.

Creating a class diagram

Selecting the Model folder and opening the context menu, click in “Add” and then “Class Diagram” as following
add class diagram

then define the name of the class diagram as following

class diagram name
clicking in “OK” the diagram file will be added to the project and will be opened, as following
class diagram in projectnow we can select all files in model folder and drag and drop it to the class designer area and the result will be
drag and drop result

and now we should move each shape and should expand each one for see details.

Expanding classes

For expand click in the arrow in top, as following
expand shape

and the result will be
shape expanded

The class details

If we open the context menu under the shape we can select the “Class Details”
select class details

the result will be something like it
class details

With this user interface is possible to see more details about the class and is possible to create/edit/delete/hide each property, method, event… without coding.

Understanding different icons

After expand all shapes we will find that some shapes show the Fields, Properties, and Methods (and can show events, nested classes…).

Let’s see the case of the MobileServiceContext

MobileServiceContext shape

Looking to the image we can conclude

  • The connectionStringName is private and is a const field;
  • All properties are public;
  • The method MobileServiceContext that is the constructor is public and the method OnModelCreating in private;

How is possible to conclude it?

Each icon means something, for example the symbol * in each item means it is private. The best way to understand the differences between each icon is to create a sample with each possible case.

Let’s see a fake class created for this purpose

class details
When we select the shape and open the context menu we have options that allow to show/hide different details.

Let’s see the case for show the base class.

Show Base Class

base class

After it and after expand the shape for MyBaseClass we will have something like it

diagram

In this moment, there are one point that can be important to defined, to show the diagram what is the most important information that should show in the diagram?

It depend what each developer likes or wants to see, but in general we can defined as a pattern, we should show only public properties, methods and events and hide all others (in some cases can be useful to show all defined as internal or protected).

Hide compartment

Selecting the shape and the item we want to hide is possible using the context menu do this, as following

hide itemsThe option “Show all members” will revert it, but in some cases is better to use the Class Details for revert it, because we can have others items hidden that we want to keep hidden.

Each shape that hidden some item will show an icon that means it, here are an example

hide symbol

Apply associations

Returning to the Field Engineer Sample and choosing the class JobHistory, Job and Customer we will find that each one has a relationship with each others, let’s highlight it using a  red and orange rectangle, as following

related classes

Let’s see how we can show the association for properties and for collection.

Show as Association

For properties which the data type is another class we can select the property in shape and in context menu should select the option “Show as Association” as following

show as associationShow as a Collection Association
and for properties that are collections/lists we can choose the option “Show as Collection Association” as following

show as collection association

and the result will be something like

associations

The number of arrows will define it is a property or a collections, but using a good name can be enough to see the differences.

Show as property

By selecting in the arrow is possible to revert it, using the option “Show as property”.

Show as property

The final class diagram

After define all arrows for all association and organizing the shapes we will have something as following

final diagram

For this diagram, we can understand that the model has five classes and the MobileServiceContext is defined by these classes. Is clear the relationship between the classes which allow to conclude (for example)

  •  A job know its job histories and each job history refers to a job;
  • A job knows which customer is associated and a customer can have more than one job;

A similar conclusion can be done for Equipment and Equipment Specifications.

Note: is possible to create arrows for the MobileServiceContext’s properties but it only will do some noise and is not important in this case to see these associations.

Each developer should customize the class diagram and there are no fixed rules for it. With this kind of diagram is very simple to understand the model and in some cases found mistakes like wrong names, wrong relationships or even missed features.

Conclusion

In conclusion, we can conclude that the class diagram is a very useful for understand the code made by us or by others, the relationship between the classes and an easy way to find mistakes. In some cases, with class diagram is possible to have a big picture about the application without see one line of code.

 

See also

Resources for client apps that uses the Azure Mobile Services

In the last months, I have been working with Azure Mobile Services, where my focus was  create .Net BackEnd for IOS and Android Developers. For different reason I searched for the right article for each subject or problem and saved it as a curation for share and use later.

Here are the resources focused in each platform

Note: in some cases is need to select if we want a .Net or a JS BackEnd.

and is possible to use the start point Azure Mobile Services for Client Apps which contains all references defined above.

 

Enjoy it! :)

 

Curah! 2014 award – TOP 10 Featured Curators

Last week, was published the article

2014 Curah! Awards + the Week of Curah! Awards (1 Year Celebration!)

And couldn´t be more happy, because I won the Curah! 2014 – TOP 10 Featured Curators badge which represent the award for the curators with most featured curations ( in this case the top 10!).

Here is my new badge
badge