Generic Reference Tables API

Generic Reference Tables API

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:

/// 
/// Базовый класс для справочников
/// 
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:

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

Va albatta DbContextga bola sinfni qo’shamiz:

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

Endi eng asosiysi Generic sinf yozamiz:

public class ReferenceLookUpRepository : IRepositoryBase
    {
    /// 
    /// Точка входа
    /// 
    /// Название класса
    /// 
    public IEnumerable 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;
     }
    /// 
    /// Справочники
    /// 
    /// 
   private IEnumerable GetReferenceListFromDb() where T : DbReferenceBaseModel
     {
        using (var context = new ReferenceContext())
        {
            return _context.Set().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.

Umumiy Dasturlash
Generic Reference Tables API