Archive for category Keywords

Tribal Knowledge: Linq Entities To Do Your Bidding via the Partial Keyword in C#

TribalKnowledge2Having premade entities created during the Linq process in .Net really comes in handy. But did you know that one can extend those entities further outside of of the generated dbml file? By using the concept introduced in C# 2.0 of a partial class, where one can have business logic methods and other properties which are not generated but are none the less relevant in an Object Oriented fashion it can be achieved.

As mentioned the entities are generated and nothing is worse than having the generator throw away user code placed in the generated file after a regeneration. To allow for generated and user code to coexist hand in hand was accomplished by partial classes. That is where the partial keyword is appended to a class which allows for the class to have a subset of functionality in a different code file. The generated code file resides in one file and the user code lives elsewhere in a different file allowing them co-exist.

I have used this process to put validation logic on an entity. That logic was used to verify that the differing properties which the user had entered values against matched business rules. Those rules could not be entirely expressed in a database schema. It was very Handy!

Example

A user posted a question on the MSDN Forums as to how one could achieve indexing into a Linq generated class entity where each property represented a numerated indexed field. The below code shows the use of a partial class in a separate file which the user could  have an index-able entity.

public partial class Request
{
    public int this[int index]
    {
        get
        {
            int retValue;

            switch ( index )
            {
                case 0: retValue = Val1; break;
                case 1: retValue = Val2; break;
                case 2: retValue = Val3; break;
            }

            return retValue;
        }
        set
        {
            switch (index)
            {
                case 0: Val1 = value; break;
                case 1: Val2 = value; break;
                case 2: Val3 = value; break;
            }
        }
    }
}
Share

Tags: , , ,

C#: Explicit Keyword A Cast Which Acts Like a Copy Constructor

I actually missed this in a job interview…but ended up getting the job! C# does not provide a copy constructor but does provide a way to do such operations using the explicit keyword as a way to do it. What happens is that in a cast situation, one casts from one object type to a different object type. Here is an example where one could go from a dataset to another object such as this:

Dataset DS = GetDataSet();
myclass abc = (myclass) DS;

The cast actually returns a new myclass object. That magic is done using the explicit keyword. One can do many such casts as needed for differing objects Here is an example, call the static Usage() on the class, where I explicitly cast one class object to another. A new object is returned and it prints out the value from the cast’ed object:

// Class which will be cast-ed from
public class OtherClass
{

   public OtherClass() { originalValue = 42; }

   public int originalValue;
}

// Class We are interested in.
public class ExplicitTestClass
{
     private int _myValue;

     // Do nothing Constructor
     public ExplicitTestClass() { }

     // Mimic Conversion Constructor
     public static explicit operator ExplicitTestClass(OtherClass oc)
     {
         ExplicitTestClass etc = new ExplicitTestClass();

         etc._myValue = oc.originalValue;

         return etc;

     }

     public override string ToString()
     {
         return _myValue.ToString();
     }

     // This is just to show it working
     // as is not needed outside of this
     // blog and could/should be found outside the class.
     public static void Usage()
     {
         OtherClass oc = new OtherClass();

         ExplicitTestClass myETC = (ExplicitTestClass)oc;

         Console.WriteLine(myETC.ToString()); // Prints 42

     }
 }
  • Line 02: This class will hold the value which we are interested in. It is the target class which will be cast-ed from.
  • Line 11: This is our result class which after the cast will be our result.
  • Line 16: This constructor will create the blank slate for our operations.
  • Line 19: This allows the cast to our target class OtherClass, which we will get its value to and return a new valid ExplicitTestClass object.
  • Line 21: This new’s the new object to eventually be returned.
  • Line 23: This is the copy constructor esque section. We are only interested in one value…but there could be others.
  • Line 25: Return the valid new object.
  • Line 29: Just show the value we are looking for…
  • Line 37: This is only for this blog post and shows how to use the cast in a real world situation.
Share

Tags: , ,