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. JQ加载进度条动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 把asp:CheckBoxList 变成单选框

    单选框代码 <asp:CheckBoxList runat="server" RepeatDirection="Horizontal" ID=" ...

  3. ivew语法中'${}`的用法

  4. 将double数据保留两位小数

    private double formatDouble(double number) { DecimalFormat df = new DecimalFormat("#.00"); ...

  5. [国家集训队]拉拉队排练 Manancher_前缀和_快速幂

    Code: #include <cstdio> #include <algorithm> #include <cstring> using namespace st ...

  6. Win 7系统倒计时!

    3月25日消息,近日微软已经开始通知当前正在使用Windows 7的用户,该操作系统“接近尾声”.微软表示计划在2020年1月14日终止对Windows 7的所有支持.但结束Windows 7似乎并不 ...

  7. uwsgi erro

    Installing collected packages: uwsgi Running setup.py install for uwsgi: started Running setup.py in ...

  8. 二 HTable 源码导读

    户端调优的方法里面无非就这么几种:1)关闭autoFlush2)关闭WAL日志3)把writeBufferSize设大一点,一般说是设置成5MB        经过实践,就第二条关闭日志的效果比较明显 ...

  9. 怎样只接受固定长度数组为参数 & 数组形参

    注意,对于多维数组的情况.

  10. scikit-learn:3.2. Grid Search: Searching for estimator parameters

    參考:http://scikit-learn.org/stable/modules/grid_search.html GridSearchCV通过(蛮力)搜索參数空间(參数的全部可能组合).寻找最好的 ...