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();
}
}
Keeping track of interesting places I put my feet on in the software development world.
Search This Blog
Sunday, August 30, 2015
Extension Method to Flatten Entity Framework Validations Errors - providing Entity, Property and Error message
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.
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.
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".
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:
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)
Tuesday, July 21, 2015
Valuable Tools When Working With Regular Expressions
Update: This tool works well for testing Javascript's Regular Expression implementation.
Check here for a list of tools for other implementations.
I am posting here links to some highly valuable tools I found for when you are working with regular expressions:
One is a playground/tester where you can literally play with regular expressions, in an extremely user-friendly manner.
The other is a tool for generating string samples based on a regular expression input. This is very handy when need to deal with all sorts of complex regular expressions you did not create, and you are trying to test if they are working or not. (Ex: when testing .xsd schema validations).
The playground/tester is located at http://www.regexr.com.
Features I like the most:
The samples generator resides at http://uttool.com/text/regexstr/default.aspx.
If you are interested in a C# implementation of such string samples generator, https://github.com/moodmosaic/Fare can be useful.
There is also a generator from microsoft, Rex
Regexplained offers a visual explanation of the regular expression. Try it!
Check here for a list of tools for other implementations.
I am posting here links to some highly valuable tools I found for when you are working with regular expressions:
One is a playground/tester where you can literally play with regular expressions, in an extremely user-friendly manner.
The other is a tool for generating string samples based on a regular expression input. This is very handy when need to deal with all sorts of complex regular expressions you did not create, and you are trying to test if they are working or not. (Ex: when testing .xsd schema validations).
The playground/tester is located at http://www.regexr.com.
Features I like the most:
- You can hover over any part of the expression and get its meaning/understand what it does - on the fly.
- You can save whatever expression you have been playing with, and share them.
- It contains a summarized yet easy-to-understand reference for regular expression syntax components.
- You can type as many test strings you want, and it automatically highlights every match. It also colors every syntax component, making it easy to identify them in the pattern.
The samples generator resides at http://uttool.com/text/regexstr/default.aspx.
If you are interested in a C# implementation of such string samples generator, https://github.com/moodmosaic/Fare can be useful.
There is also a generator from microsoft, Rex
Regexplained offers a visual explanation of the regular expression. Try it!
Monday, July 20, 2015
Difference between explicit casting and using the "as" operator in C#
He who does not ask, will not learn. I would not learn this, if I did not ask - something else.
Upon raising the following question in StackOverflow, I started devising the ideal solution I would like to see for the problem (in an ideal world, a place I have been inhabiting for the past 30 years, alone).
Then, the person who offered an answer to the question - made a side remark.
Well, it then quickly had me building up a fresh new question :-)
He suggested instead of performing a cast using the "as" operator, I should do it simply with (T).
Example: say x is variable of type "object", and some type T, then I had var z = x as T;
He stated I should have var z = (T) x;
At first I noted, with the characteristic stubbornness - "it's a matter of taste. I think "as" makes the intent clearer". Minutes later, after playing with the solution, I needed to cast a double type. And thus... thankfully, I learned something new :-)
Upon raising the following question in StackOverflow, I started devising the ideal solution I would like to see for the problem (in an ideal world, a place I have been inhabiting for the past 30 years, alone).
Then, the person who offered an answer to the question - made a side remark.
Well, it then quickly had me building up a fresh new question :-)
He suggested instead of performing a cast using the "as" operator, I should do it simply with (T).
Example: say x is variable of type "object", and some type T, then I had var z = x as T;
He stated I should have var z = (T) x;
At first I noted, with the characteristic stubbornness - "it's a matter of taste. I think "as" makes the intent clearer". Minutes later, after playing with the solution, I needed to cast a double type. And thus... thankfully, I learned something new :-)
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GenericCaster<string>(12345));
Console.WriteLine(GenericCaster<object>(new { a = 100, b = "string" }) ?? "null");
Console.WriteLine(GenericCaster<double>(20.4));
//prints:
//12345
//null
//20.4
Console.WriteLine(GenericCaster2<string>(12345));
Console.WriteLine(GenericCaster2<object>(new { a = 100, b = "string" }) ?? "null");
//will not compile -> 20.4 does not comply to the type constraint "T : class"
//Console.WriteLine(GenericCaster2<double>(20.4));
}
static T GenericCaster<T>(object value, T defaultValue = default(T))
{
T castedValue;
try
{
castedValue = (T) Convert.ChangeType(value, typeof(T));
}
catch (Exception)
{
castedValue = defaultValue;
}
return castedValue;
}
static T GenericCaster2<T>(object value, T defaultValue = default(T)) where T : class
{
T castedValue;
try
{
castedValue = Convert.ChangeType(value, typeof(T)) as T;
}
catch (Exception)
{
castedValue = defaultValue;
}
return castedValue;
}
}
Bottom line: GenericCaster2 will not work with struct types. GenericCaster will.
Subscribe to:
Posts (Atom)
