If we take a step back, and think about what you're saying, I think
you'll realize it doesn't make sense.
It sounds like you're saying you want to have some generic object that
you hold onto in your class but you don't want to know what the type
of that object is. However, you want to be able to write code that
uses that object. There's not any sensible way that the compiler
could type-check your code without knowing what T is, which is why you
either need to specify a value of T in your field (i.e. bind the
parameter to a type), or you need to make T a generic parameter of
your type (i.e. leave it as an 'open' generic and 'provide' the T from
the use site of your class).
The thing I'm not really getting is why the control author cares what
T is. It seems to me that if you're doing VM / PM pattern well, you
should be striving to have very little (if any) code in your controls,
and all of the interaction should be through bindings to the VM (which
are - by definition - untyped, though if you use something like the
binding frameworks that Glenn has been trying to build, you'll see
that you can use stronger typing in your bindings).
What you can do, and I've done this in the past, is not make the
repository generic, but rather make its methods generic (i.e.
Query<T>(...) instead of IRepository<T>::Query(...)) but this won't
allow you to resolve different repositories by type from the IoC
container.
The other thing I'd ask yourself is whether inheritance is the right
solution to your problem. In nearly every case in my past that I
thought it was, I turned out to be wrong. You might also think about
whether the type of the repository is important to be in a base class,
or whether you can get the same effect by having the base class be
abstract, providing a few methods to 'hook' into the derived class
implementations, and then you can have the type parameter on the base
class and have the deriving class provide the "T" when it derives. In
other words, you could make:
public class FooContainerControl: ContainerControlBase<Foo> { ... }
We do this a lot in our UI code (though we do it with the PMs, not the
controls, I personally hate base classes for controls - it never works
out well, we use interfaces exclusively for them). For instance, we
have a:
public class EditorGridPresentationModelBase<T> where T: IItemCore, class
{ ... }
and then for Table and Variable (both IItemCore implementers), we have
public class EditorGridPresentationModel: EditorGridPresentationModelBase<Table>
{ ... }
(in the Tables namespace), and
public class EditorGridPresentationModel:
EditorGridPresentationModelBase<Variable>
{ ... }
(in the Input namespace).
Kelly
On Wed, Mar 3, 2010 at 4:10 PM, Erick Thompson <[email protected]> wrote:
> I have run into a situation where I can't see any good solutions, and I'm
> hoping someone has run into a similar situation.
>
> I have a generic IRepository<T> interface which is used for loading data
> from a store. I have the generic parameter as I want to allow typed queries
> (the queries will end up hitting a ANDS service). I want to use classes
> implementing interface this in custom controls and ModelViews in a
> Silverlight 4 application.
>
> The problem is that you can't have a generic interface in a class without
> having a generic type on the class.
>
> public class ControlBase {
> protected IRepository<T> _repository;
> }
>
> will not compile. As this is a base class, I can't specify the T unless
> I use a inherited base DataClass. I would like to use POCOs if possible.
>
> In the fantasy world in my head, I would simply make the base class generic,
> and Bob's your uncle. However, I want these classes to be easily consumed in
> XAML, which doesn't have a good way to instantiate a generic type.
>
> What I ultimately want is for the user to be able to create the control/form
> (bound to ModelView) without a generic type, but that the data access occurs
> in a typed way. For example:
>
> public class CoolControl : ControlBase {
> public CoolControl() {
> _repository = IoC.Resolve<IRepository<SomeConcreteDataType>>();
> }
>
> public LoadData () {
> // do some typed IQueryable stuff on repository
> }
> }
>
> With this, the user can specify the control/ViewModel in XAML, and the
> control/ViewModel author gets the benefit of typing.
>
> Is there any good way to do this or I am stuck?
>
> Thanks,
> Erick
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Seattle area Alt.Net" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/altnetseattle?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Seattle area Alt.Net" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/altnetseattle?hl=en.