Generic Reference Tables API

Tassavur qilin katta (enterprise) tizim yaratayapsiz. Va albatta sizda ma'lumot jadvali (spravochnik rus tilida, reference table ingliz tilida) juda ko'p bo'ladi. Va shu jadvallarni combobox, lookup yoki listbox controllarga chiqarishingizga to'g'ri keladi. Shunda har biriga API yozishim kerak deb bosh (asosan barmoqlarni) og'ritishga to'g'ri keladi.

Dasturchilar o'zining ishyoqmasligi va ixtirochiligi bilan lol qoldirish mahoratiga ega. Shuning uchun har biriga alohida API yozishdan ko'ra Generic umumiy bitta yozib ko'ramiz. Lekin ulangan jadvallar bo'lsa ularga bari bir alohida yozishga to'g'ri keladi. Chunki ulangan ustunlar nomi va usuli bir-biridan farq qilishi mumkin.

Generic API yozishda ORM Entity Frameworkdagi Code First usulidan foydalanamiz. Buning uchun biz hamma nasl oladigan ota sinfni yaratib olamiz:

/// <summary>
/// Базовый класс для справочников
/// </summary>
internal abstract class DbReferenceBaseModel
    {
        [Column("id")]
        public Int32 Id { get; set; }

        [Column("name0_ru")]
        public virtual string NameRu { get; set; }

        [Column("name0_uz")]
        public virtual string NameUzCyr { get; set; }

        [Column("name0_uzlat")]
        public virtual string NameUzLat { get; set; }

        [Column("name0_kar")]
        public virtual string NameKarCyr { get; set; }

        [Column("name0_karlat")]
        public virtual string NameKarLat { get; set; }

        [Column("name0_lat")]
        public virtual string NameEn { get; set; }

        [Column("active")]
        public byte Active { get; set; }

        [Column("level")]
        public Int32? Level { get; set; }

        [Column("firstid")]
        public Int32? FirstId { get; set; }

        [Column("parentfirstid")]
        public Int32? ParentFirstId { get; set; }

        [Column("parent")]
        public Int32? Parent { get; set; }

        [Column("iscode")]
        public String IsCode { get; set; }

        public object this[string propertyName]
        {
            get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
            set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
        }
}

Qolgan bola sinflarni esa quyidagicha e'lon qilamiz:

/// <summary>
/// Классификатор стран мира
/// </summary>
[Table("*******")]
internal class DbCountry : DbReferenceBaseModel
    {
    }


Va albatta DbContextga bola sinfni qo'shamiz:

class ReferenceContext : DbContext
    {
        static ReferenceContext()
        {
            Database.SetInitializer<ReferenceContext>(null);
        }
        public ReferenceContext()
        : base("RefDb")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }
        public DbSet<DbCountry> CountrySet { get; set; }
     }

Endi eng asosiysi Generic sinf yozamiz:

public class ReferenceLookUpRepository : IRepositoryBase
    {
    /// <summary>
    /// Точка входа
    /// </summary>
    /// <param name="name">Название класса</param>
    /// <returns></returns>
    public IEnumerable<DbReferenceBaseModel> GetReferenceList(String name)
    {
        var t = Assembly.GetAssembly(typeof(ReferenceLookUpRepository)).GetType("bola sinfi yotgan namespace" + "." + name, false, true);
        var a = GetType().GetMethod("GetReferenceListFromDb", BindingFlags.NonPublic | BindingFlags.Instance);
        var b = a.MakeGenericMethod(t);
        var c = b.Invoke(this, null);
        return c as IEnumerable<DbReferenceBaseModel>;
     }
    /// <summary>
    /// Справочники
    /// </summary>
    /// <returns></returns>
   private IEnumerable<DbReferenceBaseModel> GetReferenceListFromDb<T>() where T : DbReferenceBaseModel
     {
        using (var context = new ReferenceContext())
        {
            return _context.Set<T>().Where(w => w.Active == 1).ToList();
        }
     }
    }

Bu sinfni ishlatish esa juda oson.

var repo = new ReferenceLookUpRepository();
var countries = repo.GetReferenceList("DbCountry");

Balkim kimgadir kerak bo'lib qolar degan niyyatda yozdim. Sizda ham shunday generic sinflar bo'lsa yozing kutib qolamiz.


ts

Muallif haqida

Mirzaev Aziz Ibrakhimovich


Blogdagi so‘nggi maqolalar:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!