예제 #1
0
        public ICaexViewModelFactory GetFactory <T>(ICAEXWrapper model) where T : CaexObjectViewModel
        {
            var factories = new List <ICaexViewModelFactory>();

            foreach (var factory in _factories)
            {
                // only factories that can create the destination type
                var canCreateDestinationType = factory.CanCreate <T>(model);
                if (!canCreateDestinationType)
                {
                    continue;
                }

                // only factories that can handle the given CAEX object
                if (factory.TypeOfViewModel(model) == null)
                {
                    continue;
                }

                factories.Add(factory);
            }

            if (factories.Count == 0)
            {
                return(null);
            }
            if (factories.Count == 1)
            {
                return(factories[0]);
            }

            ICaexViewModelFactory match = null;

            foreach (var factory in factories)
            {
                var hasSubClass = false;
                foreach (var f in factories)
                {
                    var thisType  = factory.TypeOfViewModel(model);
                    var otherType = f.TypeOfViewModel(model);
                    if (otherType.IsSubclassOf(thisType))
                    {
                        hasSubClass = true;
                    }
                }

                if (!hasSubClass)
                {
                    if (match != null)
                    {
                        throw new Exception("Multiple factories found");
                    }
                    match = factory;
                }
            }

            if (match == null)
            {
                throw new Exception("Weird: factory missing for type " + model.GetType());
            }
            return(match);
        }
예제 #2
0
 public void Register(ICaexViewModelFactory factory)
 {
     _factories.Add(factory);
 }