13 July, 2015

Hidden merging of the entities collections

Merge two collection of the entities?

Sometimes in our applications we have problems with the performance of the operations, that are performed on the collections of the entities. Usually the problem is caused by a lot of joins between tables. If we don't want to build big, time consuming queries that return a lot of data, we can hit database a few more times and join the returned collections.

In this case, when we can get some objects in one hit (using Entity Framework) and after that, take the children (objects connected with the navigation properties) using another query. After that we can merge both collections by setting the correct values of the navigation properties in any of those two collections.

Faster solution

However, if we do both of operations using one ObjectContext in one scope, we do not need to set the navigation properties manually. The Entity Framework will do this for us automatically.

In the example below we pass the collection of the entities without connected navigation properties to the method, then we get the id's (or any other identification fields from the entities) and we take the data from the included table with full product data. In this case we do not need to merge the collections. As a result of this method we will receive the collection of ProductSets with attached products with all the full data included.

Example

        public static void AttachDespatchItemsToDespatchPackages(List<ProductSet> listToWhichWeWantToAttachObjects, 
               ObjectContext ctx)
        {
            List<int> productsSetsIds = listToWhichWeWantToAttachObjects
                .SelectMany(d => d.ProductSets.Select(dp => dp.ProductSetId))
                .Distinct()
                .ToList();
            List<Product> productsForProductSet = new List<Product>();
            if (productsSetsIds.Any())
            {
                productsForProductSet = ctx.Products
                    .Include("FullProductData")
                    .Where(p => productsSetsIds.Contains(p.ProductSetId))
                    .ToList();
            }
        }

31 May, 2015

Get rid of the duplicated entities that represent the view

The main problem

Lately I was working on a problem, that was really surprising, before I understood it. I was querying the database to get some data using the Entity Framework 4. The entity was a representation of the view, which was joining a few tables. One of the information from the view was the sum of the quantities of the given item in all of the shops.

The view, that I used was more less like this one:

CREATE VIEW [dbo].[TransferItem]
AS
SELECT 
   IT.[ItemId] /*Part of PK*/
  ,IT.[ItemCode] /*Part of PK*/
  ,IT.[Name]
  ,IT.[Barcode]
  ,IT.[Description]
  ,SI.Quantity
  ,SI.ShopId /*This is not a part of PK!*/
FROM (Item IT 
LEFT JOIN ShopInventory SI ON SI.ItemId = IT.ItemId)

Table "Item" contains a lot of different information about the products and the ShopInventory contains the information about the quantities of this product in the different shops. It is important, that in the ShopInventory, there were many rows with the same product and different quantities, each for the different shop, however the ShopId was not a foreign key. For some purposes I needed to use only some of the data from both tables and it was a reason to create the view.

I have created the DTO class called Inventory which was similar to this one:

public class Inventory
    {
        public string ItemCode { get; set; }
        public string Quantity { get; set; }
        ...

        public Inventory(string itemCode, string quantity)
        {
            ItemCode = itemCode;
            Quantity= quantity;
        }
    }

Finally, in my code, I made a query, which was taking the grouped codes of products with a sum of the quantities of the given product in all shops. It looked like this

var products = transferItems.Where(ti => ti != null)
                .GroupBy(i => i.ItemId)
                .Select(g => new Inventory(
                    itemCode: g.First().ItemCode,
                    onHand: g.Sum(s => s.Quantity).ToString())
                ).ToList();

I was sure, that it should work, but unfortunately, when I run the unit test, I've found out that something was terribly wrong: for each product code I received the quantity, which was a multiplication of the number of shops in which the product was and the quantity of it in the first, found a shop.

Example: Item A was in a shop S1 (5 items), shop S2 (3 items) and shop S3 (9 items) so as a result of the query I should have had received 17 items, but I have received 15 items.

Firstly, it was amazing, but then I've found out, that the Entity Framework sees all three products as exactly the same entity because the primary key came only from the "Item" table and the ShopId was not a part of the PK. In this case EF did not look on the ShopId as a key so each row with the same ItemId was also the same entity, which caused a confusion of the EF and the described result of the query.

