views:

64

answers:

2

I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project.

Given this class constructor:

    public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
        where TDomainContext : DomainContext, new()
        where TEntity : Entity, new()

    public SecuredDomainViewModel(TDomainContext domainContext, ProtectedItem protectedItem)
            : base(domainContext)
        {
            this.protectedItem = protectedItem;
        }

And its creation this way:

                DomainViewModel d;
                d = new SecuredDomainViewModel<MyContext, MyEntityType>(this.context, selectedProtectedItem);

Assuming I have 20 different EntityTypes within MyContext, is there any easier way to call the constructor without a large switch statement?

Also, since d is DomainViewModel and I later need to access methods from SecuredDomainViewModel, it seems I need to do this:

if (((SecuredDomainViewModel<MyContext, MyEntityType>)d).CanEditEntity)

But again "MyEntityType" could actually be one of 20 diffent types. Is there anyway to write these types of statements where MyEntityType is returned from some sort of Reflection?

Additional Info for Clarification: I will investigate ConstructorInfo, but I think I may have incorrectly described what I'm looking to do.

Assume I have the DomainViewModel, d in my original posting.

This may have been constructed via three possible ways:

d = new SecuredDomainViewModel<MyContext, Order>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Invoice>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Consumer>(this.context, selectedProtectedItem);

Later, I need to access methods on the SecuredDomainViewModel, which currently must be called this way:

ex: if (((SecuredDomainViewModel<MyContext, Order)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Invoice)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Consumer)d).CanEditEntity)

Assuming I have N+ entity types in this context, what I was hoping to be able to do is something like this with one call:

ex: if (((SecuredDomainViewModel<MyContext, CurrentEntityType)d).CanEditEntity)

Where CurrentEntityType was some sort of function or other type of call that returned Order, Invoice or Consumer based on the current item entity type.

Is that possible?

A: 

I can't help on the links, and likely not on the type either.

Constructor

If you have the Type, you can get the constructor:

ConstructorInfo construtor = typeof(MyEntityType).GetConstructor(new object[]{TDomainContext, ProtectedItem});

Type

I'm not really sure what you're looking for, but I can only see something like

if (((SecuredDomainViewModel<MyContext, entityType>)d).CanEditEntity)
{
    entityType=typeof(Orders)
}

being what you want.

ANeves
I added more information to clarify what I am trying to do.
kmacmahon
This was clearly nothing of what you were looking for, my apologies. Your approach of extending an interface sounds solid and sensible. (Uhm, I assume you got the generic interface method working? Ask for help on it, otherwise.)
ANeves
Yup, we did! Thanks so much for taking the time to review this question tho. -K
kmacmahon
+1  A: 

You can create a non-generic interface that has the CanEditEntity property on it, make SecuredDomainViewModel inherit off that, then call the property through the interface...

Also, the new() constructor allows you to call a constructor on a generic type that has no arguments (so you can just write new TEntity()), but if you want to call a constructor that has parameters one handy trick I use is to pass it in as a delegate:

public void Method<T>(Func<string, bool, T> ctor) {
    // ...
    T newobj = ctor("foo", true);
    // ...
}

//called later...
Method((s, b) => new MyClass(s, b));
thecoop
We have decided to refactor the class to use expose an interface.
kmacmahon