I'm applying strategy design pattern for data access mechanism. I have a technology-independent interface;
public interface IMyEntityDal
{
MyEntity Get();
bool Insert(MyEntity myEntity);
void Delete();
}
And I have 2 concrete data access class that inherit from IMyEntityDal interface.
public class SqlMyEntityDal : IMyEntityDal
{
MyEntity Get()
{
//some ef or ado.net code...
}
bool Insert(MyEntity myEntity)
{
//this method is not usable for this concrete class.
}
void Delete(MyEntity myEntity)
{
//some ef or ado.net code...
}
}
public class OracleMyEntityDal : IMyEntityDal
{
MyEntity Get()
{
//this method is not usable for this concrete class.
}
bool Insert(MyEntity myEntity)
{
//some oracle specific code...
}
void Delete(MyEntity myEntity)
{
//some oracle specific code...
}
}
The client that using IMyEntityDal doesn't know about IMyEntityDal implementation(Sql or Oracle). It injects concrete class with constructor.
public class Client
{
private IMyEntityDal _myEntityDal;
public Client(IMyEntityDal myEntityDal)
{
_myEntityDal = myEntityDal;
}
public void Do()
{
var myEntity = _myEntityDal.Get();
_myEntityDal.Insert(myEntity);
}
public void DeleteMyEntity(MyEntity myEntity)
{
_myEntity.Delete(myEntity);
}
I want to get MyEntity object from SQL and insert to ORACLE database. I don't want to implement Insert(MyEntity myEntity)
method for SqlMyEntityDal
concrete class. And I don't want to implement Get()
method for OracleMyEntityDal
concrete class. Also I want to implement Delete(MyEntity myEntity)
method for either two classes. In this situation, I can choose the concrete class for Delete()
method with constructor injection. But Client
is getting and inserting with same database. How can i design this mechanism?
Aucun commentaire:
Enregistrer un commentaire