The solution

As I couldn't replace the ShopId with the primary key of the ShopInventory table, I had to add another unique column. In this case the easiest thing was to add a new column which looked like this:

ROW_NUMBER() OVER(ORDER BY IT.[ItemId] ASC) AS RowNo

This new column became a part of the view entity PK and the rows became unique. The view looked like below:

CREATE VIEW [dbo].[TransferItem]
AS
SELECT 
   ROW_NUMBER() OVER(ORDER BY IT.[ItemId] ASC) AS RowNo
  ,IT.[ItemId]
  ,IT.[ItemCode]
  ,IT.[Name]
  ,IT.[Barcode]
  ,IT.[Description]
  ,SI.Quantity
  ,SI.ShopId /*This is not a FK!*/
FROM (Item IT 
LEFT JOIN ShopInventory SI ON SI.ItemId = IT.ItemId)

And that's all. Using this simple trick will give you a fake PK from the DB perspective, but the rows in the entities collection will be unique.

10 May, 2015

How to set Bootstrap menu active class using ASP.NET MVC ?

Bootstrap framework is currently the most popular framework (or rather a set of themes and controls) which makes creating of the responsive websites very easy. Unfortunately, some effects on the website are not easy to reach in caseyou want to create a ASP.NET MVC web application using the partial views. One of them is setting of the specific style on the Bootstrap navbar "li" active element.

You can obviously achieve the desired style on the active "li" element of the navbar by using a Javascript function which would check what page was loaded by the address (or e.g. the content) and set the class on the element, but it is complicated and not very elegant solution.

The best way, to achieve the mentioned effect is to use the extended MenuLink method on the ASP.NET backend of the application.

Changes on the UI

Firstly, we need to use a MenuLink in the Bootstrap menu. so it would look like more-less like this:

 

In the example I have replaced ActionLinks with Menu links. Without backend changes it won't give us a result, but now we will add an Extension method o the backend.

Changes on the backend

Lets create a new class, which will hold all of the helpers, which may be used on the UI directly. In case of our problem, we will an extension method for the MVC MenuLink. The code of the method is below.

using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

public static class HtmlHelpers
{
    //Extensibl method MenuLink
    public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
    {
        //Create a tag, that will be an action link inside a "li" element
        TagBuilder builder = new TagBuilder("li")
        {
            InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
        };
        //Get current routes from the context of the view.
        RouteData route = htmlHelper.ViewContext.RouteData;
        //Gets the controller and the action from the route data
        string action = route.GetRequiredString("action");
        string controller = route.GetRequiredString("controller");
        //If the current controller and action are the same as the element on the navbar menu,
        //then we need to add the class "active" on the tag.
        if (controllerName == controller && actionName == action)
        {
            builder.AddCssClass("active");
        }
        return new MvcHtmlString(builder.ToString());
    }
}

This simple and elegant solution will make application niecer and the management will be much easier. You won't need to touch the mechanism of setting of the Bootstrap active classes any more.

17 April, 2015

How to generate a WCF service from the WSDL file?

Why we need to generate WCF service using WSDL?

Usually we create our own webservices, but sometimes we need to create a mock of the webservice from which WSDL we have obtained. This situation may happen if we have to create a client of the third-party service to which the access is restricted. In that case it is easier to create our own service, than use the real one. It is especially useful when we shoot the data to the service and we do not really need the response except possible exceptions.

How to do it?

Firstly we need to have w WSDL file which will be a base for our mock. Then we need to generate the interface, which will be a contract in our new service. To do this, we need to use small application called wsdl.exe. It is a part of the Windows SDK and it can be found e.g. here: C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools>wsdl.exe. To generate the interface in C#, you should use the following command:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools>wsdl.exe "C:\WsdlToGenerateInterfaceFrom.wsdl" /l:CS /serverInterface

After execution of this command, the interface will be generated in a folder, where the wsdl.exe is placed. Sometimes there may be some warnings like:

