Skip to main content

Posts

Showing posts with the label C#

Adding Unique Constraints with Entity Framework Code First

Entity Framework Code First is a great way to define and maintain your database within your application it self. While it poses a nice set of complementing libraries like Data Annotations and Fluent Configurations which helps you specially in defining phase of your database, it would give you a headache if you try to define a unique constraint on a column. For example in the users table of your application you could probably have an int ID column which would serve as the primary key and you might need to make your Username column a unique one. Since you are using EF Code First you will soon find out there is no direct way to accomplish this requirement. Unfortunately fluent configurations doesn't have syntax like HasUnique(u => u.Username); I asked the same question in stackoverflow , but didn't get a convincing answer. Since there is no direct support from EF for this you could take one of following alternatives to achieve it. Approach 1 :  ...

Using BackgroundWorker in C#

අපි අද කතා කරන්න හදන්නේ තවත් අපිට ගොඩක් ප්‍රයෝජනවත් වෙන C# වල තියෙන feature එකක් ගැන. ඒ තමා මේ BackgroundWorker කියන component එක, මේක පාවිච්චි කරලා අපිට පුළුවන් අපේ programs වලට Threads හදාගන්න. අපි සාමාන්‍යයෙන් Threads පාවිච්චි කරන්නේ මොකක් හරි ගොඩක් වෙලා යන වැඩක් program එකෙන් කරද්දී ඒ වැඩේ program එක පාවිච්චි කරන කෙනාට එහෙම නැත්නම් User ට නොතේරෙන විදියට කරගන්න, උදාහරණයක් විදියට අපි database එකකට connect වෙලා data set එකක් fetch කරනවා කියල හිතන්න මේ වැඩේට ලොකු වෙලාවක් යනවා නම් User හිතෙන්නේ program එක හිර වෙලා කියල. ඉතින් මෙන්න මේ ප්‍රශ්නේ විසඳා ගන්න පුළුවන් අපිට BackgroundWorker එක පාවිච්චි කරලා. ඒ කියන්නේ පොඩි Progress Bar එකක් වගේ දෙයක් පෙන්නලා user ට කියන්න පුළුවන් වැඩේ ඇතුලෙන් කෙරෙනවා කියලා. හරි අපි පටන් ගමු, මුලින්ම කරන්න තියෙන්නේ Visual Studio වලින් Windows Forms Application එකක් හදාගන්න එක. ඊට පස්සේ සාමාන්‍යයෙන් ඔයාලගේ right hand side එකේ Tool Box එක දිහා බැලුවම පෙනෙයි මෙන්න මේ වගේ මෙතන අපිට පේනවා BackgroundWorker කියල tool එකක් තියෙනවා, දැන් මේක drag කරලා drop ක...

Introduction to Extension methods

අද අපි කතා කරන්න ලෑස්ති වෙන්නේ C# වල තියෙන අපිට ප්‍රයෝජනවත් වෙන තවත් feature එකක් ගැන. ඒ තමා මේ Extension Methods. අපි පුරුදු විදිහටම සරල උදාහරණයක්‌ එක්කම මේක පටන් ගමු. using System; namespace ExtensionTest { static class ExtensionMethods { public static int GetSquaredValue(this int num) { return num * num; } } class Program { static void Main(string[] args) { int i = 20; int squaredValue = i.GetSquaredValue(); Console.WriteLine("The squared value of {0} is {1}.",i,squaredValue); } } } දැන් බලමු මොකක්ද මෙතන ඇත්තටම සිද්ද වෙන්නේ කියල. ඕනේ නම් මේ code එක run කරලා බලන්නකො වැඩ කරනවා ද කියල. මුලින්ම අපි කරලා තියෙන්නේ static class එකක් declare කරනවා අපේ Extension method එක ලියාගන්න. ඊට පස්සේ අපි කරන්නේ අපේ Extension method එක ලියාගන්නවා මේ class එක ඇතුලේ. මෙතන අපි ගන්නේ ඉලක්කමක වර්ගය හොයාගන්න පුළුවන් method එකක් GetSquaredValue කියල නේ නම දාල තියෙන්නෙත්...

If then else short hand

ඔයාල කවුරුත් ගොඩක් වෙලාවට පාවිච්චි කරන if then else කියන statements ගැන අලුත් දෙයක් කියල දෙන්න තමා අද ලෑස්ති වෙන්නේ. මුලින්ම අපි බලමු සාමාන්‍යයෙන් අපි if then else ලියන්නේ කොහොමද කියල. අපි මේ වෙලාවේ උදාහරණයක්‌ විදිහට ගමු ඉලක්කම් දෙකකින් ලොකු ඉලක්කම මොකක්ද කියල හොයාගන්න පාවිච්චි කරන සරල function එකක්. අපි කවුරුත් මේ වගේ එව්වා ලියල තියෙනවා programming පුරුදු වෙන කාලේ, private int GetMax(int i, int j) { int max; if (i > j) { //condition එක true උනාම කරන්න ඕනේ වැඩේ max = i; } else { //condition එක false උනාම කරන්න ඕනේ වැඩේ max = j; } return max; } එත් ඉතින් මෙච්චර පුංචි දෙයක් ලියාගන්න ඇයි අපි මෙච්චර මහන්සි වෙන්නේ? ඒ function එකම අපිට ලියාගන්න පුළුවන් පහල පෙන්නලා තියෙන විදිහට. private int GetMax(int i, int j) { int max; max = i > j ? i : j; return max; } මේ ලියල තියෙන code එක පොඩ්ඩක් විතර කතා කලොත්, = ලකුණයි ? ලකුණයි අතරට එන්නේ අපිට check කරන්න ඕනේ condition එ...

Passing Arrays to methods in a better way

අද අපි බලන්න යන්නේ C# වල තියෙන තවත් අපට ගොඩක් ප්‍රයෝජනවත් වෙන දෙයක් ගැන. ගොඩක් වෙලාවට අපි methods ලියද්දි අපිට ඕනේ වෙනවා මේ methods වලට Arrays pass කරන්න. ඒ වගේ වෙලාවට අපි කරන්නේ method එක call කරන තැන Array එකක් හදල ඒකට data දාල method ඒකට pass කරන එක.  පහල පෙන්නලා තියෙන්නේ එක කරගන්න විදිහ. int Add(int[] data) {    int sum = 0;    for (int i = 0; i < data.Length; i++)    {       sum += data[i];    }    return sum; } අපි දැන් මේ method එක call කරන්න පුළුවන් මෙන්න මේ විදිහට. void TestArray() {     int[] data = new int[4];     data[0] = 3;     data[1] = 5;     data[2] = 2;     data[3] = 7;     int sum = Add(data); } පේනවා නෙ අපි array එක හදල ඒකට values දාල එවන විදිහ.මේක ඉතින් එච්චරම ලේසි වැඩක් නෙමෙයි කියල දැන් පැහැදිලි නෙ. ඒත් මේක කරගන්න මීට වඩා ලේසි ක්‍රමයක් තියෙනවා. ඒ ගැන කතා කරන්න තම අපි මේ ලෑස්ති වෙන්නේ. මේකට අපට...