Is it possible to simplify this common C # code and pass only one type?

The following code from my teammate works:

public T Get<T, V>(V repo, string pk, string rk) where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity where V : IAzureTable<T> { try { var item = repo.GetPkRk(pk, rk); if (item == null) throw new Exception(); return (T)item; } catch (Exception ex) { _ex.Errors.Add("", typeof(T).Name + rk + " does not exist"); throw _ex; } } 

Call Code:

 var account = base.Get<Account, IAzureTable<Account>>(_accountRepository, pk, rk); 

Can this be simplified. The only type variable here is โ€œAccountโ€, and I wonder if it is possible to combine the types T and V into one, since V depends only on T.

+4
source share
3 answers

Yes

 public T Get<T>(IAzureTable<T> repo, string pk, string rk) { ... } 

then

 var account = base.Get<Account>(_accountRepository, pk, rk); 

you could even leave with

 var account = base.Get(_accountRepository, pk, rk); 

if the compiler can infer the parameter type from _accountRepository .

+2
source

Since the only place that V appears in the signature, I would think you could change it to:

 public T Get<T>(IAzureTable<T> repo, string pk, string rk) where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity 

But I do not have a sample code that could be verified.

+5
source
 public T Get<T>(IAzureTable<T> repo, string pk, string rk) where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity 
+3
source

Source: https://habr.com/ru/post/1387173/


All Articles