Warning: This web reference does not conform to WS-I Basic Profile v1.1.
R2706: A wsdl:binding in a DESCRIPTION MUST use the value of "literal" for the use attribute in all soapbind:body, soapbind:fault, soapbind:header and soapbind: headerfault elements.

Hopefully, usually the warnings are not danger. Now you can create new VS WCF library (service) project and implement the interface (your contract).

After implementing of the interface it is good to run the web service and look into the WSDL file generated by it. Unfortunately sometimes, some of the types may be missing. It happens due to the fact, that there are some attributes added automatically to the generated classes. We need to be aware that some of them may be removed or sometimes, we need to add some attributes (like DataContract or OperationContract)

Finally we can use our webservice as a mock for other applications. This is very useful for quick tests of some behaviors.

25 March, 2015

"HasValue vs null-check" and "Boxing vs Value property"

A few days ago I took part in a discussion about the differences in performance of the popular expressions, that every developer uses almost every day in the code. The subjects of the discussion were the difference between: null checking versus the HasValue property and boxing cast versus Value property. I have never checked what are the differences between them on my own. I heard a lot of gossips about those expressions and so I have decided to look into the structure of the compiled code. To test the compiled code I have used "IL Disassembler" - the tool, which is included in the Visual Studio package. Here are the results of my investigation.

Boxing vs Value property

Firstly, I created a simple console application, which assigned nullabale integer to two different variables. In the first case, the value was boxed and in the second one, I used the Value property. The code of the application is below:

namespace NullableTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int? testInt = 0;
            int castValue = (int) testInt;
            int valueProperty = testInt.Value;
        }
    }
}

Next, I have started IL Disassembler and looked into the application assembly. The result was a little bit surprising:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       25 (0x19)
  .maxstack  2
  .locals init ([0] valuetype [mscorlib]System.Nullable`1 testInt)
  IL_0000:  ldloca.s   testInt
  IL_0002:  ldc.i4.0
  IL_0003:  call       instance void valuetype [mscorlib]System.Nullable`1::.ctor(!0)
  IL_0008:  ldloca.s   testInt
  IL_000a:  call       instance !0 valuetype [mscorlib]System.Nullable`1::get_Value()
  IL_000f:  pop
  IL_0010:  ldloca.s   testInt
  IL_0012:  call       instance !0 valuetype [mscorlib]System.Nullable`1::get_Value()
  IL_0017:  pop
  IL_0018:  ret
} // end of method Program::Main

It occurred, that both constructions are replaced by the body of the Value() property. It means that the performance is exactly the same. There are no differences in the execution time of both structures. You can choose the one you prefer.

HasValue vs null-check

As a next step, I have created another console application, which was checking if the nullable variable is a null. I made it in two different ways: by using the classic null-check and then by using the HasValue property. Here is the code of the application:

namespace NullableTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int? testInt = 0;
            if (testInt != null)
            {
                //Do something ...
            }
            if (testInt.HasValue)
            {
                //Do something else ...
            }
        }
    }
}
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       25 (0x19)
  .maxstack  2
  .locals init ([0] valuetype [mscorlib]System.Nullable`1 testInt)
  IL_0000:  ldloca.s   testInt
  IL_0002:  ldc.i4.0
  IL_0003:  call       instance void valuetype [mscorlib]System.Nullable`1::.ctor(!0)
  IL_0008:  ldloca.s   testInt
  IL_000a:  call       instance bool valuetype [mscorlib]System.Nullable`1::get_HasValue()
  IL_000f:  pop
  IL_0010:  ldloca.s   testInt
  IL_0012:  call       instance bool valuetype [mscorlib]System.Nullable`1::get_HasValue()
  IL_0017:  pop
  IL_0018:  ret
} // end of method Program::Main

And another surprise! Both constructions are replaced by the body of the HasValue property. Once again it means that the performance is exactly the same and you can choose the one you prefer.

Honestly, I was surprised by the results of the investigation and I can say only one thing: if we are not sure how the code will look after compilation, let's check it using disassebler. After that we will be able to tell exactly what code is a real result of our work and there won't be any discussions about performance... at least in those two cases :)

08 March, 2015

How to get database column property for entity?

Why getting the column property for the entity is a problem?

When we use Entity Framework, we often want to get some information about some columns, that are a part of the database table, which is behind the entity. The most elegant way of getting this information is to use a partial class which contains the decorated (with attributes) properties. It may look like below:

[Column("Description")]
[Required(ErrorMessage = "Description is mandatory!")]
[StringLength(255, MinimumLength = 5, ErrorMessage = "The description must contain more than 5 and less then 255 characters!")]
public string Description{ get; set; }

This solution is the most elegant and easiest to use. You can use e.g. the messages in the higher layers of your application and use the lenght in validation of the objects also on the higher layers of the application. Unfortunatly, it is hard to use this mechanism in the application that has a big database and you have very limited time to make a change.

Quick solution

If you have the problem as stated above, you can use a little bit different solution - get access to the properties by the Reflection. The main idea is to access the properties of the entity by its names and types represented as strings like below:

        public static object GetInfoAboutColumn<TypeOfEntity>(ObjectContext objectContext, 
                              Expression<Func<TypeOfEntity, string>> column, 
                              string typeName, 
                              string propertyName)
        {
            object resultValue = null;
            Type entType = typeof(TypeOfEntity); //we need to know the type of the entity, so we know what we should look for
            string columnName = ((MemberExpression)column.Body).Member.Name; //Get the name of the column (field) in entity
            if (objectContext != null)
            {
               // Get collection of items from the context.
               ReadOnlyCollection<GlobalItem> globalItems = objectContext.MetadataWorkspace.GetItems(DataSpace.CSpace);
               if (globalItems != null)
               {
                 // Get properties of the given type and name from the given entity.
                 var allPropertiesOfType = 
                                 GetAllPropertiesOfType<TypeOfEntity>(typeName, globalItems, columnName, entType);
                 IEnumerable<object> propertyResults = 
                                 allPropertiesOfType.Select(sel => sel.TypeUsage.Facets[propertyName].Value).ToList();
                 if (propertyResults.Any())
                 {
                     resultValue = propertyResults.First(); // Get the value which we were looking for.
                 }
               }
            }
            return resultValue;
        }

The most interesting of this solution is the mechanism of looking for the value, that we are interested in the collection of the entities. It can be done by the LINQ query as below:


        private static IEnumerable<EdmProperty> GetAllPropertiesOfType<TypeOfEntity>(string typeName, 
                                                     ReadOnlyCollection<GlobalItem> globalItems, 
                                                     string columnName, 
                                                     Type entType)
        {
            IEnumerable<EdmProperty> allPropertiesOfType = globalItems
                .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                .SelectMany(meta => ((EntityType) meta)
                    .Properties
                    .Where(p => p.Name == columnName
                                && p.TypeUsage.EdmType.Name == typeName
                                && p.DeclaringType.Name == entType.Name));
            return allPropertiesOfType;
        }

You can test it for example by trying to get "MaxLength" property of any entity. Cheers!

10 February, 2015

How to use params in C# ?

Why it is sometimes to use params?

Imagine, that you have a lot of code, that is complicated, hard to maintain and you have a small amount of time to make a change in it. For example, you need to change a parameter (e.g. width, height, color etc.) of a controls with a specific tag. Those controls may be placed on many different screens, but unfortunatly they can be sometimes removed from the screen.

To solve this problem you can use a list of controls (remember to check if it is not a NULL!) or you can use params.

Using of the keyword params gives a possibility to pass to the method a comma-separated set of arguments of one type or an array of arguments of one type. The great thing about params is that passing the argument is not mandatory and it is recognized as an empty collection and not as a null. It is useful especially if we are not sure if there are elements and we do not want (or can't for some reasons) to make null-checks.

Example

Lets take a look on the example. We have a simple WPF application with some controls and a button with a handler.


    
        

Thehere is also a method, that changes the width of the controls, that are marked with the specific Tag.

 private static void ChangeWidth(params Control[] allControls)
        {
            foreach (Control ctcw in allControls)
            {
                if (ctcw.Tag != null && ctcw.Tag.ToString() == "ToChangeWidth")
                {
                    ctcw.Width = 100;
                }
            }
        }

As you see, the controls array is preceded by the keyword params. Now in a handler we can test how the params works.

 public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 1 - No change of the controls width, but also no error.
            ChangeWidth();

            // 2 - //All controls with tag "ToChangeWidth" will have new width
            Control[] allControls = mainGrid.Children.OfType().ToArray();
            ChangeWidth(allControls);

            // 3 - //Throws exception !
            ChangeWidth(null); 
        }

The great thing is that in first two cases the solution will work. Remember, that passing a null value to the method will work as usual, so in the foreach loop it will throw an error. Only NOT passing any parameter will (in fact) pass empty array to the method.

Why is it worth using?

  • It is faster than a List, because in fact, the compiler creates an array of objects of the given type instead of a List.
  • It is useful if you need to pass unknown number of the parameters.
  • You can easy read the code and understand it, because there is less code to write.
  • In some cases you do not need to write the null-checks (but they are usually a safe solution!).

02 February, 2015

New Technology Radar

This time something really short: new Technology radar was released a few days ago.

For me, as a .NET and web developer, I will:

  • Learn more about AngularJS. To be honest, I have never made anything "for real" in this framework and I think that the time has come. AngularJS is now a mature framework with big community and it offers a lot features.
  • Read more about REST without PUT. How can we replace it? Only by POST? The idea is interesting and seems to be more and more popular. I think we need to used to REST without PUT...
  • Definitly test GruntJS. It has a strong community and a lot of great plugins. I heard a about 1,5 h year ago and this year finally I will give him a chance.
  • Try the EventStore database. It is a very interesting concept of the database, that stores the data as the immutable events. It is not for every purpose, but it is free, fast and, for sure, worth testing.
  • Take a closer look on Roslyn. It is an open source .NET compiler, which might be very useful in terms of the refactoring and optimization.

... and I will take a closer look on Google Go language.

27 January, 2015

How to make an API?

Background...

For a few years I worked with different APIs delivered by the different vendors. I was also taking part in designing and creating of the APIs for the software that I was working on. In this post I would like to describe some key points, which in my opinion a good API should have and some that shouldn't have.

Subjective list of the well designed API features

  • Extensibility

    Remember, to make your API extensible. It is more than possible, that you will need to add some new features as a client request and it is better to do this as fast as possible, especially if your system and the API are new on the market.

  • Batches

    Your API should offer a possibility of using batch operations. E.g. Taking details of an object can be done either for a list of items, or only for one of them. It will decrease the number of hits to the server and increase performance.

  • Technologies easy-to-use

    Try to avoid old, complicated technologies. If you can give an access to your system using the REST service, then use it instead of the old web service. If you are designing a desktop application API, then don't use old, awful COM objects, when you have many different ways of accessing the data.

  • Return everything as a result

    This point may be controversial and I am sure that not everybody will agree with me. I think, that the best way to return any data is to return it as a defined result type. The worst thing you can do, is to throw an exception every time something unusual is happening, instead of giving the user a possibility to decide, what he wants to do. Remember, that this situation may be unusual, but it might not be an error in your clients system.

  • Meaningful error messages

    If an error appears, do not return only the code of the error. I know it is the easiest way, but not the best one. Nobody likes receiving only the number instead of the message.

  • Meaningful names of types and methods

    Names of the methods and types should be simple and meaningful. If you are not sure, if the name is simple enough, it means that it is probably too complicated and if you are not sure, if it is meaningful- ask someone who is not working on this project, what in hers / his opinion this type or method is created for.

  • Well documented

    I know, that it is not possible every time to make a good API documentation, but if you have some time try to make some documentation. It would be a great help for the users of your API and more people will be willing to try it.

  • Supported by the community

    If you have a support, it is much easier to use any API so think about making a forum, Facebook fanpage or any other place, where users of your API can exchange their experiences, ideas and ask questions. Remember, that many interesting projects failed because of the lack of community support!

