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?

4
  • 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 Commented May 19, 2013 at 9:31
  • 1
    Agreed: What are these classes? Are they supposed to be subclasses? And if they are, do they meet Liskov substitution principle? Commented May 19, 2013 at 9:33
  • They are not sub classes, they will be used as model, in .net mvc to satisfy different goals. Commented May 19, 2013 at 9:37
  • @RogiervanhetSchip I'm searching about LSP, thanks. Commented May 19, 2013 at 11:09

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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)?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.