नमूना कार्यक्रम:क्या आप इस जेनेरिक व्यवहार को समझा सकते हैं और यदि मेरे पास कोई कामकाज है?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericsTest
{
class Program
{
static void Main(string[] args)
{
IRetrievable<int, User> repo = new FakeRepository();
Console.WriteLine(repo.Retrieve(35));
}
}
class User
{
public int Id { get; set; }
public string Name { get; set; }
}
class FakeRepository : BaseRepository<User>, ICreatable<User>, IDeletable<User>, IRetrievable<int, User>
{
// why do I have to implement this here, instead of letting the
// TKey generics implementation in the baseclass handle it?
//public User Retrieve(int input)
//{
// throw new NotImplementedException();
//}
}
class BaseRepository<TPoco> where TPoco : class,new()
{
public virtual TPoco Create()
{
return new TPoco();
}
public virtual bool Delete(TPoco item)
{
return true;
}
public virtual TPoco Retrieve<TKey>(TKey input)
{
return null;
}
}
interface ICreatable<TPoco> { TPoco Create(); }
interface IDeletable<TPoco> { bool Delete(TPoco item); }
interface IRetrievable<TKey, TPoco> { TPoco Retrieve(TKey input); }
}
यह नमूना कार्यक्रम इंटरफेस मेरी वास्तविक कार्यक्रम का उपयोग करता है का प्रतिनिधित्व करता है, और (FakeRepository
में बाहर टिप्पणी की) समस्या मैं आ रही हैं दर्शाता है। मैं बेस क्लास द्वारा सामान्य रूप से संभालने के लिए इस विधि कॉल को कॉल करना चाहता हूं (जो कि मेरे वास्तविक उदाहरण में दिए गए मामलों में से 9 5% को संभालने में सक्षम है), जिससे टीके के प्रकार को स्पष्ट रूप से निर्दिष्ट करके बाल कक्षाओं में ओवरराइड की अनुमति मिलती है। यह कोई फर्क नहीं पड़ता कि मैं iretrievable के लिए उपयोग की जाने वाली पैरामीटर बाधाओं का उपयोग करता हूं, मैं बेस क्लास के माध्यम से कभी भी कॉल कॉल नहीं कर सकता।
इसके अलावा, अगर कोई इस तरह के व्यवहार को लागू करने के लिए वैकल्पिक तरीका देख सकता है और परिणाम प्राप्त कर रहा हूं, तो मैं इसे देखकर बहुत दिलचस्पी लेता हूं।
विचार?
जोड़ने के लिए एक चीज, और दुर्भाग्य से मैंने इसे मूल उदाहरण में शामिल नहीं किया है, यह है कि मेरे FakeRepository में कई आईआरटीरिवेबल इंटरफेस परिभाषित हो सकते हैं, सभी एक ही टीपीओओ के साथ, लेकिन विभिन्न टीकेई। –