Reputation: 1036
I try to extract some operation into an interface with some additional property like this :
Base Entity Class:
public enum MyClassEnum
{
classA,
classB,
classC
}
public abstract class MyBaseClass
{
public string entityA { get;set; }
public int entityB { get; set; }
}
And then I have some class that Derive from MyBaseClass:
public class ClassA : MyBaseClass{}
public class ClassB : MyBaseClass
{
public string AnotherEntity { get; set; }
}
public class ClassC : MyBaseClass
{
public string AnotherEntity2 { get; set; }
public string AnotherEntity3 { get; set; }
}
I try to have an interface like this (this is I got so far):
public interface IMyClassRepository
{
void Create(MyBaseClass param);
void Update(MyBaseClass param);
}
And the concrete class it should be like this:
public class ClassBRepository : IMyClassRepository
{
private readonly BaseRepository _baseRepository;
public ClassBRepository
{
_baseRepository = new BaseRepository();
}
public void Create(MyBaseClass param)
{
// will use automapper to do the mapping
var theClassB = MyBaseClass.TranslateTo<ClassB>();
// How i can design the interface that accept additional Property on class B?
// last save the operation to DB..
_baseRepository.Save(theClassB);
}
}
UPDATE
I will create IMyClassRepository
use Factory
like this:
public class MyClassFactory
{
public IMyClassRepository CreateInstance(MyClassEnum param)
{
switch(param)
{
case MyClassEnum.ClassA
{
return new ClassARepository;
}
case MyClassEnum.ClassB
{
return new ClassBRepository;
}
case MyClassEnum.ClassC
{
return new ClassCRepository;
}
}
}
}
and in the end have operation class like this:
public class ConcreteOperationClass
{
private IMyClassRepository _myClass;
public ConcreteOperationClass(MyClassEnum param)
{
_myClass = new MyClassFactory().CreateInstance(param);
}
public void CreateMyClass(MyBaseClass param, // this should be a parameter class B or Class C needed)
{
_myClass.Create(param, // additional property for classB or classC);
}
}
The Problem is how I create the implementation of IMyClassRepository
with additional property from ClassB
or ClassC
?
Upvotes: 1
Views: 114
Reputation: 14850
If I understand right, you're trying to design something similar to a repository pattern? If that's the case, you could create a repository implementation for all your entities. For this, you will need to create a generic interface that works with concrete implementations of your entities...
public interface IMyClassRepository<T> where T : MyBaseClass
{
T Get(int id);
void Create(T param);
void Update(T param);
}
Then create an implementation of this repository for each entity...
public ClassARepo : IMyClassRepository<ClassA>{...}
And then you could create a factory object that serves these concrete implementations on the fly. Something like...
var repository = RepositoryFactory.Resolve<ClassB>();
ClassB entity = repository.Get(234);
entity.entityA = "new value";
repository.Update(entity);
You don't even have to implement it yourself if you use a D/I Container such as Windsor Castle where you can define implementations and services through config file, and even swap them without having to recompile the whole app.
Upvotes: 2