While developing a library in .Net which could be used by another programming language than it was originally designed in, it is important to remember that the .Net Languages offers a subset of CLR/CTS while offering a superset of CLS. And, if you want to develop a library that could be used by any programming language supported by .Net, you have additional responsibility to ensure that your code doesn’t take of care of any features outside the CLS.
Thankfully, .Net provides us an easier way to check the same using the CLSCompliant Attribute.
public class TestClass
public int Method1() => 1;
public int method1() => 2;
private int mEthod1() => 3;
The above code would raise warning “Warning CS3005 Identifier ‘TestClass.method1()’ differing only in case is not CLS-compliant” to help us identify the non-compliance. Do note that mEthod1 doesn’t raise a warning as it is a private method and is not exposed outside the assembly.
Anyone working on .Net application with MySql 5.7 and EF in Db First Approach would have come across what is an officially confirmed bug. This bug rises when you attempt to generate/update your Entity Model after changes to the Database.
“Unable to generate the model because of the following exception: ‘System.Data.StrongTypingException: The value for column ‘IsPrimaryKey’ in table ‘TableDetails’ is DBNull. —> System.InvalidCastException:”
The workaround you need to do to fix this one
- Open your Workbench and execute following commands on your Db.
set global optimizer_switch='derived_merge=off';
- Close your workbench ( you might have to restart your visual studio as well ).
- Generate/Update your model.
That should work.
An easier way to parse for folders, which do not contain files with a specified extension.
Directory .GetDirectories( @"D:\Root Folder")
.Where(t => Directory .GetFiles(t, "*.txt" ,SearchOption .AllDirectories)
.ToList().Count == 0);
Mocking objects is integral part of Unit Testing. The nucleus of Mocking is Dependency Injection, which allows us to inject Mock object into the code. But what happens when programmer has used the “Using” keyword ? For Example,
using(CustomDbCommand cmd = new CustomDbCommand ())
This makes it difficult to mock DbCommand. The solution lies in deploying Factory Pattern to create our CustomDbCommand. For the purpose, I have created a IDbCommandFactory interface and its concrete solution in DbCommandFactory. It comprises of a method called CreateDbCommandObject, which returns a new instance of CustomDbCommand;
public interface IDbCommandFactory
public class DbCommandFactory : IDbCommandFactory
public CustomDbCommand CreateDbCommandObject()
return new CustomDbCommand();
Now that we have our Factory Object ready, it is time to inject it into our Repository Class. I prefer to use Constructor Injection for the purpose.
private readonly IDbCommandFactory objDbCommmandFactory;
public TestRepository( IDbCommandFactory DbCommandFactory)
objDbCommmandFactory = DbCommandFactory;
public void TestMethod()
using ( var cmd = objDbCommmandFactory.CreateDbCommandObject())
Now we are all set to Mock our CustomDbCommand Object, or in other words, our IDispossible object. Happy Unit Testing.
When developing domain classes in Entity Framework using Code First Approach, the default code conventions creates a primary key of a property named “ID” or <ClassName>ID. But if you wanted to use another property as your primary key , you can use DataAnnotations and decorate the property using “Key” attribute.
What is more interesting is that you could actually create a composite primary key. All you need to do is use another attribute along with “Key”, namely, “Column”.
The above example creates a Table with a Composite Primary Key made up of Dept1 and Dept2. Entity Framework is surely letting the developers live in the comfort zone of Visual Studio rather than dig up SQL Server.
Again one of those things which looks so simple, but could drive one mad during debugging.
Much before Optional Parameters came in, C# developers used to depend on Method Overloading for mimicking Optional Parameters. Consider a scenario where both exists, what would be the output ?
The output would be :
The reason behind this is the overload resolution method adopted when we have a overloaded method and a method with optional parameter. Here is what MSDN says.
If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.