0

I have a class like:

public class Soru
{ 
    public void SoruKaydet(){...}
    private static void SoruEtiketKaydet(Guid soruId, Etiket etiket, DbManager db){...}

    public Guid? SoruId { get; set; }
    public Guid? SoranId { get; set; }
    public int? BakilmaSayisi { get; set; }
    public string HtmlGovde { get; set; }
    public string MarkdownGovde { get; set; }
    public string Baslik { get; set; }
    public int? KategoriId { get; set; }
    public DateTime OlusturulmaTarihi { get; set; }

    public List<Etiket> Etiketler { get; set; }
}

I need another class should contain some of variables in class "Soru". I have two scenario for this.

The first scenario:

public class SoruSayfa
{ 
    public static List<SoruSayfa> SoruSayfaGetir(){...}   

    public Guid? SoruId { get; set; }
    public Guid? SoranId { get; set; }
    public int? BakilmaSayisi { get; set; }        
    public string Baslik { get; set; }
    public int? KategoriId { get; set; }
    public int DurumId { get; set; }
    public double SoruPuani { get; set; }
    public int CevapSayisi { get; set; }
    public int BakilmaSayisi { get; set; }
    public string KullaniciAdi { get; set; }
    public double? KisiAlanPuani { get; set; }
}

The second scenario:

public class SoruSayfa
{
    public static List<SoruSayfa> SoruSayfaGetir(){...}   

    // Refers the class Soru instead of some variables of it
    public Soru MySoru { get; set; }        
    public int DurumId { get; set; }
    public double SoruPuani { get; set; }
    public int CevapSayisi { get; set; }
    public int BakilmaSayisi { get; set; }
    public string KullaniciAdi { get; set; }
    public double? KisiAlanPuani { get; set; }
}

In first scenario, there is not extra variable which is not used, but in second scenario some variables of MySoru is not used(HtmlGovde, MarkdownGovde, OlusturulmaTarihi, Etiketler). In addition, Soru and SoruSayfa classes will be used as model for different actions in asp .net MVC. They contain different methods. Which scenario is better?

serefbilge
  • 1,654
  • 4
  • 29
  • 55
  • I could say the worst one is the first scenario, but this doesn't mean that the second is the best. You should better explain the usage context of the second class – Steve May 19 '13 at 09:31
  • 1
    Agreed: What are these classes? Are they supposed to be subclasses? And if they are, do they meet [Liskov substitution principle](http://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle)? – Rogier van het Schip May 19 '13 at 09:33
  • They are not sub classes, they will be used as model, in .net mvc to satisfy different goals. – serefbilge May 19 '13 at 09:37
  • @RogiervanhetSchip I'm searching about LSP, thanks. – serefbilge May 19 '13 at 11:09

1 Answers1

2

Try a third scenario ;)

public class SoruBase
{
    public Guid? SoruId { get; set; }
    public Guid? SoranId { get; set; }
    public int? BakilmaSayisi { get; set; }
    public string Baslik { get; set; }
    public int? KategoriId { get; set; }
}

public class Soru : SoruBase
{
    public string HtmlGovde { get; set; }
    public string MarkdownGovde { get; set; }
    public DateTime OlusturulmaTarihi { get; set; }

    public List<Etiket> Etiketler { get; set; }
}

public class SoruSayfa : SoruBase
{
    public int DurumId { get; set; }
    public double SoruPuani { get; set; }
    public int CevapSayisi { get; set; }
    public int BakilmaSayisi { get; set; }
    public string KullaniciAdi { get; set; }
    public double? KisiAlanPuani { get; set; }
}
Rafal
  • 1,081
  • 8
  • 10
  • What if there are more than two classes and they have shared properties but these properties are not shared by three of them(so there can not be a base class)? – serefbilge May 19 '13 at 09:50
  • You can these shared properties group in class and use that class as new property where you need. Try also combine single/multiple class inheritance with properties grouped in classes. – Rafal May 19 '13 at 10:03