Whenever you see you are writing too many if, if/else or case statements, STOP THERE. There must be some better of way of doing this.

fb_anonymous_user's picture

This is where design patterns come into play. Patterns like factory, builder, composite and visitor to name a few.

habib's picture

Do not stop if you see to many if if/else to the design pattern itself. for example, Factory itself contain to many if/else.

fb_anonymous_user's picture

then the factory design was not implemented correct. For there are many ways of using abstraction that allow for generic code implementation. If you have to man if, if/else or case statements in a factory implementation you are doing it wrong

habib's picture

Often face a scenario like Object need to build based on configuration value. IDb CreateDb(){ int dbType=int.Parse(ConfigurationManager.Appsettings["dbType"])); if (dbType == 1) return new SqlDb(); If(DbType == 2) return new OracleDb(); if(dbType == 3) return new MySqlDb(); if (dbType == 4) return new AccessDb();} How to stop to see that type of if logic from my factory and find out the better way to doing that? It will be very helpful for me to know the "if" remove solution for this scenario. Thanks in advance.

habib's picture

Another factory: To many if/else. Need to know better solution. public static IModelRepo CreateRepository()
{
if (typeof(IModelRepo) == typeof(IUserRepository))
{
IUserRepository repository = new NHUserRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(IOrganizationRepository))
{
IOrganizationRepository repository = new NHOrganizationRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(IApplicationRepository))
{
IApplicationRepository repository = new NHApplicationRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(IRoleRepository))
{
IRoleRepository repository = new NHRoleRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(IPermissionRepository))
{
IPermissionRepository repository = new NHPermissionRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(ISecurityQuestionRepository))
{
ISecurityQuestionRepository repository = new NHSecurityQuestionRepository(NHSessionFactory.CreateNHSession());
return (IModelRepo)repository;
}
else if (typeof(IModelRepo) == typeof(ISPTOCRepository))
{
return (IModelRepo)(ISPTOCRepository)new SPTOCRepository();
}
else if (typeof(IModelRepo) == typeof(ISPNewsRepository))
{
return (IModelRepo)(ISPNewsRepository)new SPNewsRepository();
}
else if (typeof(IModelRepo) == typeof(IDynamicLinkRepository))
{
return(IModelRepo) (IDynamicLinkRepository)new NHDynamicLinkRepository(NHSessionFactory.CreateNHSession());
}
else if(typeof(IModelRepo) == typeof(ISPResourceCategoryRepository))
{
return (IModelRepo) (ISPResourceCategoryRepository) new SPResourceCategoryRepository();
}
else if(typeof(IModelRepo) == typeof(ISPResourceLinkRepository))
{
return (IModelRepo) (ISPResourceLinkRepository) new SPResouceLinkRepository();}
else if(typeof(IModelRepo) == typeof(ISPTopNavRepository))
{
return (IModelRepo) (ISPTopNavRepository) new SPTopNavRepository();
}
else if(typeof(IModelRepo) == typeof(ISPDataLinkRepository))
{
return (IModelRepo) (ISPDataLinkRepository) new SPDataLinkRepository();
}
else
{

throw new UracException("Cannot create repository implementation for " + typeof(IModelRepo).Name);
} }//End of Method

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.