Exemplo n.º 1
0
        /// <summary>
        /// 保存当前实例。
        /// </summary>
        /// <param name="site">网站实例对象。</param>
        /// <returns>返回保存结果。</returns>
        public virtual async Task <DataResult> SaveAsync(TSite site)
        {
            var adapter = SiteAdapter.FromSite(site);

            if (await Context.AnyAsync(x => x.SiteKey == adapter.SiteKey && x.Id != adapter.Id))
            {
                return(DataAction.Duplicate);
            }
            if (adapter.Id > 0)
            {
                return(FromResult(adapter.Id, await Context.UpdateAsync(adapter), DataAction.Updated));
            }
            var result = await Context.BeginTransactionAsync(async db =>
            {
                if (!await db.CreateAsync(adapter))
                {
                    return(false);
                }
                site.Id = adapter.Id;
                foreach (var eventHandler in _eventHandlers)
                {
                    if (!await eventHandler.OnCreatedAsync(db, site))
                    {
                        site.Id = 0;
                        return(false);
                    }
                }
                return(true);
            }, 600);

            return(FromResult(adapter.Id, result, DataAction.Created));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存当前实例。
        /// </summary>
        /// <param name="site">网站实例对象。</param>
        /// <returns>返回保存结果。</returns>
        public virtual DataResult Save(TSite site)
        {
            var adapter = SiteAdapter.FromSite(site);

            if (Context.Any(x => x.SiteKey == adapter.SiteKey && x.Id != adapter.Id))
            {
                return(DataAction.Duplicate);
            }
            if (adapter.Id > 0)
            {
                return(FromResult(adapter.Id, Context.Update(adapter), DataAction.Updated));
            }
            var result = Context.BeginTransaction(db =>
            {
                if (!db.Create(adapter))
                {
                    return(false);
                }
                site.Id = adapter.Id;
                foreach (var eventHandler in _eventHandlers)
                {
                    if (!eventHandler.OnCreated(db, site))
                    {
                        site.Id = 0;
                        return(false);
                    }
                }
                return(true);
            }, 600);

            return(FromResult(adapter.Id, result, DataAction.Created));
        }