Search This Blog

Sunday, August 30, 2015

Extension Method to Flatten Entity Framework Validations Errors - providing Entity, Property and Error message

    public static class ExtensionMethods
    {
        public static List FlattenErrors(this DbContext dbContext)
        {
            return dbContext
                .GetValidationErrors()
                .SelectMany(e =>
                    e.ValidationErrors
                    .Select(ve =>
                        string.Format("Entity: [{0}], Property: [{1}], Error: [{2}].", ObjectContext.GetObjectType(e.Entry.Entity.GetType()).Name, ve.PropertyName, ve.ErrorMessage)
                    ))
                .ToList();
        }
    }

Wednesday, August 26, 2015

Ease debugging by serializing your objects

When you have a bunch of types containing an additional bunch of fields, it can be useful to be able to see all the values of a given object all at once, while debugging. Something that looks like:


In other words, I would like my types to be able to display all their properties in a stringified fashion (much like an object serialization). To accomplish this, I thought I could create a base class that overrides the ToString() method, and have all those classes inheriting from it - and sharing this same ToString() implementation. The same can be accomplished by adding an extension method to the object type, but the drawback would be I would have to cope with such method popping-up every time when intellisensing every object...

The snippet below illustrates how the discussed above can be achieved. The following will help only with primitive types. If your type has lists, for instance, you will need to add logic in how to treat it.

internal class Program
{
    private static void Main(string[] args)
    {
        public B()
        {
            b1 = "hello";
            b2 = "world";
        }
        B b = new B();
        Console.WriteLine(b.ToString());
        //prints hello,world
    }
}

public class A
{
    public override string ToString()
    {
        List<PropertyInfo> p = this.GetType().GetProperties().ToList();
        return string.Format(string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, p.Select(z => z.GetValue(this))));
    }
}

public class B : A
{
    public string b1 { get; set; }
    public string b2 { get; set; }
}

Thursday, August 20, 2015

Difference between filtering joins in the ON and Where clauses

Recollecting what I learned about the topic:

For Inner Joins with filters in the ON or Where clauses, the result is the same - but filtering within the ON clause restricts the row set that will be input to the next Sql engine step (where filter, here), making it more performance-wise.

The results differ in outer joins - where you filter the table you are outer joining in the ON clause.
I found a good and concise explanation here.

The great thing about this article is that it explores an example of filtering the table that is to be outer joined (i.e., all its records should be returned).

This, together with the piece of information that outer joins are logically executed after inners, means that whenever you filter within the ON clause, in such an outer join, your filter is ultimately ignored/discarded, because the outer joining will occur afterwards anyway, "bringing back" all the outer joining table records to the result set. And with no Where clause left to filter, this outer join with no where clause shows more records than the one with a where clause.

Thursday, August 13, 2015

Sql Server Management Studio T-SQL Free Formatter Plugin

Poor Man's T-SQL Formatter Plug-in

If the link gets broken, google for it.
Install the msi file and restart your SSMS at the end of installation. And that's all.
The options singled out below will be added under "Tools".


Thursday, August 6, 2015

Indispensable Developer Tools

I will keep updating this post every time I come across something new...

For the time being, here is what I know of/use:


I managed to lose the excel file where I had this typed in.
For the time being, am adding my last findings:


  • UltraSearch (file search)
  • SOAP UI
  • Instant EyeDropper (extract color code from any pixel in the screen)