Reputation: 31
I work on a MVC C# project and i have some classes that are always the same. What i would like to do is find a way to factorize the code so that i can implement it rapidly every time I need.
Here is an example of one of those classes
public class FeedbackManager
{
public List<Feedback> List = new List<Feedback>();
public FeedbackManager(){}
public List<Feedback> GetAll() { ... }
public Feedback GetById(string id) { ... }
public void Edit(string id, Feedback item) { ... }
public void Delete(string id) { ... }
public void Add(Feedback item) { ... }
public void Serialize() { ... }
public static FeedbackManager DeSerialize() { ... }
}
What i tried to do was to create an interface and change all the "Feedback" with "object" so that when I implement the interface I change "object" with "Feedback" but it didn't worked out. If you got some tricks to achieve such factorization i'd be interested
Upvotes: 0
Views: 197
Reputation: 1196
public interface IManager<T>
{
public List<T> GetAll();
public T GetById(string id);
public void Edit(string id, T item);
public void Delete(string id);
public void Add(T item);
public void Serialize();
}
or
public abstract class AbstractManager<T>
{
public List<T> List;
protected AbstractManager(){
List = new List<T>();
}
public abstract List<T> GetAll();
public abstract T GetById(string id);
public abstract void Edit(string id, T item);
public abstract void Delete(string id);
public abstract void Add(T item);
public abstract void Serialize();
}
Upvotes: 4
Reputation: 2268
As per the #1 comment on your answer it sounds exactly like a good use case for generics:
public interface IManager<T>
{
public List<T> List = new List<T>();
public List<T> GetAll() { ... }
public T GetById(string id) { ... }
public void Edit(string id, T item) { ... }
public void Delete(string id) { ... }
public void Add(T item) { ... }
public void Serialize() { ... }
}
something along these lines?
Upvotes: -2