Need help understanding Generics, How To Abstract Types Question.

Posted by kmacmahon on Stack Overflow See other posts from Stack Overflow or by kmacmahon
Published on 2010-03-08T00:03:41Z Indexed on 2010/03/08 0:12 UTC
Read the original article Hit count: 698

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()

And its creation this way:

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

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

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

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

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

© Stack Overflow or respective owner

Need help understanding Generics, How To Abstract Types Question.

Posted by kmacmahon on Stack Overflow See other posts from Stack Overflow or by kmacmahon
Published on 2010-03-08T00:03:41Z Indexed on 2010/03/08 10:51 UTC
Read the original article Hit count: 698

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?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about generics