Kodni takrorlang

Juda o'xshash bo'lgan ikkita uslub bilan bir sinf bor:

public class MyClass
{
    public MyClass ()
    {

    }

    public IQueryable PersonSelect1()
    {
        var ctx = new MyContextSQL();
        var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
            (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
            && (x.department != 90 && x.department != 94)//only difference
            && (x.workID == 789 || x.workID == 567)
            && (x.clientID != 789247)
            && (x.auditID != 9)
            && (x.expDate >= new DateTime(2017, 1, 1))
            && (x.expDate <= DateTime.Today));
        var result = temp.OrderByDescending(x => x.expDate);
        return result;
    }

    public IQueryable PersonSelect2()
    {
        var ctx = new MyContextSQL();
        var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
            (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
            && (x.department == 90 || x.department == 94) //only difference
            && (x.workID == 789)
            && (x.clientID != 789247)
            && (x.auditID != 9)
            && (x.expDate >= new DateTime(2017, 1, 1))
            && (x.expDate <= DateTime.Today));
        var result = temp.OrderByDescending(x => x.expDate);
        return result;
    }
}

PersonSelect1() va PersonSelect2() o'rtasida farq qiladigan bitta satr mavjud. Buning qanday ko'rinishini yoqtirmayman, chunki kod juda ko'p. Buni quyidagi kabi hal qilishni o'ylayman:

  public IQueryable ThirdMethod()
    {
        var ctx = new MyContextSQL();
        var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
            (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)        
            && (x.workID == 789)
            && (x.clientID != 789247)
            && (x.auditID != 9)
            && (x.expDate >= new DateTime(2017, 1, 1))
            && (x.expDate <= DateTime.Today));
        var result = temp.OrderByDescending(x => x.expDate);
        return result;
    }

    public IQueryable PersonSelect1()
    {
        var result = this.ThirdMethod().Where(x => (x.department != 90 || x.department != 94));     
        return result;
    }

    public IQueryable PersonSelect2()
    {
        var result = this.ThirdMethod().Where(x => (x.department == 90 || x.department == 94));
        return result;
    }

ThirdMethod() PersonSelect1() yoki PersonSelect2() dan ma'lumotlar bazasiga ulanadi deb hisoblayotgan eng yaxshi yondashuvmi? Ushbu to'plamga qarshi ThirdMethod() chaqiruvidan kelib chiqdimi? Men ishlashni o'ylayapman va bir vaqtning o'zida takrorlanadigan kodni chetlab o'tishga harakat qilaman.

1
Kodning qadimgi versiyasini joylashtirdim va u haqda hech narsa bilmas edim, hozirda bu yerda yangilanadi, rahmat
qo'shib qo'ydi muallif AlexGH, manba
@maccettura Bu ajoyib g'oya va mening savolimda qayd etilayotgan yondashuvdan ko'ra sizning fikringizni ko'proq yoqtiraman
qo'shib qo'ydi muallif AlexGH, manba
@maccettura Bu ajoyib g'oya va mening savolimda qayd etilayotgan yondashuvdan ko'ra sizning fikringizni ko'proq yoqtiraman
qo'shib qo'ydi muallif AlexGH, manba
Nima uchun umumiy parametrlarga ega SQL ko'rinishini yaratmaysiz, shuning uchun C# kodi faqat ko'rinish va filtrlarni x.department! = 90 || bilan ataydi. x.department! = 94 / x.department! = 90 || x.department == 94
qo'shib qo'ydi muallif maccettura, manba
Nima uchun umumiy parametrlarga ega SQL ko'rinishini yaratmaysiz, shuning uchun C# kodi faqat ko'rinish va filtrlarni x.department! = 90 || bilan ataydi. x.department! = 94 / x.department! = 90 || x.department == 94
qo'shib qo'ydi muallif maccettura, manba
Men sizning sharhingizni siz uchun ishlab chiqilgan taqdirda javob sifatida yubordim.
qo'shib qo'ydi muallif maccettura, manba
Men sizning sharhingizni siz uchun ishlab chiqilgan taqdirda javob sifatida yubordim.
qo'shib qo'ydi muallif maccettura, manba
Men sizning sharhingizni siz uchun ishlab chiqilgan taqdirda javob sifatida yubordim.
qo'shib qo'ydi muallif maccettura, manba
&& (x.department! = 90 || x.department! = 94)//faqat farq
qo'shib qo'ydi muallif Jianping Liu, manba

7 javoblar

Quyidagi o'zgarmaydigan nomlar qo'rqinchli va sintaksisiz o'chirilgan bo'lishi mumkin (bir necha yillar davomida .net-dan uzoqda bo'lganman), lekin, albatta, eng oddiy narsa, ba'zi bir parametrlardan tashqari ThirdMethod shunga o'xshash so'rov:

public IQueryable ThirdMethod(bool isFirstPerson)
{
    var ctx = new MyContextSQL();
    var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
        (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9) 
        && isfirstPerson ? 
             (x.department != 90 && x.department != 94) : (x.department == 90 || x.department == 94)    
        && (x.workID == 789)
        && (x.clientID != 789247)
        && (x.auditID != 9)
        && (x.expDate >= new DateTime(2017, 1, 1))
        && (x.expDate <= DateTime.Today));
    var result = temp.OrderByDescending(x => x.expDate);
    return result;
}

public IQueryable PersonSelect1()
{
    return this.ThirdMethod(true);
}

public IQueryable PersonSelect2()
{
    return this.ThirdMethod(false);
}
1
qo'shib qo'ydi

Quyidagi o'zgarmaydigan nomlar qo'rqinchli va sintaksisiz o'chirilgan bo'lishi mumkin (bir necha yillar davomida .net-dan uzoqda bo'lganman), lekin, albatta, eng oddiy narsa, ba'zi bir parametrlardan tashqari ThirdMethod shunga o'xshash so'rov:

public IQueryable ThirdMethod(bool isFirstPerson)
{
    var ctx = new MyContextSQL();
    var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
        (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9) 
        && isfirstPerson ? 
             (x.department != 90 && x.department != 94) : (x.department == 90 || x.department == 94)    
        && (x.workID == 789)
        && (x.clientID != 789247)
        && (x.auditID != 9)
        && (x.expDate >= new DateTime(2017, 1, 1))
        && (x.expDate <= DateTime.Today));
    var result = temp.OrderByDescending(x => x.expDate);
    return result;
}

public IQueryable PersonSelect1()
{
    return this.ThirdMethod(true);
}

public IQueryable PersonSelect2()
{
    return this.ThirdMethod(false);
}
1
qo'shib qo'ydi

Dasturni mantiqiy baham ko'rishga urinishning o'rniga, mantiqni almashadigan va sizning C# -ga faqat filtrni bajaradigan SQL ko'rinishini yaratishingiz mumkin.

Bundan tashqari, LINQ2SQL ga nisbatan tezroq bajariladigan ishning samarasi oshiriladi.

1
qo'shib qo'ydi

Dasturni mantiqiy baham ko'rishga urinishning o'rniga, mantiqni almashadigan va sizning C# -ga faqat filtrni bajaradigan SQL ko'rinishini yaratishingiz mumkin.

Bundan tashqari, LINQ2SQL ga nisbatan tezroq bajariladigan ishning samarasi oshiriladi.

1
qo'shib qo'ydi

Ishonchim komil emas, lekin shunday qila olasizmi?

public IQueryable PersonSelect()
{
    var ctx = new MyContextSQL();
    var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
        (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
        && (x.workID == 789 || x.workID == 567)
        && (x.clientID != 789247)
        && (x.auditID != 9)
        && (x.expDate >= new DateTime(2017, 1, 1))
        && (x.expDate <= DateTime.Today));
    var result = temp.OrderByDescending(x => x.expDate);
    return result;
}

public IQueryable PersonSelect1()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department != 90 && x.department != 94);
    return result;
}

public IQueryable PersonSelect2()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department == 90 && x.department == 94);
    return result;
}
0
qo'shib qo'ydi

Ishonchim komil emas, lekin shunday qila olasizmi?

public IQueryable PersonSelect()
{
    var ctx = new MyContextSQL();
    var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
        (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
        && (x.workID == 789 || x.workID == 567)
        && (x.clientID != 789247)
        && (x.auditID != 9)
        && (x.expDate >= new DateTime(2017, 1, 1))
        && (x.expDate <= DateTime.Today));
    var result = temp.OrderByDescending(x => x.expDate);
    return result;
}

public IQueryable PersonSelect1()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department != 90 && x.department != 94);
    return result;
}

public IQueryable PersonSelect2()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department == 90 && x.department == 94);
    return result;
}
0
qo'shib qo'ydi

Ishonchim komil emas, lekin shunday qila olasizmi?

public IQueryable PersonSelect()
{
    var ctx = new MyContextSQL();
    var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
        (x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
        && (x.workID == 789 || x.workID == 567)
        && (x.clientID != 789247)
        && (x.auditID != 9)
        && (x.expDate >= new DateTime(2017, 1, 1))
        && (x.expDate <= DateTime.Today));
    var result = temp.OrderByDescending(x => x.expDate);
    return result;
}

public IQueryable PersonSelect1()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department != 90 && x.department != 94);
    return result;
}

public IQueryable PersonSelect2()
{
    var ctx = PersonSelect();
    ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
         (x.department == 90 && x.department == 94);
    return result;
}
0
qo'shib qo'ydi