views:

63

answers:

4

Hey Everyone --

If I want to compile my LINQ->SQL classes into a DLL to provide to my developers, how can I make it so that they can still hook into the various entity partial methods (OnValidate, for example)? I know that you cannot implement partial methods on a compiled assembly, but any suggestions on how I can extract this behavior (perhaps with events?) would be appreciated.

+1  A: 

As far as I am aware, you cannot.

When partial classes are encountered by the compiler, it combines them together to form a complete class. The only way I see your needs being fulfilled is to make your classes inheritable, but with L2S, this may prove to be more trouble than it's worth.

EDIT:

As for events, depending on the size of your L2S class count, it all depends on what you're willing to put in. This solution could work, but will take a long time to get right. Combining Interfaces with events and custom handlers can get you there, just be prepared for the time investment if there are a large number of classes you want accessible.

MSDN - Partial Classes

Kyle Rozendo
A: 

I would handle this by publishing public events at any point you desire.

JoshJordan
+1  A: 

You may need to customize this solution for your needs, but a simple way of publishing events from partial methods can be done like so:

partial class LinqClass
{
    public event Action<LinqClass, ChangeAction> OnValidating;

    partial void OnValidate(ChangeAction action)
    {
        if (OnValidating != null)
        {
            OnValidating(this, action);
        }
    }
}

You may or may not need to pass along different parameters, but the Action will support numerous.

Ryan Versaw
A: 

If I want to compile my LINQ->SQL classes into a DLL to provide to my developers

I dont think that this is a good idea. There are so many reasons why a LinqToSql Model might want to change. You will likely need to modify your model while discovering new insight into your domain etc.

By creating a seperate DLL "to provide to your developers" you create an artificial boundary that is likely to hinder efficiency in development. Getting a source control system might be more appropriate for what you may try to accomplish (beeing VERY vague here)

Johannes Rudolph
Yes, in most cases I'm totally in synch with you. In my scheme, though, we generate the linq->sql classes based on an internal tool that we use to model the database model quickly which lets us regenerate whenever we need to, this way the linq->sql classes and our database are always in synch.
Mustafakidd