views:

59

answers:

2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EfTestFactory
{
    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 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 AntsController
    {
        static Dictionary<DataModelType, Func<object>> s_creators =
            new Dictionary<DataModelType, Func<object>>()
            {
                { DataModelType.Radyoloji,  () => new _Radyoloji() },
                { DataModelType.Company,    () => new _Company() },
                { DataModelType.Muayene,    () => new _Muayene() },
                { DataModelType.Satis,      () => new _Satis() },
            };

        public object CreateByEnum(DataModelType modeltype)
        {
            return s_creators[modeltype]();
        }
    }

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

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

if i write above codes i see some error: Cannot create an instance of abstract class or interface 'EfTestFactory_Company'How can i solve it? Look please below pic.alt text

+1  A: 

You cannot instantiate abstract classes. You could instantiate only concrete classes:

static Dictionary<DataModelType, Func<object>> s_creators =
    new Dictionary<DataModelType, Func<object>>()
    {
        { DataModelType.Radyoloji,  () => new Radyoloji() },
        { DataModelType.Company,    () => new Company() },
        { DataModelType.Muayene,    () => new Muayene() },
        { DataModelType.Satis,      () => new Satis() },
    };
Darin Dimitrov
+3  A: 

The purpose of the keyword abstract is to make a class not instantiable. If you have declared a class abstract and you want to instantiate it, then you have a design flaw.

Petar Minchev