views:

40

answers:

3

GenoTipController must produce class according to the enum type. i have 3 class: _Company,_Muayene,_Radyoloji. Also i have CompanyView Class GetPersonel method. if you look GenoTipController my codes need refactoring. Can you understand me? i need a class according to ewnum type must me produce class. For example; case DataModelType.Radyoloji it must return radyoloji= new Radyoloji . Everything must be one switch case?


 public abstract class _Company
    {
        public abstract List<Personel> GetPersonel();

        public abstract List<Prim> GetPrim();

        public abstract List<Finans> GetFinans();
    }

    public abstract class _Radyoloji
    {
        public abstract List<string> GetRadyoloji();
    }
    public abstract class _Satis
    {
        public abstract List<string> GetSatis();
    }
    public abstract class _Muayene
    {
        public abstract List<string> GetMuayene();
    }

    public class Company: _Company
    {

        public override List<Personel> GetPersonel()
        {
            throw new NotImplementedException();
        }

        public override List<Prim> GetPrim()
        {
            throw new NotImplementedException();
        }

        public override List<Finans> GetFinans()
        {
            throw new NotImplementedException();
        }
    }

    public class Radyoloji : _Radyoloji
    {
        public override List<string> GetRadyoloji()
        {
            throw new NotImplementedException();
        }
    }

    public class Satis : _Satis
    {
        public override List<string> GetSatis()
        {
            throw new NotImplementedException();
        }
    }

    public class Muayene : _Muayene
    {
        public override List<string> GetMuayene()
        {
            throw new NotImplementedException();
        }
    }

 public class GenoTipController
    {
        public _Company GenerateCompany(DataModelType modeltype)
        {
            _Company company = null;
            switch (modeltype)
            {
                case DataModelType.Radyoloji:
                    break;
                case DataModelType.Satis:
                    break;
                case DataModelType.Muayene:
                    break;
                case DataModelType.Company:
                    company = new Company(); 
                    break;
                default:
                    break;
            }

            return company;

        }

        public _Muayene GenerateMuayene(DataModelType modeltype)
        {
            _Muayene muayene = null;
            switch (modeltype)
            {
                case DataModelType.Radyoloji:
                    break;
                case DataModelType.Satis:
                    break;
                case DataModelType.Muayene:
                    muayene = new Muayene();
                    break;
                case DataModelType.Company:
                    break;
                default:
                    break;
            }

            return muayene;

        }

        public _Radyoloji GenerateRadyoloji(DataModelType modeltype)
        {
            _Radyoloji radyoloji = null;
            switch (modeltype)
            {
                case DataModelType.Radyoloji:
                    radyoloji = new Radyoloji();
                    break;
                case DataModelType.Satis:
                    break;
                case DataModelType.Muayene:

                    break;
                case DataModelType.Company:
                    break;
                default:
                    break;
            }

            return radyoloji;

        }
    }

    public class CompanyView
    {
        public static List<Personel> GetPersonel()
        {
            GenoTipController controller = new GenoTipController();
            _Company company = controller.GenerateCompany(DataModelType.Company);
             return company.GetPersonel();

        }
 }

    public enum DataModelType
    {
        Radyoloji,
        Satis,
        Muayene,
        Company
    }
}

if use oleksiy.t method :

 public class GenoTipController
    {


        public object CreateByEnum(DataModelType modeltype)
        {
            string enumText = modeltype.ToString(); // will return for example "Company"
            Type classType = Type.GetType(enumText); // the Type for Company class
            object t = Activator.CreateInstance(classType); // create an instance of Company class

            return t;
        }
    }

    public class CompanyView
    {
        public static List<Personel> GetPersonel()
        {
            GenoTipController controller = new GenoTipController();
            _Company company = controller.CreateByEnum(DataModelType.Company);
             return company.GetPersonel();

        }
   }
    public enum DataModelType
    {
        Radyoloji,
        Satis,
        Muayene,
        Company
    }

alt text

i face to face error: i like oleksiy method but how can i use it?

+1  A: 

Have a look at the factory pattern, which is what I think you are looking for.

example code.

public interface IPizza
{
    decimal Price { get; }
}

public class HamAndMushroomPizza : IPizza
{
    decimal IPizza.Price
    {
        get
        {
            return 8.5m;
        }
    }
}

public class DeluxePizza : IPizza
{
    decimal IPizza.Price
    {
        get
        {
            return 10.5m;
        }
    }
}

public class HawaiianPizza : IPizza
{
    decimal IPizza.Price
    {
        get
        {
            return 11.5m;
        }
    }
}

public class PizzaFactory
{
    public enum PizzaType
    {
        HamMushroom,
        Deluxe,
        Hawaiian
    }

    public static IPizza CreatePizza(PizzaType pizzaType)
    {
        IPizza ret = null;

        switch (pizzaType)
        {
            case PizzaType.HamMushroom:
                ret = new HamAndMushroomPizza();

                break;
            case PizzaType.Deluxe:
                ret = new DeluxePizza();

                break;
            case PizzaType.Hawaiian:
                ret = new HawaiianPizza();

                break;
            default:
                throw new ArgumentException("The pizza type " + pizzaType + " is not recognized.");
        }

        return ret;
    }
}

public class PizzaLover
{
    public static void Main(string[] args)
    {
        Dictionary<PizzaFactory.PizzaType, IPizza> pizzas = new Dictionary<PizzaFactory.PizzaType, IPizza>();

        foreach (PizzaFactory.PizzaType pizzaType in Enum.GetValues(typeof(PizzaFactory.PizzaType)))
        {
            pizzas.Add(pizzaType, PizzaFactory.CreatePizza(pizzaType));
        }

        foreach (PizzaFactory.PizzaType pizzaType in pizzas.Keys)
        {
            System.Console.WriteLine("Price of {0} is {1}", pizzaType, pizzas[pizzaType].Price);
        }
    }
}

Output:
Price of HamMushroom is 8.5
Price of Deluxe is 10.5
Price of Hawaiian is 11.5
PieterG
PieterG; i need a factory also i know patterns. But you have one interface: IPizza but i have more interface . İ rearranged my codes above. İlease Look:)
programmerist
A: 

You can use a naming convention for Enum names and Class names and create instances via reflection:

namespace MyNamespace
{
    public enum DataModelType { Company, Person }

    public class Company
    {
    }

    public class Person
    {
    }

    public class ObjectFactory
    {
        public static object CreateByEnum(DataModelType modelType)
        {
            string enumText = modelType.ToString(); // will return for example "Company"
            Type classType = Type.GetType("MyNamespace." + enumText); // the Type for Company class
            object t = Activator.CreateInstance(classType); // create an instance of Company class

            return t;
        }
    }
}
i rearranged my codes oleksiy. i think your solution good but return me error.
programmerist
I've missed the namespace name in Type.GetType
A: 

Using the method you described after your edit, you need to cast the object to type _Company:

_Company company = controller.CreateByEnum(DataModelType.Company) as _Company;

or

_Company company = (_Company)controller.CreateByEnum(DataModelType.Company);

Though I would recommend against using that method and suggest a switch statement, as that will save you from run-time errors.

Perhaps I can't see it from you example (or maybe it's too late at night) but if none of these items inherit from the same base class or implement the same interface then you will have to know the class you are using when you instantiate it in order to access any of its members.

nexus
Thanks! alot but which one is best?
programmerist