Adding Entity Framework


In this post, we will talking about some improvements to do in our application. We will introduce E.F. That don’t have nothing to see with MVP pattern but we will use the Project of the previous post about MVP. In any case, you can apply this improvements in other projects.

The first step is create the database. In our case a book store database.

1

After create the database, we need to create a ADO.NET Entity Data Model

2

We follow the instructions and we can select the objects to import (Table, Views, procedures, …). In our case we are going to select the tables (we can add, remove, update later).

3

In our Diagram, and if we don’t have the foreign keys in the database or we want to add additional associations in the model, we can do it through the Diagram. To do it, we need to select one entity > Add new > Association.

4

After that, we select the relation (the line) and we need to specify the fields of the relation.

5

After finish the associations, our model should be something like that:

6

Next step is separate our entities. By default, the entities are been created in the same folder and Project than our model. This not respect the X-Layer design, so we should have the entities in another Project. Do that is very easy from versión 5.X of Entity framework. We only need to move the “.tt” file to the Entities Project.

7

When we have the file in the other Project, we need to change the file to put where are located the model in the the inputFile variable.

8

If all Works fine, we can see that our classes are been created in this new Project. Now we have all separated, so we can continue. We need to take into account that if we modify something from our Model, we need to come to this file and make some modification (or only save again) to reload the changes in this file.

We have created a new class to connect to E.F. and retrieve data

9

Now we only need to change our presenter to call this class instead the previous one and change the data source. (We have removed the old Book entity and replace by the new one of E.F.). Probably we are going to get an error trying to connect, due to the local database (“Error finding books. No connection string named ‘BookStoreEntities’ could be found in the application config file.”). We need to add the connection to the Project config file (we can see it in the Model Project) and add the EntityFramework library (EntityFramework.SqlServer.dll).

We can see that now, the result are retrieved from the database.

10

Oracle UDT


Have you ever been passed a list of parameters to Oracle Procedure? Some people use arrays to pass list of parameters to the procedures but Oracle has a more powerful option to do this, the UDT types (User Define Types). In this post we are to see a basic example to do this.

Currently, if you want to do this, the first step is to create an array as IN parameters, create a table with the same type that the elements into the array, fill this table and use it in the query. In .net you need to pass the array as value of the OracleParameter:

create or replace TYPE LIST_ITEM_STRING IS TABLE OF VARCHAR2(20);

1

2

There are another way to do this more directly. We will use the UDT types. In this case we can use the table directly instead use an array and fill the table in the procedure.

3

In .net part we need to assign the type used for the table, in our case the type defined above (HR.LIST_ITEM_STRING). Here the type would be Object and the value a “TableTemplate” of string.

4

We need to define the UDT type too:

5

OracleParameterBinding: We need to do the mapping between Oracle and .Net (read and write). This can be a little dificult to implement, in fact, a lot of documentation that i have read about that is a hell. We have created this library to do it more easy. As you can see your value only need to be an array of TableTemplate and that’s it. Easy, right? Feel free to use the library in your projects if you want, the library is included in the source code bellow. In addition, you can contribute with our paypal account ;).

You can download the code here.

Code Analysis with SonarQube and C#


Sometimes, and especially when our application is huge or there are a lot of people working on it, maybe is usefull take a global vision of the state of the source code, view the possible improvements, avoid possible future issues or be aligned with the design guidelines.

There are a lot of programs to manage this, even Visual Studio has his own Code Analysis (in my opinion is very poor). For me, one of the best is SonarQube, that have a web interface and is easy to track, view, get reports, etc. In this post we will configure and run SonarQube with C# for our project.

 

The first step is download SonarQube from his page (http://www.sonarqube.org/downloads/). We unzip the files in a folder. We need to take into account that SonarQube not needs install it, the files that it needs for run are these files unzipped.

1

 

The next step is go to this folders and find our distribution inside bin folder, in my case “windows-x86-32“, and execute StartSonar.bat to configure apache and SonarQube. Probably you can get an  error if you don’t have JRE installed. You can download it from here. After that you can navigate to http://localhost:9000 to display the home page of SonarQube.

2

You can change some parameters from a config file inside installation folder (conf/sonar.properties) to manage the port, connection pool, proxy, etc., but we can use the default parameters). Ok, now we need to install the plugin for run C# or VB.Net projects. We make login in the application (admin/admin) and we go to Update Center menu (on the left). Here we can see the installed o available plugins.

3

We install the C# and .Net plugins. After that we need to restart the SonarQube (we need to close and launch again StartSonar.bat). We are finishing our configuration, we only need the last step, get a runner, in this case we will use Sonar-Runner (we can use others). We can download it from here. As SonarQube it no need to install it, is enough with download the files. Now we have all ready to work. To avoid go to the folders each time, we can register the path in “Windows path” to recognize the program names (Computer/Properties/Advanced system settings/Environment Variables). We search the Path variable and is needed to add the folder that contains StartSonar and the runner (D:\sonarqube\sonar-runner-2.3\bin\;D:\sonarqube\bin\windows-x86-32\; in my case). With that we can start SonarQube or Runner only typing StartSonar or sonnar-runner in a cmd window.

We need to go to our solution project and create a file called sonar-project.properties. In this file we need to configure the project Name, path and the language. We need to place this file in the same place that the solution. This is the common content of the file:

sonar.projectKey=KeyOfTheProject
sonar.projectName=NameOfTheProject
sonar.projectVersion=1.0

sonar.sources=.
sonar.language=cs
sonar.sourceEncoding=UTF8

It’s time to analyze our project. We need to open a cmd screen and go to our solution folder. After navigate to the folder we type “sonar-runner” and the analysis starts. If you have an error about FxCop, you need to go to the SonarQube Portal, go to General Settings and in .Net FxCop we need specify the path of FxCop.

6

 

Run again the sonar-runner and this time should be work. When the analisys ends, we can see in SonarQube portal a new link on the left with our project and the results:

7

 

You can detail each kind of issue, see graphics, summary, evolution, etc. The ideal is don’t have any Evidence or the less possible.

8