PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/CBR/CBR.Core/Services/BookServiceFactory.cs

#
C# | 83 lines | 50 code | 10 blank | 23 comment | 4 complexity | d6d036193235343c163db000990bcbfe MD5 | raw file
 1using System;
 2using System.IO;
 3using CBR.Core.Files;
 4using CBR.Core.Models;
 5using CBR.Core.Helpers;
 6
 7namespace CBR.Core.Services
 8{
 9    public class BookServiceFactory
10    {
11        #region ----------------SINGLETON----------------
12
13		public static readonly BookServiceFactory Instance = new BookServiceFactory();
14
15		/// <summary>
16		/// Private constructor for singleton pattern
17		/// </summary>
18        private BookServiceFactory()
19		{
20		}
21
22		#endregion
23
24        public BookServiceBase GetService(Book bk)
25        {
26            if (bk == null)
27                return new BookServiceBase();
28            else
29                return GetService(bk.FilePath);
30        }
31
32        public BookServiceBase GetService(string filePath)
33        {
34            return (BookServiceBase)Activator.CreateInstance(
35                FileService.Instance.FindBookFilterByExt(Path.GetExtension(filePath)).Service
36                );
37        }
38
39        //public ViewModelBase GetViewModel(Book bk)
40        //{
41        //    try
42        //    {
43        //        return GetViewModel(bk.FilePath);
44        //    }
45        //    catch (Exception err)
46        //    {
47        //        ExceptionHelper.Manage("BookServiceFactory:GetViewModel", err);
48        //        return null;
49        //    }
50        //}
51
52        //public ViewModelBase GetViewModel(string filePath)
53        //{
54        //    FileExtension fe = FileExtensionManager.Instance.FindBookFilterByExt(Path.GetExtension(filePath));
55        //    if (fe != null)
56        //        return (ViewModelBase)new ReflectionHelper().CreateInstance(fe.ViewModel);
57        //    else
58        //        return null;
59        //}
60
61        public string GetViewModel(Book bk)
62        {
63            try
64            {
65                return GetViewModel(bk.FilePath);
66            }
67            catch (Exception err)
68            {
69                ExceptionHelper.Manage("BookServiceFactory:GetViewModel", err);
70                return string.Empty;
71            }
72        }
73
74        public string GetViewModel(string filePath)
75        {
76            FileExtension fe = FileService.Instance.FindBookFilterByExt(Path.GetExtension(filePath));
77            if (fe != null)
78                return fe.ViewModel;
79            else
80                return string.Empty;
81        }
82    }
83}