DCSIMG
DEV - Shimmy on .NET

Browse by Tags

All Tags » DEV (RSS)

Entity-Framework Code First Designer with Database Initialization in few simple steps! by Shimmy

I find it very frustrating that I cannot use the entity model designer to generate code-first DbContext & entities that retains all the aspects of code-first including database initialization. Here are a set of instructions that can easily help you to achieve true code-first using the EDM designer, and allowing initialization of database (database initialization is not supported in model-first, it only allows generation of database scripts that has to be generated on the server). This is very...

Get a list of all countries in C# by Shimmy

I'm sharing it here mostly for myself, but I hope someone else would also be able to enjoy it once... static IEnumerable < Country > GetCountries () { return from ri in from ci in CultureInfo . GetCultures ( CultureTypes . SpecificCultures ) select new RegionInfo ( ci . LCID ) group ri by ri . TwoLetterISORegionName into g //where g.Key.Length == 2 select new Country { CountryId = g . Key , Title = g . First (). DisplayName }; } class Country { public string CountryId { get ; set ; } public...
תגים:,

No ICloneable in WinRT? by Shimmy

Looks like the ICloneable interface has not been included in WinRT. I could guess it's because of the uncertainty of whether the implentation is a shallow or a deep clone. Would like to hear what you think about it.
תגים:, ,

Get Jewish date string by Shimmy

namespace System . Globalization { public static class DateTimeExtensions { private const string HebrewCultureName = "he-IL" ; private static readonly CultureInfo HebrewCulture = #if SILVERLIGHT new CultureInfo ( HebrewCultureName ); #else CultureInfo.CreateSpecificCulture(HebrewCultureName); #endif /// <summary> /// Converts a gregorian date to its hebrew date string representation, /// using custom DateTime format string. /// </summary> /// <param name= "value"...

Determine if char is Hebrew (2) by Shimmy

About 2 weeks ago I posted a function that determines whether a given char is hebrew: // avoid unicode chars in code private const char FirstHebChar = ( char )1488; //א private const char LastHebChar = ( char )1514; //ת private static bool IsHebrew ( this char c ) { return c >= FirstHebChar && c <= LastHebChar ; } Schabse Laks commented that it won't include punctuation marks / vowels etc. This week I needed a more sophisticated function that does the job, so I ended up with the...
תגים:, , , , ,

LeakEvent → WeakEvent by Shimmy

I'm sure not any Silverlight developer is aware of the fact that when one subscribes to an event, a strong reference to the handler class ('target') is attached to the event source ('source'), hence, if the source object life-cycle is longer than the target, memory is leaked, becuase when you don't need the target anymore and all its references are removed, there is still one reference to it by that event handler in the source, and thus, won't get collected by GC. A strong...

UniqueAttribute that validates a unique field against its fellow rows in the database (inherits DataAnnotations.ValidationAttribute) by Shimmy

UPDATE: Jan 30 2012 : Added check that entity is not itself (i.e. when user attempts to save the same entity). UPDATE: Feb 26 2012 : Added support for inherited entities, where the validated property is on the sub-class of the inheritance hierarchy. Here is a ValidationAttribute subclass that will allow you to validate that a column doesn't have duplicates. It's intended to be used with EF 4.1 DbContext , or with the EF4's ObjectContext . namespace System . ComponentModel . DataAnnotations...

Determine if char is Hebrew by Shimmy

Simple Ex. method: // avoid unicode chars in code private const char FirstHebChar = ( char )1488; //א private const char LastHebChar = ( char )1514; //ת private static bool IsHebrew ( this char c ) { return c >= FirstHebChar && c <= LastHebChar ; } Note: a comment bellow noted that the above function solely checks for hebrew characters within the א-ת range, it won't include punctuation marks, vowels or other biblical symbols. For a function that covers all (all that I'm aware...
תגים:, , , , ,

Enum.IsDefined for combined values not declared in the enum body by Shimmy

When you try to determine if an Enum value is defined in an Enum, it returns true only if the flags you've combined are explicitly declared in the Enum. Consider this Enum: [ Flags ] // Enum defined with a FlagsAttribute attribute. enum Hue : sbyte { Black = 0, //no color Red = 1, Green = 2, Blue = 4, Cyan = Green | Blue , //6 Magenta = Red | Blue , //5 BadColor = 127 } The following line will return false: var white = Hue . Red | Hue . Blue | Hue . Green ; //7 Even tho theoretically the enum...
תגים:,

Close window from ViewModel by Shimmy

Although this is somewhat corny, I will dedicate for it a post. Scenario: you want to close a window from the ViewModel, without exposing the Window to it. This can be done in a simple and clean way. (Some code removed for brevity) HTH, Shimmy XAML: < Window x : Name = "this" xmlns : i = "http://schemas.microsoft.com/expression/2010/interactivity" xmlns : ei = "http://schemas.microsoft.com/expression/2010/interactions" > < i : Interaction.Triggers > <...

TIP: Silverlight 5 Beta IMarkupExtension<T> and StaticExtension by Shimmy

In WPF, there is the well known StaticExtension . However Silverlight users are struggling in finding quirky workarounds to this missing feature. Now, in Silverlight 5, Microsoft introduced the IMarkupExtension<T> interface. This one, not only reflects to the WPF MarkupExtension , but also is a generic interface rather than a class. The MarkupExtension abstract class features only one method ProvideValue , this is a huge disadvantage, since you cannot inherit from multiple classes, and if you...

TIP: Dictionary<Type, T> - Use generic type argument as keys by Shimmy

When you want to store items by known types using the type as the key, you could add some methods to the Dictionary<,> class to make this work the generic way. It basically does nothing but does the typeof ( MyType ) for you. Note: Didn't test performance impacts. using System . Collections . Generic ; namespace System . Collections . Specialized { public class TypeDictionary < TValue > : Dictionary < Type , TValue> { public TValue Get < T >() { return this [ typeof (T...
תגים:,

Tip: Casting in C# shortcut extension method by Shimmy

The C# cast operator (Type)value is pretty verbose. When you work with COM, extenal APIs, DataSet s or other not well-OOPed infrastractures, you often get an expected type as object (i.e. you know the runtime-type of the variable is string , but it's object at compile-time). Here is an example of many castings in one-line: IEntity entity = ( IEntity ) GetObjectById ( "Customer" , 34); string name = ( string ) entity . GetField ( "Name" ); int length = name . Length ; Or as...

Tip: VB's 'My' namespace for Silverlight by Shimmy

Silverlight lacks the My namespace, but for those who are addicted to the My keyword, it can still be achieved manually. Open windows explorer and navigate to your project Add a new file MyExtension.vb to this folder (location and filename are optional) Include it in your project Add the below content to it Although the Info , Computer , and many other features are missing from the Silverlight runtime, you could use alternatives from System.Reflection etc. Here is a MyExtension.vb sample, you can...

TIP: Have the 'SILVERLIGHT' processor directive appear in intellisense in the ASP.NET host project (VB.NET) by Shimmy

Nothing special anyway, but I thought let's write it down briefly. In Silverlight solutions, especially when RIA Services is enabled, it's pretty common to use the SILVERLIGHT processor directive in the shared code and other. So the SILVERLIGHT directive is known in the Silverlight project, but in the ASP.NET there it doesn't appear in the intellisense, and since this is always valid, it's kinda error-prone when there is no intellisense support. To enable the intellisense simply open...
More Posts Next page »
Powered by Community Server (Commercial Edition), by Telligent Systems