Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the better C# class design for dealing with read+write versus readonly

I'm contemplating two different class designs for handling a situation where some repositories are read-only while others are read-write. (I don't foresee any need for a write-only repository.)


Class Design 1 -- provide all functionality in a base class, then expose applicable functionality publicly in sub classes

public abstract class RepositoryBase
{
    protected virtual void SelectBase() { // implementation... }
    protected virtual void InsertBase() { // implementation... }
    protected virtual void UpdateBase() { // implementation... }
    protected virtual void DeleteBase() { // implementation... }
}

public class ReadOnlyRepository : RepositoryBase
{
    public void Select() { SelectBase(); }
}

public class ReadWriteRepository : RepositoryBase
{
    public void Select() { SelectBase(); }
    public void Insert() { InsertBase(); }
    public void Update() { UpdateBase(); }
    public void Delete() { DeleteBase(); }
}

Class Design 2 - read-write class inherits from read-only class

public class ReadOnlyRepository
{
    public void Select() { // implementation... }
}

public class ReadWriteRepository : ReadOnlyRepository
{
    public void Insert() { // implementation... }
    public void Update() { // implementation... }
    public void Delete() { // implementation... }
}

Is one of these designs clearly stronger than the other? If so, which one and why?

P.S. If this sounds like a homework question, it's not, but feel free to use it as one if you want :)

like image 356
devuxer Avatar asked May 10 '10 18:05

devuxer


People also ask

What version of C is best?

Yes, it's a bit odd that you can get a loud consensus that K&R is a great C book, and also a loud consensus that C99 is the correct/current/best version of C.

Which is better C or C+?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

Which is best C or Python?

Ease of development – Python has fewer keywords and more free English language syntax whereas C is more difficult to write. Hence, if you want an easy development process go for Python. Performance – Python is slower than C as it takes significant CPU time for interpretation. So, speed-wise C is a better option.

Which is easier C or C++?

Answers: Actually, both are difficult and both are easy. C++ is built upon C and thus supports all features of C and also, it has object-oriented programming features. When it comes to learning, size-wise C is smaller with few concepts to learn while C++ is vast. Hence we can say C is easier than C++.


1 Answers

How about a third option, closely related to the first, but using interfaces instead:

public interface IReadRepository {
    public void Select();
}

public interface IWriteRepository {
    public void Insert();
    public void Update();
    public void Delete();
}

// Optional
public interface IRepository : IReadRepository, IWriteRepository {
}

public class Repository : IRepository {
   // Implementation
}

This way the implementation is (or can be) all in one place, and the distinction is made only by which interface you are looking at.

like image 128
Eric Petroelje Avatar answered Oct 06 '22 23:10

Eric Petroelje