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 = "操作成功" });
}

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

.Net数据操作案例的更多相关文章

  1. Android 常用数据操作封装类案例

    1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...

  2. Android之三种网络请求解析数据(最佳案例)

    AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...

  3. oracle-2-sql数据操作和查询

    主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...

  4. Lotus防病毒与数据备份案例

    Lotus防病毒与数据备份案例 上文(http://chenguang.blog.51cto.com/350944/1334595)中我们已安装好了Domino服务器,这节里我们需要考虑安全解决方案, ...

  5. D3js初探及数据可视化案例设计实战

    摘要:本文以本人目前所做项目为基础,从设计的角度探讨数据可视化的设计的方法.过程和结果,起抛砖引玉之效.在技术方案上,我们采用通用web架构和d3js作为主要技术手段:考虑到项目需求,这里所做的可视化 ...

  6. MySQL(一) -- MySQL学习路线、数据库的基础、关系型数据库、关键字说明、SQL、MySQL数据库、MySQL服务器对象、SQL的基本操作、库操作、表操作、数据操作、中文数据问题、 校对集问题、web乱码问题

    1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...

  7. R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...

  8. Redis 安装,配置以及数据操作

    Nosql介绍 Nosql:一类新出现的数据库(not only sql)的特点 不支持SQL语法 存储结构跟传统关系型数据库中那种关系表完全不同,nosql中存储的数据都是k-v形式 Nosql的世 ...

  9. MySQL库操作,表操作,数据操作。

      数据库服务器:本质就是一台计算机,该计算机上安装有数据库管理软件的服务端,供客户端访问使用. 1数据库管理系统RDBMS(本质就是一个C/S架构的套接字),关系型数据库管理系统. 库:(文件夹)- ...

随机推荐

  1. Metasploit的攻击实例讲解----辅助扫描工具

    不多说,直接上干货! 怎么弹出来这个呢,连续按两次tab. msf > use auxiliary/scanner/ Display all possibilities? (y or n) us ...

  2. vue中makeMap方法的使用 (定义注册一些值 后期方便使用)

    function makeMap ( str, expectsLowerCase ) { var map = Object.create(null); var list = str.split(',' ...

  3. GoldenGate 进程

    GoldenGate进程 Manager进程 Manager进程是GoldenGate的控制进程,运行在源端和目标端上.它主要作用有以下几个方面:启动.监控.重启Goldengate的其他进程,报告错 ...

  4. MNIST手写数字数据集

    下载python源代码之后,使用: import input_data mnist = input_data.read_data_sets('MNIST_data/',one_hot=True) 下载 ...

  5. [NOIP2012提高组]国王游戏

    题目:洛谷P1080.Vijos P1779.codevs1198. 题目大意:国王和每个大臣左.右手各写了一个数.规定每个大臣得到的金币数为他前面所有人左手的数字的乘积除以他自己右手的数(向下取整) ...

  6. 关于vue自定义事件中,传递参数的一点理解

    例如有如下场景 先熟悉一下Vue事件处理 <!-- 父组件 --> <template> <div> <!--我们想在这个dealName的方法中传递额外参数 ...

  7. [转] C# HttpWebRequest 绝技

    c# HttpWebRequest与HttpWebResponse绝技    阅读原文 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的G ...

  8. Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)

    搭建的环境:eclispe下面的maven web项目 Struts:    2.5.10 Spring:    4.3.8 Hibernate:   5.1.7 .Final MySQL:   5. ...

  9. Codeforces--602A--Two Bases(水)

    Two Bases Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit St ...

  10. tomcat安装部署

    1.tomcat6 下载地址 http://tomcat.apache.org/download-60.cgi 下载的话,下载那个.tar.gz后缀名的即可. 好像在 Linux.Unix上tomca ...