.Net数据操作案例

简介:

Interface


using System.Collections.Generic;
using Ddd.Core.Domain.Customers;
namespace Ddd.Services.Customers
{
    /// <summary>
    /// Contact service
    /// </summary>
    public partial interface IContactService
    {
        /// <summary>
        /// Gets all Contacts
        /// </summary>
        /// <returns>Contact</returns>
        IList<Contact> GetAllContacts();

        /// <summary>
        /// Gets a Contact 
        /// </summary>
        /// <param name="contactId">Contact identifier</param>
        /// <returns>Contact</returns>
        Contact GetContactById(int contactId);

        /// <summary>
        /// Inserts a Contact
        /// </summary>
        /// <param name="contact">Contact</param>
        void InsertContact(Contact contact);

        /// <summary>
        /// Updates the Contact
        /// </summary>
        /// <param name="contact">Contact</param>
        void UpdateContact(Contact contact);

        /// <summary>
        /// Deletes a Contact
        /// </summary>
        /// <param name="Contact">Contact</param>
        void DeleteContact(Contact contact);

        /// <summary>
        /// Deletes a Contact
        /// </summary>
        /// <param name="SelfId">SelfId</param>
        /// <param name="FriendId">FriendId</param>
        void DeleteContact(int SelfId,int FriendId);

        List<Contact> GetContactsByCustomerId(int customerId, bool includeBlack = true);
        Contact GetContactByCustomerIds(int sourceId, int targetId);
        bool CheckFriends(int sourceId, int targetId);
    }
}

Interface实现


using System;
using System.Collections.Generic;
using System.Linq;
using Ddd.Core.Caching;
using Ddd.Core.Data;
using Ddd.Core.Domain.Customers;
using Ddd.Services.Events;

namespace Ddd.Services.Customers
{
    /// <summary>
    /// Contact service
    /// </summary>
    public partial class ContactService : IContactService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string CONTACTS_ALL_KEY = "YY.contact.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : contact ID
        /// </remarks>
        private const string CONTACT_BY_ID_KEY = "YY.contact.id-{0}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string CONTACTS_PATTERN_KEY = "YY.contact.";
        #endregion
        
        #region Fields
        private readonly IRepository<Contact> _contactRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="contactRepository">Contact repository</param>
        /// <param name="eventPublisher">Event published</param>
        public ContactService(ICacheManager cacheManager,
            IRepository<Contact> contactRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._contactRepository = contactRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all Contacts
        /// </summary>
        /// <returns>Contacts</returns>
        public virtual IList<Contact> GetAllContacts()
        {
            string key = CONTACTS_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _contactRepository.Table
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        /// <summary>
        /// Gets a Contact 
        /// </summary>
        /// <param name="contactId">Contact identifier</param>
        /// <returns>Contact</returns>
        public virtual Contact GetContactById(int contactId)
        {
            if (contactId == 0)
                return null;

            string key = string.Format(CONTACT_BY_ID_KEY, contactId);
            return _cacheManager.Get(key, () => _contactRepository.GetById(contactId));
        }

        /// <summary>
        /// Inserts a Contact
        /// </summary>
        /// <param name="contact">Contact</param>
        public virtual void InsertContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Insert(contact);

            _cacheManager.RemoveByPattern(CONTACTS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(contact);
        }

        /// <summary>
        /// Updates the Contact
        /// </summary>
        /// <param name="contact">Contact</param>
        public virtual void UpdateContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Update(contact);

            _cacheManager.RemoveByPattern(CONTACTS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(contact);
        }

        /// <summary>
        /// Deletes a Contact
        /// </summary>
        /// <param name="contact">Contact</param>
        public virtual void DeleteContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Delete(contact);

            _cacheManager.RemoveByPattern(CONTACTS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(contact);
        }

        /// <summary>
        /// Deletes a Contact
        /// </summary>
        /// <param name="SelfId">SelfId</param>
        /// <param name="FriendId">FriendId</param>
        public virtual void DeleteContact(int SelfId,int FriendId)
        {
            Contact contact = GetContactByCustomerIds(SelfId, FriendId);
            DeleteContact(contact);
        }


        public Contact GetContactByCustomerIds(int sourceId, int targetId)
        {
            var query = from c in _contactRepository.Table
                        where c.SelfId == sourceId
                        && c.FriendId == targetId
                        select c;
            return query.FirstOrDefault();
        }

        public List<Contact> GetContactsByCustomerId(int customerId,bool includeBlack=true)
        {
            var query = from c in _contactRepository.Table
                        where c.SelfId == customerId
                        &&(includeBlack||!c.IsBlack)
                        select c;
            return query.ToList();
        }

        public virtual bool CheckFriends(int sourceId,int targetId)
        {
            return _contactRepository.Table.Any(c => c.SelfId == sourceId && c.FriendId == targetId);
        }
        #endregion
    }
}

调用

private readonly IContactService _contactService;
public ActionResult delFriends(int SelfId,int FriendId)
{
     _contactService.DeleteContact(SelfId, FriendId);
     return Json(new { result = true, info = "", msg = "操作成功" });
}

小结:也很清晰,很简单。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6857110.html,如需转载请自行联系原作者

相关文章
|
8月前
mvc.net分页查询案例——实体层(HouseModel.cs)
mvc.net分页查询案例——实体层(HouseModel.cs)
|
8月前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
|
8月前
mvc.net分页查询案例——PagerExtension
mvc.net分页查询案例——PagerExtension
|
8月前
mvc.net分页查询案例——PagedList
mvc.net分页查询案例——PagedList
|
5月前
|
SQL 开发框架 数据库
".NET开发者的超能力:AgileEAS.NET ORM带你穿越数据库的迷宫,让数据操作变得轻松又神奇!"
【8月更文挑战第16天】AgileEAS.NET是面向.NET平台的企业应用开发框架,核心功能包括数据关系映射(ORM),允许以面向对象方式操作数据库,无需编写复杂SQL。通过继承`AgileEAS.Data.Entity`创建实体类对应数据库表,利用ORM简化数据访问层编码。支持基本的CRUD操作及复杂查询如条件筛选、排序和分页,并可通过导航属性实现多表关联。此外,提供了事务管理功能确保数据一致性。AgileEAS.NET的ORM简化了数据库操作,提升了开发效率和代码可维护性。
59 5
|
3月前
|
SQL XML 关系型数据库
入门指南:利用NHibernate简化.NET应用程序的数据访问
【10月更文挑战第13天】NHibernate是一个面向.NET的开源对象关系映射(ORM)工具,它提供了从数据库表到应用程序中的对象之间的映射。通过使用NHibernate,开发者可以专注于业务逻辑和领域模型的设计,而无需直接编写复杂的SQL语句来处理数据持久化问题。NHibernate支持多种数据库,并且具有高度的灵活性和可扩展性。
52 2
|
3月前
|
数据采集
爬虫案例—爬取ChinaUnix.net论坛板块标题
爬虫案例—爬取ChinaUnix.net论坛板块标题
64 0
爬虫案例—爬取ChinaUnix.net论坛板块标题
|
5月前
|
开发框架 .NET 数据库连接
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
101 1
|
6月前
|
开发框架 JSON 前端开发
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理