Re: Access subclass attributes from base
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Thu, 20 Dec 2007 20:03:40 +0100
Kyle M. Burns wrote:
Remember that it's totally irrelevant if it's neat in *theory*. A framework should be offering *practical* benefits. So now we've got the sermon out of the way, let's look at the actual issues.
We're not talking theory here, but a set of objects that share a common creation strategy in a distributed COM+ environment where logging and other corrective measures to ensure the client obtains an instance of the object need to be taken.
In that case, you have my condolences. DCOM is no fun.
The approach originally recommended by Microsoft was a factory object with the interface described earlier that implements more than a few lines in the actual factory method and needs to be applied to every one of our factories.So you have share functionality (actually, it sounds more like *identical* functionality to me). There are still quite a lot of ways to implement that.
Alright, I have a clearer picture of what you're trying to achieve now.Why do you need to mention "BusinessObject" at all at this point? Isn't the factory implementation the thing that has to deal with BusinessObjects eventually? Then let that take care of it and don't hoist this dependency to the general level, where it doesn't belong.The subclasses only responsibility is to tell the base class to which interface Instance must conform and which concrete class will be used in the creation, so this is the only point in the creation process in which BusinessObject matters at all.
Yes, you will. But there doesn't seem to be any pressing reason to muck about with attributes. From what I've gathered so far, your base class does *everything*, including the instantiation, so I gather it has some way of instantiating a business object given its concrete type. How about:You can use Object.GetType() to get the concrete type of the instance, and Type.GetAttributes() to get the attributes of that type.I was trying to find a way to accomplish this from the static factory method, but it looks like I may need "this" in order to obtain the attributes.
static class BusinessFactory { // Or make it a singleton
public static IBusinessObject CreateBusinessObject<IBusinessObject, TBusinessObject>() {
/* Instantiate a new TBusinessObject here, return as IBusinessObject*/
}
};
public class WidgetFactory {
public IWidget CreateWidget() {
return BusinessFactory.CreateBusinessObject<IWidget, Widget>();
}
}
Add interfaces and subtyping where necessary. Since WidgetFactory only uses the concrete Widget type in its method, not its type signature, there is no dependency between the clients of WidgetFactory and the Widget. Or are you worried about some other dependency? It's not completely clear to me. Note that there is obviously no way to avoid some dependency between WidgetFactory and Widget, since it will have to pass the type to the business object factory somehow! If you want to remove *all* compile-time dependencies, you'd have to use reflection to dynamically get the types, but this seems far more trouble than it's worth.
--
J.
.
- References:
- Re: Access subclass attributes from base
- From: Jeroen Mostert
- Re: Access subclass attributes from base
- From: Kyle M. Burns
- Re: Access subclass attributes from base
- Prev by Date: Re: Access subclass attributes from base
- Next by Date: Re: which IL OpCodes call a method
- Previous by thread: Re: Access subclass attributes from base
- Next by thread: Re: Access subclass attributes from base
- Index(es):
Relevant Pages
|