Wednesday, June 3, 2009

Multiple inheritance for .Net is here!!!

I though i should post on this one cause its far more than interesting

suppose you have a Customer that is a Person

public class Person
{
public string Name { get; set; }
}

public class Customer : Person
{

}


and implements 2 interfaces ICustomUser,ICustomer



public interface ICustomUser
{
string Title { get; set; }
}

public interface ICustomer
{
string Title { get; set; }
}


one could write



public class Customer : Person,ICustomUser,ICustomer
{

string ICustomUser.Title { get; set; }
string ICustomer.Title { get; set; }

}


But in order to access the values of the properties Customer should be extended to something like



public class Customer : Person,ICustomUser,ICustomer
{
private string customUserTitle;
public string CustomUserTitle
{
get { return customUserTitle; }
set
{
customUserTitle = value;
}
}
private string customerTitle;
public string CustomerTitle
{
get { return customerTitle; }
set
{
customerTitle = value;
}
}

string ICustomUser.Title
{
get { return customUserTitle; }
set { customUserTitle = value; }
}


string ICustomer.Title
{
get { return customerTitle; }
set { customerTitle = value; }
}
}


Test]
public void Test_Customer_Interface_Implementations()
{
var customer = new Customer {CustomerTitle = "CustomerTitle", CustomUserTitle = "CustomUserTitle"};

Assert.AreEqual("CustomerTitle", customer.CustomerTitle);
Assert.AreEqual("CustomUserTitle", customer.CustomUserTitle);
}


PostSharp changes all that by allowing you to achieve compile time inheritance. What i mean by that? That we can use PostSharp to omit IL instructions to your assembly so after compilation it will be like you actually implement those Interaces based on some Types that already implement that.



Maybe i still sound confusing so lets write some code lets create the implementations of the events for some other type than customer



public class CustomUser:ICustomUser
{
public string Title { get; set; }
}

public class BaseCustomer:ICustomer
{
public string Title { get; set; }
}


wouldn’t be perfect if we could write our Customer like



public class Customer : Person, BaseCustomer, ICustomer
{



}


of course this do not compile. But PostSharp allows us to write it like



[SimpleCompositionAttribute(typeof(ICustomer),typeof(BaseCustomer))]
[SimpleCompositionAttribute(typeof(ICustomUser),typeof(CustomUser))]
public class Customer : Person
{

}


to be conviced open up Reflector and see what code PostSharp generate for your Customer class



reflector



and in order to acces the Value of the properties since they do not exist at design Time PostSharp provides the casting method Post.Cast<TSource,TTarget> which is both safe and efficient because it is checked and transformed at compile time.



[Test]
public void Test_Customer_Interface_Implementations()
{
var customer = new Customer ();
ICustomer iCustomer = Post.Cast<Customer, ICustomer>(customer);
iCustomer.Title = "CustomerTitle";
ICustomUser iCustomUser = Post.Cast<Customer, ICustomUser>(customer);
iCustomUser.Title = "CustomUserTitle";

Assert.AreEqual("CustomerTitle", iCustomer.Title);
Assert.AreEqual("CustomUserTitle", iCustomUser.Title);
}


Ans here you can download a sample for all the above MultipleInheritance

Subscribe to XAF feed
Subscribe to community feed

DiggIt!

1 comment: