• The semantics of c# interface

    We all use interface construct in c#. Recently I came across the Haack’s blog Interface Inheritance Esoterica, I decided to find out more. So I write a very simple example like below

    public interface IAnimal
        {
            void Walk();
        }
    
    
  • Get started with EntityFramework 4

    We have the following option to using EntityFramework4


    • Using designer
      • Model First

        We can design model first , then generate the sql for the model, use the sql to generate the database, and then connect the model to the database. The process of sql gereration is customizable. You change this workflow (process) and the template of the sql, these file are located at C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen.
        ASO.NET team blog has an article about this a approach.

      • Database First

        This is a classic approach. You have your database in the first place, and generate model based on that.


      Since you have use the designer, you have other customization options that are related to code generation from the model created by designer. But default designer use a Code generation strategy call "Default". If you model file name is "model.edmx", the strategy generate a cs file "model.cs" file, and the code generate the ObjectContext, and other entities, and so forth. The entities


      [EdmEntityTypeAttribute(NamespaceName="CrmModel", Name="Post")]
      [Serializable()]
      [DataContractAttribute(IsReference=true)]
      public partial class Post : EntityObject
      { ... }
      

      Since EF4 use t4 to generate code, you can customized the t4 to customized the code generation by right clicking the designer and adding code generation item. When you add code generation item, this will turn off the default code generation strategy. Your model.cs file will be empty.


      There are a couple code generation item available now. When you add an item a t4 file (*.tt) is added to the project. You can customize the tt file as want.


    • Use code only
      Feature CTP Walkthrough: Code Only for the Entity Framework (Updated)
  • Nullable notes

    Nullable is value type object. But the following code can be compiled.

    int? x = null
    

    Isn't "null" supposed to be used with reference type, why it can be assigned with value type? It turns out to be just syntax suger, and the compiler emit the following code. It does not call any constructor.


    IL_0001:  ldloca.s   x
    IL_0003:  initobj    valuetype [mscorlib]System.Nullable`1<int32>
    


    However, if you write the following code, compiler will emit the msil like the following. It call the constructor.


    int? y = 123;
    
    
  • make your obsolete method invisible

    When I started to use moq, there was lots example of moq that use mock.Setup method, althought these code can be compile. The this method is not shown up in intellisense popup, it turns out in the latest version of moq, it hide this memeber like below.


    [EditorBrowsable(EditorBrowsableState.Never)]
    [Obsolete("Expect has been renamed to Setup.", false)]
    public ISetup<T> Expect(Expression<Action<T>> expression);
    
  • When a constructor is not called

    Normally, when we build a .net object, clr will call the constructor.
    But In a few situations, an instance of a type can be created without an instance constructor being called. In particular, calling Object's MemberwiseClone method allocates memory, initializes the object's overhead fields, and then copies the source object's bytes to the new object. Also, a constructor is usually not called when deserializing an object.