Ejemplo n.º 1
0
        protected void AddService <TModel, TService, TRepository>()
            where TModel : class
            where TService : class, IService <TModel>, new()
            where TRepository : class, IRepository <TModel>, new()
        {
            Type key = typeof(TService);

            if (!ServiceContainer.ContainsKey(key))
            {
                Lazy <IService <TModel> > obj;
                obj = new Lazy <IService <TModel> >(valueFactory: () =>
                {
                    IService <TModel> x = new TService();
                    x.Root = this;

                    if (x.RequiresOwnDbContext)
                    {
                        x.Context = this.NewDbContext();
                    }
                    else
                    {
                        x.Context = this.Context; // shared
                    }

                    x.Repository         = new TRepository();
                    x.Repository.Context = x.Context;
                    return(x);
                });

                ServiceContainer.Add(key: key, value: obj);
            }
        }
Ejemplo n.º 2
0
        public TService Services <TModel, TService>()
            where TModel : class
            where TService : class, IService <TModel>
        {
            Type key = typeof(TService);

            if (ServiceContainer.ContainsKey(key))
            {
                var lazy = (Lazy <IService <TModel> >)ServiceContainer[key];
                return(lazy.Value as TService);
            }
            else
            {
                var s = string.Format("Service '{0}' has not been registered to the App.",
                                      typeof(TService).Name);
                throw new Exception(s);
            }
        }