protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            DataServiceContext context =
                new DataServiceContext(CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint.AbsoluteUri,
                    CloudStorageAccount.DevelopmentStorageAccount.Credentials);

            CloudBlobClient blobClient = new CloudBlobClient(CloudStorageAccount.DevelopmentStorageAccount.BlobEndpoint.AbsoluteUri,
                CloudStorageAccount.DevelopmentStorageAccount.Credentials);

            Services = new ServiceFacade();

            Services.Accounts = new AccountService(Services, new AccountServiceImplementor(context));
            Services.Groups = new GroupService(Services, new GroupServiceImplementor(context));
            Services.Roles = new RoleService(Services, new RoleServiceImplementor(context));
            Services.Images = new ImageService(Services, new ImageServiceImplementor(context));
            Services.Blobs = new BlobService(Services, new BlobServiceImplementor(blobClient));

            if (User.Identity.IsAuthenticated)
                Account = Services.Accounts.Get(User.Identity.Name);
            else
                Account = null;

            if (null == Account)
                FormsAuthentication.SignOut();
            else
            {
                Account.LastActivityDate = DateTime.Now;
                Account.Save();
            }

            ViewData["Account"] = Account;
        }
        public ActionResult InitStorage()
        {
            DataServiceContext context =
                new DataServiceContext(CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint.AbsoluteUri,
                    CloudStorageAccount.DevelopmentStorageAccount.Credentials);

            context.InitializeStorage();
            Services.InitializeStorage();

            return RedirectToAction("Index", "Root");
        }
        /// <summary>
        /// Создает счетчик последовательных идентификаторов.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетчика </param>
        public static void CreateIfNotExist(DataServiceContext context,
            string id)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            if (null == GetById(context, id))
            {
                Identity identity = new Identity
                {
                    PartitionKey = id,
                    RowKey = string.Empty,
                    Value = 0
                };
                context.AddObject(Table.Identities, identity);
                context.SaveChanges();
            }
        }
 public AccountImplementor(DataServiceContext context)
 {
     _context = context;
 }
 public GroupServiceImplementor(DataServiceContext context)
 {
     _context = context;
 }
 public ImageServiceImplementor(DataServiceContext context)
 {
     _context = context;
 }
        /// <summary> 
        /// Расчитывает новый последовательный идентификатор для данного
        /// счетчика.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетика </param>
        /// <returns> Новый последовательный идентификатор </returns>
        public static long Get(DataServiceContext context, string id)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            long result = -1;
            Identity identity = GetById(context, id);
            identity.Value++;
            result = identity.Value;
            Update(context, identity);
            return result;
        }
        public static void Update(DataServiceContext context, Identity identity)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (null == identity)
                throw new ArgumentNullException("identity");

            MergeOption mergeOption = context.MergeOption;
            context.MergeOption = MergeOption.PreserveChanges;
            bool preconditionFailed;
            do
            {
                try
                {
                    preconditionFailed = false;
                    context.UpdateObject(identity);
                    context.SaveChanges();
                }
                catch (DataServiceRequestException e)
                {
                    if (e.Response.First().StatusCode
                        == (int)HttpStatusCode.PreconditionFailed)
                    {
                        preconditionFailed = true;
                        identity = GetById(context, identity.PartitionKey);
                    }
                    else
                        throw;
                }
            }
            while (preconditionFailed);
            context.MergeOption = mergeOption;
        }
        /// <summary>
        /// Устанавливает значение счетчика идентификаторов.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетчика </param>
        /// <param name="value"> Новое значение </param>
        public static void Set(DataServiceContext context, string id,
            long value)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            Identity identity = GetById(context, id);
            if (null == identity)
                throw new ArgumentNullException("id");
            identity.Value = value;
            Update(context, identity);
        }
        /// <summary>
        /// Удаляет счетчик последовательных идентификаторов.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетчика </param>
        public static void Remove(DataServiceContext context, string id)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            Identity identity = GetById(context, id);
            context.DeleteObject(identity);
            context.SaveChanges();
        }
        /// <summary>
        /// Возвращает счетчик по его идентификатору.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетчика </param>
        /// <returns></returns>
        public static Identity GetById(DataServiceContext context,
            string id)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            return (from i in context.Identities
                    where i.PartitionKey == id
                    select i).FirstOrDefault();
        }
 public RoleImplementor(DataServiceContext context)
 {
     _context = context;
 }