8

I have just been refactoring nearly all classes in my Services layer to inherit from a ServiceBase, to reduce duplication in initializing data access and other aspects identical to nearly all services, but I was stopped in my tracks when I reached my RoleService, as it has to inherit from RoleProvider so that I can configure it as my web site's 'official' role provider.

Now it is a bit late at night, and the caffeine is on form, but I was wondering if there was any way to use a dynamic object in place of a derived object, and add all the members of the base object to the 'derived' object, at runtime, instead of compile time.

Is this even remotely possible?

2
  • 1
    +1 for the interesting question. I have no idea :) Commented Apr 19, 2011 at 21:45
  • 2
    Not directly answering your question here, but I've found inheriting from a common base class to be largely unnecessary when separating concerns and using Dependency Injection properly. Something to consider. Commented Apr 19, 2011 at 21:46

3 Answers 3

3

No, DynamicObject does not allow you derive from two concrete classes, which is what multiple inheritance is, and which C# does not support. The problem you face is the same either way, dynamic or static. If you have Base1 and Base2 that are unrelated to each other, then as soon as you derive Derived from Base1, there is no way that that Derived is Base2 can ever be true. Instead you can settle for Derived is IBase2.

I recommend that you use the:

together with multiple interfaces or one concrete derivation and one interface. To simulate multiple inheritance you:

  • create an interface instead of a base
  • implement the interface instead of deriving from the base
  • create an implementor class that does the work you wanted to do in the base
  • "bridge" to the implementor by forwarding all the interface calls to an instance of the implementor

This limits the amount of code in your implementing class to just one forwarding call per interface method or property.

Sign up to request clarification or add additional context in comments.

3 Comments

I never said I want to enherit from two classes, but rather dynamically build an object with members from both 'base' classes.
@ProfK: The very title of your question includes the phrase "multiple inheritance". That very clearly says that you want to mimic inheriting from two classes. Nevertheless, I still believe DynamicObject is of no help in adding members from another pseudo base class. At least not any better than the bridge pattern.
I'll reformulate this question later to not mention 'inheritance', and rather to deal with simply merging members from more than one object into one object.
1

You should be able to do that with the DynamicObject class.

Comments

1

Dunno if you really need to go through the trouble at the class level, but here are some resources that might be helpful for setting up your Role provider as a WCF Service:

http://wcfsecurity.codeplex.com/wikipage?title=How%20To%20-%20Use%20SQL%20Role%20Provider%20with%20Windows%20Authentication%20in%20WCF%20calling%20from%20Windows%20Forms (what a link!)

http://msdn.microsoft.com/en-us/library/aa702542.aspx

edit---- Also it was mentioned in this post here: Can I create a custom roleprovider through a WCF service?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.