The most common problems

  • Old technologies

    Sometimes developers can design a useful API, but finally they are trying to rebuild the old version of API using old technologies. If it is possible, try to make a new API and show to your clients, why it is better to use the new one. Remember- do not to give access to the old API to new clients- it causes problems.

  • No support

    API must have a support: either paid or community (free) - this is obligatory. If there is no support, then nobody will even try your API.

  • Not enough tests

    Always give your clients well tested product. If API contains errors, then it is not reliable so nobody will trust you and your API.

Things to remember

  • Listen to other developers

    Even if your API is the best in the whole galaxy, maybe it is worth to hear other developers opinion. They sometimes may have good ideas, which may help you in creating really great API :)

  • Listen to your clients

    Client is a boss. Don't forget about it. If he is saying that something might be better or easier to use - think about it and try to understand clients' needs.

10 January, 2015

How to replace "switch" with the polymorphism?

Problem

A few days ago we were discussing the coding standards, that we are going to adhere. One of the points was preventing from creating of the very big conditional statements.

Lets take the "switch" statement into consideration. It is nothing wrong in using this statement until it does one thing, but as we all know, "switch" usually makes a lot of things and it is much bigger, then it should be. If we want to adhere SOLID principles, we cannot write huge, multi-actions conditional statements.

In this post I want to show you one of the most popular methods of replacing of the "switch" with the polymorphism. Lets see how to do this?

Solution

public class DocumentsHandler
{
    public void HandleDocument(Document document)
    {
        //...
        List importedDocumentElementsIds = GetImportedDocumentElementsIds(clientName, document);
        //...
    }

    public List GetImportedDocumentElementsIds(string clientName, object document)
    {
        /* Somewhere in the code we have:
         * - entity context
         * - name of the client
         * 
         * Some operations ...
         * 
         * */
        switch typeof(document)
        {
            case Sales :
                return BaseDocumentElementsImport(clientName, context.GetSalesDocumentsElements())
            case Purchase :
                return BaseDocumentElementsImport(clientName, context.GetPurchaseDocumentsElements())
            case Custom :
                return BaseDocumentElementsImport(clientName, context.GetCustomDocumentsElements())
            // other document types...
            case default :
                return BaseDocumentElementsImport(clientName, context.GetOtherDocumentsElements())
        }
    }
}

Now we will replace an old Document with a new one. It will be an abstract class with an template method, which we are going to override.

public abstract class Document
{
    public abstract List ImportDocumentElements(string clientName)
}

In the next step we create a set of derived classes which will contain overriden template method.

    public class SalesDocument : Document
    {
        //...
          public override List ImportDocumentElements(string clientName) 
          {
            return BaseDocumentElementsImport(clientName, context.GetSalesDocumentsElements());
          } 
        //...
    }

    public class PurchaseDocument : Document
    {
        //...
          public override List ImportDocumentElements(string clientName) 
          {
            return BaseDocumentElementsImport(clientName, context.GetPurchaseDocumentsElements());
          } 
        //...
    }

    public class CustomDocument : Document
    {
        //...
          public override List ImportDocumentElements(string clientName) 
          {
            return BaseDocumentElementsImport(clientName, context.GetCustomDocumentsElements());
          } 
        //...
    }

    public class OtherDocument : Document
    {
        //...
          public override List ImportDocumentElements(string clientName) 
          {
            return BaseDocumentElementsImport(clientName, context.GetOtherDocumentsElements());
          } 
        //...
    }

Now in the code we can write simple, short line of code like below:

List importedDocumentElementsIds = document.ImportDocumentElements(clientName));

Benefits

It seems that the code is now more complicated and we wrote more LOCs, so why did we do this?

  • We do not duplicate the code because we do not have identical conditions.
  • Usage is simpler and more elegant, because we write smaller numer of LOCs.
  • Object knows everything about itself. We do not need to pass the state the the object under some conditions. We only need to tell the object to perform the operation and it will decide how to do this.
  • Now our code is extensible. We can add new class with new implementation of the overriden method instead of adding new case to the "switch". To be honest, "switch" conditions like to grow very quickly.