开发者

C# Generics - Can I make this class into a generic?

I have a class repeated many times. My boss ask me to make to generic but I'm not sure what this means. Can anyone out help or suggest how? Sorry for bad English. I hope it make sense.

public class CapacityServiceContext : TableServiceContext
{
    public CapacityServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public const string TableName = "Capacity";
    public IQueryable<Capacity> CapacityTable
    {
        get
        {
            return this.CreateQuery<Capacit开发者_开发百科y>(TableName);
        }
    }

}

thank you in advance.

Mina


As an example, your code could probably look like this:

public class ServiceContext<T> : TableServiceContext
{
    public ServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public const string TableName = typeof(T).Name;
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(TableName);
        }
    }
}

And would be instantiated like any generic class:

ServiceContext<Capacity> context = new ServiceContext<Capacity>(…);

Essentially, I have changed every occurrence of Capacity in your class into T and made the class generic by appending <T> to the class name. Now the class is usable for other types as well.


You can make TableServiceContext<T>, but your problem is going to be with your TableName constant. Unless you can guarantee that the table name is always the same name as the class of your generic, then it doesn't look good.

But if you can make that guarantee, it would look something like this:

public class TableServiceContext<T>
{
    public TableServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public string TableName { get { return typeof(T).Name; } }
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(TableName);
        }
    }
}


Generics are very popular and can help make your code more reusable.

Here is a general overview, you can decide how to best use it in your projects.

http://msdn.microsoft.com/en-us/library/512aeb7t(v=VS.100).aspx


I would gues your boss wants something like the following:

public class ServiceContext<T> : TableServiceContext
{
    public ServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(typeof(T).Name);
        }
    }

}

You should ask him/her to be sure though, you'd use this class as follows:

ServiceContext<Capacity> serviceContext = new ServiceContext<Capacity>();
IQueryable<Capacity> query = serviceContext.Table;


I'm assuming he is suggesting that you make it generic with regard to Capacity.

public class ServiceContext<T> : TableServiceContext {

...

public IQueryable<T> Table {        
  get {            
    return this.CreateQuery<T>(typeof(T).Name);       
  }    
}

Then you could create a:

ServiceContext<Capacity>()

Without seeing the other classes, I'm not sure what would really be involved.


Possible he asked about changing CapacityTable property:

...
public IQueryable<T> Table
{
    get
    {
        return this.CreateQuery<T>(typeof(T).Name);
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