Created
June 6, 2013 13:06
-
-
Save yemrekeskin/5721357 to your computer and use it in GitHub Desktop.
Practices about Data Access Object Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Account | |
| { | |
| // set fields and properties | |
| } | |
| public class Customer | |
| { | |
| // set fields and properties | |
| } | |
| public interface IAccount | |
| { | |
| int CreateAccount(Account account); | |
| bool DeleteAccount(int accountId); | |
| bool UpdateAccount(Account account); | |
| List<Account> GetAccount(); | |
| } | |
| public interface ICustomer | |
| { | |
| int CreateCustomer(Customer customer); | |
| bool DeleteCustomer(int customerId); | |
| bool UpdateCustomer(Customer customer); | |
| List<Customer> GetCustomers(); | |
| bool DeleteAccounts(int customerID); | |
| } | |
| // Data Account Object for sql connect | |
| public class SqlAccountDAO | |
| : IAccount | |
| { | |
| public int CreateAccount(Account account) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteAccount(int accountId) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool UpdateAccount(Account account) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public List<Account> GetAccount() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } | |
| public class SqlCustomerDAO | |
| : ICustomer | |
| { | |
| public int CreateCustomer(Customer customer) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteCustomer(int customerId) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool UpdateCustomer(Customer customer) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public List<Customer> GetCustomers() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteAccounts(int customerID) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } | |
| // Data Account Object for oracle connect | |
| public class OracleAccountDAO | |
| : IAccount | |
| { | |
| public int CreateAccount(Account account) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteAccount(int accountId) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool UpdateAccount(Account account) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public List<Account> GetAccount() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } | |
| public class OracleCustomerDAO | |
| : ICustomer | |
| { | |
| public int CreateCustomer(Customer customer) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteCustomer(int customerId) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool UpdateCustomer(Customer customer) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public List<Customer> GetCustomers() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public bool DeleteAccounts(int customerID) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } | |
| // Data Access Object Factory | |
| public abstract class DAOFactory | |
| { | |
| public abstract ICustomer GetCustomerDao(); | |
| public abstract IAccount GetAccountDao(); | |
| public static DAOFactory GetDaoFactory() | |
| { | |
| string strDaoFactoryType = ConfigurationManager.AppSettings["DAOFactory"]; | |
| if (String.IsNullOrEmpty(strDaoFactoryType)) | |
| throw new NullReferenceException(""); | |
| else | |
| { | |
| Type DaoFactoryType = Type.GetType(strDaoFactoryType); | |
| if (DaoFactoryType == null) | |
| throw new NullReferenceException(""); | |
| Type DaoFactoryBaseType = | |
| Type.GetType("Sample.DataAccessLayer.DAOFactory"); | |
| if (!DaoFactoryBaseType.IsAssignableFrom(DaoFactoryType)) | |
| throw new ArgumentException(""); | |
| DAOFactory daoFactory = (DAOFactory)Activator.CreateInstance(DaoFactoryType); | |
| return daoFactory; | |
| } | |
| } | |
| } | |
| public class SqlDAOFactory | |
| : DAOFactory | |
| { | |
| public static string ConnectionString | |
| { | |
| get | |
| { | |
| if (ConfigurationManager.ConnectionStrings["MsSqlDao"] == null) | |
| throw new NullReferenceException(""); | |
| string connectionString = | |
| ConfigurationManager.ConnectionStrings["MsSqlDao"].ConnectionString; | |
| if (String.IsNullOrEmpty(connectionString)) | |
| throw new NullReferenceException(""); | |
| else | |
| return connectionString; | |
| } | |
| } | |
| public override ICustomer GetCustomerDao() | |
| { | |
| return new SqlCustomerDAO(); | |
| } | |
| public override IAccount GetAccountDao() | |
| { | |
| return new SqlAccountDAO(); | |
| } | |
| } | |
| public class OracleDAOFactory | |
| : DAOFactory | |
| { | |
| public static string ConnectionString | |
| { | |
| get | |
| { | |
| if (ConfigurationManager.ConnectionStrings["OracleDao"] == null) | |
| throw new NullReferenceException(""); | |
| string connectionString = | |
| ConfigurationManager.ConnectionStrings["OracleDao"].ConnectionString; | |
| if (String.IsNullOrEmpty(connectionString)) | |
| throw new NullReferenceException(""); | |
| else | |
| return connectionString; | |
| } | |
| } | |
| public override ICustomer GetCustomerDao() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public override IAccount GetAccountDao() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment