public virtual Type ResolveViewModelType(string name)
        {
            IDictionary <string, string> properties;
            Type res    = ResolveType(name, out properties);
            bool isPOCO = GetIsPOCOViewModelType(res, properties);

            return(isPOCO ? ViewModelSource.GetPOCOType(res) : res);
        }
Beispiel #2
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(UnitOfWork <>))
            .As(typeof(IUnitOfWork <>));

            foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(t => t.HasCustomAttribute <POCOViewModelAttribute>()))
            {
                builder.RegisterType(ViewModelSource.GetPOCOType(type)).InstancePerDependency().As(type);
            }
        }
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();

            ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(
                viewType =>
            {
                var viewName         = viewType.FullName;
                viewName             = viewName?.Replace(".Views.", ".ViewModels.");
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
                var suffix           = viewName != null && viewName.EndsWith("View") ? "Model" : "ViewModel";
                var viewModelName    = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);
                return(ViewModelSource.GetPOCOType(Type.GetType(viewModelName)));
            });
        }
Beispiel #4
0
        public static ModulePageCollection Create(IEnumerable <IModule> modules)
        {
            ModulePageCollection defaultCollection = new ModulePageCollection();

            if (!File.Exists(FileLocation))
            {
                return(defaultCollection);
            }

            try
            {
                List <Type> knownTypes = new List <Type>();
                foreach (IModule module in modules)
                {
                    Type moduleType = ViewModelSource.GetPOCOType(module.GetType());
                    if (!knownTypes.Contains(moduleType))
                    {
                        knownTypes.Add(moduleType);
                    }
                }

                using (FileStream fileStream = new FileStream(FileLocation, FileMode.Open, FileAccess.Read))
                {
                    DataContractSerializerSettings settings = new DataContractSerializerSettings();
                    settings.DataContractResolver = new ModuleDataContractResolver {
                        ModuleTypes = knownTypes
                    };
                    settings.KnownTypes = knownTypes;

                    DataContractSerializer serializer           = new DataContractSerializer(typeof(ModulePageCollection), settings);
                    ModulePageCollection   serializedCollection = serializer.ReadObject(fileStream) as ModulePageCollection;

                    if (serializedCollection != null)
                    {
                        foreach (ModulePage page in serializedCollection.Pages)
                        {
                            page.Purge();
                        }

                        return(serializedCollection);
                    }

                    return(defaultCollection);
                }
            }
            catch { return(defaultCollection); }
        }
        /// <summary>
        /// A bootstrapper that initializes the inversion of control container and registers all the types necessary for dependency injection (sets up MVVM relationship).
        /// </summary>
        /// <returns>Returns the DI container.</returns>
        public static IServiceProvider ConfigureServices()
        {
            // Registers the logger, the repositories and mapper.
            IServiceCollection services = new ServiceCollection()
                                          .AddLogging(builder => builder.AddFilter("Microsoft", LogLevel.Warning).AddFilter("System", LogLevel.Warning).AddNLog())
                                          .AddSingleton <IInventoryRepository, FileInventoryRepository>()
                                          .AddSingleton <InventoryMapper>();

            // Register all view models in the assembly with the custom attribute [POCOViewModel].
            var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();

            foreach (var vmt in assembly.GetTypes().Where(t => Attribute.GetCustomAttribute(t, typeof(POCOViewModelAttribute)) != null))
            {
                services.AddTransient(vmt, ViewModelSource.GetPOCOType(vmt));
            }

            // Create and return the DI container.
            return(services.BuildServiceProvider());
        }
 /// <summary>
 /// Creates a new view model of type T.
 /// </summary>
 /// <typeparam name="T">A type representing any view model type (i.e book, movie, video game, etc.)</typeparam>
 /// <returns>A new model of type T</returns>
 private static T CreateNewModel <T>()
 {
     // DevExpress uses Reflection Emit to create a descendant of the specified ViewModel class and returns the descendant class instance at runtime.
     // We pass a new emitted type to the activator to create a new corresponding instance.
     return((T)Activator.CreateInstance(ViewModelSource.GetPOCOType(typeof(T))));
 }