खोज…


टिप्पणियों

IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter) के कार्यान्वयन के बारे में: इसका विचार सामान्य अनुरोध लिखने के लिए i => x.id == 17 जैसे एक्सप्रेशंस का उपयोग करना है। यह आपकी तकनीक की विशिष्ट क्वेरी भाषा का उपयोग किए बिना डेटा को क्वेरी करने का एक तरीका है। कार्यान्वयन अधिक व्यापक है, इसलिए आप अन्य विकल्पों पर विचार करना चाह सकते हैं, जैसे आपके लागू किए गए रिपॉजिटरी पर विशिष्ट तरीके: एक काल्पनिक CompanyRepository रिपॉजिटरी GetByName(string name) विधि प्रदान कर सकती है।

केवल-पढ़ने के लिए भंडार (C #)

निर्दिष्ट घटकों में डेटा भंडारण विशिष्ट कोड को एनकोड करने के लिए एक रिपॉजिटरी पैटर्न का उपयोग किया जा सकता है। आपके एप्लिकेशन का वह भाग, जिसे डेटा की आवश्यकता है, केवल रिपॉजिटरी के साथ काम करेगा। आप अपने द्वारा संग्रहित वस्तुओं और अपनी डेटाबेस तकनीक के प्रत्येक संयोजन के लिए एक भंडार बनाना चाहेंगे।

केवल रिपॉजिटरी का उपयोग करें रिपॉजिटरी बनाने के लिए उपयोग किया जा सकता है जो डेटा में हेरफेर करने की अनुमति नहीं है।

इंटरफेस

public interface IReadOnlyRepository<TEntity, TKey> : IRepository
{
    IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter);

    TEntity Get(TKey id);
}

public interface IRepository<TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
{
    TKey Add(TEntity entity);

    bool Delete(TKey id);

    TEntity Update(TKey id, TEntity entity);
}

प्रौद्योगिकी के रूप में ElasticSearch का उपयोग करके एक उदाहरण कार्यान्वयन (NEST के साथ)

public abstract class ElasticReadRepository<TModel> : IReadOnlyRepository<TModel, string>
    where TModel : class
{

    protected ElasticClient Client;

    public ElasticReadRepository()
    {
        Client = Connect();
    }

    protected abstract ElasticClient Connect();

    public TModel Get(string id)
    {
        return Client.Get<TModel>(id).Source;
    }

    public IEnumerable<TModel> Get(Expression<Func<TModel, bool>> filter)
    {
        /* To much code for this example */
        throw new NotImplementedException();
    }
}

public abstract class ElasticRepository<TModel>
    : ElasticReadRepository<TModel>, IRepository<TModel, string>
    where TModel : class
{
    public string Add(TModel entity)
    {
        return Client.Index(entity).Id;
    }

    public bool Delete(string id)
    {
        return Client.Delete<TModel>(id).Found;
    }

    public TModel Update(string id, TModel entity)
    {
        return Connector.Client.Update<TModel>(
            id,
            update => update.Doc(entity)
        ).Get.Source;
    }
}

इस कार्यान्वयन का उपयोग करके, अब आप उन वस्तुओं के लिए विशिष्ट रिपोजिटरी बना सकते हैं, जिन्हें आप स्टोर या एक्सेस करना चाहते हैं। लोचदार खोज का उपयोग करते समय, यह सामान्य है, कि कुछ घटकों को केवल डेटा पढ़ना चाहिए, इस प्रकार केवल-पढ़ने के लिए रिपॉजिटरी का उपयोग किया जाना चाहिए।

इकाई रूपरेखा (C #) का उपयोग करते हुए रिपॉजिटरी पैटर्न

रिपॉजिटरी इंटरफ़ेस;

public interface IRepository<T>
{
    void Insert(T entity);
    void Insert(ICollection<T> entities);
    void Delete(T entity);
    void Delete(ICollection<T> entity);
    IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
    IQueryable<T> GetAll();
    T GetById(int id);
}

सामान्य भंडार;

public class Repository<T> : IRepository<T> where T : class
{
    protected DbSet<T> DbSet;

    public Repository(DbContext dataContext)
    {
        DbSet = dataContext.Set<T>();
    }

    public void Insert(T entity)
    {
        DbSet.Add(entity);
    }

    public void Insert(ICollection<T> entities)
    {
        DbSet.AddRange(entities);
    }

    public void Delete(T entity)
    {
        DbSet.Remove(entity);
    }

    public void Delete(ICollection<T> entities)
    {
        DbSet.RemoveRange(entities);
    }

    public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public T GetById(int id)
    {
        return DbSet.Find(id);
    }
}

उदाहरण का उपयोग डेमो होटल वर्ग का उपयोग करके किया जाता है;

var db = new DatabaseContext();
var hotelRepo = new Repository<Hotel>(db);

var hotel = new Hotel("Hotel 1", "42 Wallaby Way, Sydney");
hotelRepo.Insert(hotel);
db.SaveChanges();


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow