ProfK
ProfK

Reputation: 51114

Can I somehow mimic multiple inheritance using dynamic objects?

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?

Upvotes: 8

Views: 1178

Answers (3)

Rick Sladkey
Rick Sladkey

Reputation: 34250

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.

Upvotes: 3

porusan
porusan

Reputation: 918

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?

Upvotes: 1

Mark Cidade
Mark Cidade

Reputation: 100047

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

Upvotes: 1

Related Questions