开发者

need to figure out T of class Repository is for

I have class Repository, and based on T i want to create objectset for the type T..in my constructor...this is what i have so far..

public static class Repository<T> : IDisposable
    where T : class
{
    private DeltaDBEntities con开发者_JS百科text;
    private ObjectSet<T> objectset;
    public Repository()
    {
        this.context =  new DeltaDBEntities();
        switch
        {
            case T = typeof(ViewModels.Company):
                this.objectset = context.Companies;
                break;
        }

        this.objectset = context.Set
    }


If your DeltaDBEntities inherits from ObjectContext, you can use its generic method CreateObjectSet<T>().

public class Repository<T> where T : class
{
    private DeltaDBEntities context;
    private ObjectSet<T> objectset;

    public Repository()
    {
        this.context = new DeltaDBEntities();
        this.objectset = context.CreateObjectSet<T>();
    }
}


Not sure what you are trying to achieve and why are you using generics for something that is not known at compile time but you may try the following:

public class Repository<T> : IDisposable where T : class
{
    private DeltaDBEntities context;
    private ObjectSet<T> objectset;

    public Repository()
    {
        this.context = new DeltaDBEntities();
        if (typeof(T) == typeof(ViewModels.Company))
        {
            this.objectset = context.Companies;
        }
        else if (typeof(T) == typeof(ViewModels.SomeOtherClass))
        {
            this.objectset = context.SomethingElse;
        }
        else
        {
            this.objectset = context.Set;
        }
    }

    public void Dispose()
    {
        // TODO: implement IDisposable
    }
}

Also note that a static class cannot implement an interface so Repository<T> should not be static.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