Consider the next scenario:
You want to have a generic class, to handle different types of primitive data, but also strings.
So you'd like to have something like this:
public class Manager<T>
where T: struct OR string
{
...
}
or maybe you'd like to handle objects and strings:
where T: (ISomeInterface, new()) OR struct
or even specify some specific available types:
where T: long OR int
or two possible base classes:
where T: MyGreatUserControlBase OR MyGreatWebControlBase
et cetera.
I'd like to hear comments about that (and suggestions to the OR sign - |, ||, ; or whatever). If the feedback will be positive, I might suggests it to MS guys as a ladybug or such, and we might see it as part of C# 3.0, or maybe 4.0?
I'm downloading the whole package, letting Roee play with the WPF and Oren with WWF, leaving the WCF stuff to myself.
I hope to have some insights on the subject, soon.
The Linq preview is already installed, and I'm working on some minor project as part of my Bachelor's degree seminar, dealing with the impendency mismatch, between the software OO world and the database E-R world, using DLinq to demonstrate this kind of O/R mapping solution.
The web application that we're working on now, uses ASP.NET 2.0's ObjectDataSource model, to bind to GridView and FormView in the front-end.
Starting off, I've made a hierarchy of base classes to manage the data-binding and visual behaviors for Entity Pages (used to view, edit or insert a single entity) and Master-Details Pages (used to manage Master-Details scenarios).
The base classes looked like:
class BasePage : Page { /* ... */ }
class BaseEntityPage<T> : BasePage
where T: Entity
{
public BaseEntityPage()
{
/// hook ObjectDataSource and FromView databind events
}
//...
}
class BaseMasterDetailsPage<T> : BaseEntityPage<T>
where T: Entity
{
public BaseMasterDetailsPage() : base()
{
/// hook GridView databind events
}
//...
}
Somewhere along the road, we've decided to change the first module being developed, to use ascx controls in a single page, instead of multiple pages in a master page (due to the fact that it became a nested master page, and vs2005 doesn't like it).
So, a developer working on the change from Pages to UserControls, changed the base class to something like that:
class BaseUserControl : UserControl { /*...*/ }
class BaseEntityUserControl<T> : BaseUserControl
where T: Entity
{
public BaseEntityUserControl()
{
/// hook ObjectDataSource and FromView databind events
}
//...
}
class BaseMasterDetailsUserControl<T> : BaseEntityUserControl<T>
where T: Entity
{
public BaseMasterDetailsUserControl() : base()
{
/// hook GridView databind events
}
//...
}
Later on, we needed some standalone pages (not "mastered") to have data capabilities.
We already had the databinding for UserControls, but now we would need to create empty "dummy" pages and host UserControls in them, which means that instead of aspx + aspx.cs files per page, we'll have aspx + aspx.cs + ascx + ascx.cs files !!!
We could always keep the BasePage hierarchy next to the BaseControl hierarchy, but it will create an ugly duplication.
If Multi-Inheritance was possible, we would have used something like:
class EntityController { /* ... */ }
class BaseEntityPage<T> : BasePage, EntityController { /* ... */ }
class BaseEntityUserControl<T> : BaseUserControl, EntityController { /* ... */ }
The solution is to use an external controller, we'll call the Manager, to do all the recurrent login that applies to both the Pages an the UserControls.
We'll use an EntityManager that will manage both BaseEntityPages and BaseEntitycontrols, and a MasterDetailsManager that will manage BaseMasterDetailsPages and BaseMasterDetailsControls. Each and every page and UserControl will register itself with a manager.
Now the base class look like that:
class EntityManager<T> {}
class MasterDetailsManager<T> : EntityManager<T> { }
class BaseEntityPage<T> : BasePage
where T: Entity
{
private EntityManager<T> manager;
public BaseEntityPage()
{
manager = new EntityManager<T>(this);
}
//...
}
class BaseMasterDetailsPage<T> : BaseEntityPage<T>
where T: Entity
{
public BaseMasterDetailsPage() // not calling base() cuz we need a different manager
{
manager = new MasterDetailsManager<T>(this);
}
//...
}
class BaseEntityUserControl<T> : BaseUserControl
where T: Entity
{
private EntityManager manager;
public BaseEntityUserControl ()
{
manager = new EntityManager<T>(this);
}
//...
}
class BaseMasterDetailsUserControl<T> : BaseEntityUserControl<T>
where T: Entity
{
public BaseMasterDetailsUserControl () // not calling base() cuz we need a different manager
{
manager = new MasterDetailsManager<T>(this);
}
//...
}
All the databinding logic and visual behavior control, is now placed in the managers classes, and the page or control itself, is only dealing with the things that are specifically needed by it.
The developers on the team, when creating a new page or UserControl, only need to register their page or control to the appropriate manager, and not to mind all the databinding and behavior stuff.
I've been yesterday to a lecture, given by my colleague Oren Ellenbogen, on the subject: "Code Templating – Advanced use of Delegates and Generics in c# 2.0".
The lecture took place at Raanana's Microsoft offices, as part of the C++/C# User Group.
He presented us with a way to refactor our recurring code blocks, by "separating the recurrent from the unique". This technique gives us the ability to have the recurrent logic (let's say, open a db connection, apply logging and transaction management, etc.) only on one place, aka – the code template, and then apply unique logic (actual select clause, or population of a Business Object from the database, etc.) where you need, using the template as a wrapper.
The technique can be achieved using Interfaces, but thanks to the anonymous delegates and methods presented by C# 2.0, the process can be more "code viewer friendly".
All that (and a more technical explanation) would probably be published on Oren's blog, during the next few days, so I'd advise on keeping an eye (or rss reader) on that one.
One note about the lecturing technique he took – well, he did let the audience ask question anytime during the lecture, and actually answered their questions immedietly. This caused some flaws in the flow of the lecture, so I'd advice to be firm with the audience and postpone any question to a predetermined point (or points) in the lecture. The accompanying powerpoint presentation was lovely made, with large and readable fonts, nice images to emphasize important points, and the Visual Studio demos where pre-written, well documented, and presented well, using some nice add-ons ,such as "demo font" and Windows Magnifier.
I hope too, that I'll be able to give a lecture to the group during the next months, if I'll have time to compose one …
I've just checked activity on the blog, using the blog's backoffice, and I've found out that google searches came up with this blog for some people searches today. I've picked one of the searches, and found out that I was first on google !!!
Thumbs up !!! To me, and of course, to newtelligence guys who've designed and wrote the dasBlog engine.
And if you do not believe me (I find it hard to. Not generally of course, since I'm a decent reliable person most of the time, but the matter of the little googled I've surprisingly gained), attached here is a snapshot:
Just got back from Eilat.
It was my first-time at a Tech-Ed event and I'd like to thank my bosses at SQLink that gave me the opportunity to go there, after only about half a year in the company.
I am very impressed by the level of the organization. Everything there as ticking to the second, from the gr8 sessions, through the transportation system, and of course, the famous beach party ...
I went there with my Manager (Moty), my CTO (Roee) and my colleage (Oren). We've all learned a lot, and had some fun (check Oren's blog for details in the next few days - He brought a magical non eating batteries kind' a digital camera along with us).
It was my first experience meeting the MS people that actually have a lot of influence on the way we all code, being the ones that design and implement core features of the development frameworks, and even of the programming languages themselves.
I'll update this post once I've settled from the trip (including a lot of petting to my 3 cats ...).
I've also had a little time there to draft some messages I've wanted to post here during the last two months. I hope I'll upload some of that during the upcoming weekend, and the week that follows. I hope. That I will post, not that the week will follow, cuz weeks usually follows weekends, but I do not usually post more that one post a week. I hope, too, that this would change. Not the week-following-weekend-following-week thingy, but the posting habits of mine.