.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 = "操作成功" });
}
小结:也很清晰,很简单。
.Net数据操作案例的更多相关文章
- Android 常用数据操作封装类案例
1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...
- Android之三种网络请求解析数据(最佳案例)
AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...
- oracle-2-sql数据操作和查询
主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...
- Lotus防病毒与数据备份案例
Lotus防病毒与数据备份案例 上文(http://chenguang.blog.51cto.com/350944/1334595)中我们已安装好了Domino服务器,这节里我们需要考虑安全解决方案, ...
- D3js初探及数据可视化案例设计实战
摘要:本文以本人目前所做项目为基础,从设计的角度探讨数据可视化的设计的方法.过程和结果,起抛砖引玉之效.在技术方案上,我们采用通用web架构和d3js作为主要技术手段:考虑到项目需求,这里所做的可视化 ...
- MySQL(一) -- MySQL学习路线、数据库的基础、关系型数据库、关键字说明、SQL、MySQL数据库、MySQL服务器对象、SQL的基本操作、库操作、表操作、数据操作、中文数据问题、 校对集问题、web乱码问题
1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...
- R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...
- Redis 安装,配置以及数据操作
Nosql介绍 Nosql:一类新出现的数据库(not only sql)的特点 不支持SQL语法 存储结构跟传统关系型数据库中那种关系表完全不同,nosql中存储的数据都是k-v形式 Nosql的世 ...
- MySQL库操作,表操作,数据操作。
数据库服务器:本质就是一台计算机,该计算机上安装有数据库管理软件的服务端,供客户端访问使用. 1数据库管理系统RDBMS(本质就是一个C/S架构的套接字),关系型数据库管理系统. 库:(文件夹)- ...
随机推荐
- Android textView开头空两格问题,排版缩进2个汉字
一般为了排版,textView中字符段落开头一般都会空两格显示,如下图 但是如果你靠敲击空格来解决那就错了,那样在不同的屏幕上显示会差异,完美的解决方法是用转义字符”\t“,在段首加\t\t就解决.加 ...
- CSU 1364 Interview RMQ
题意: 瑶瑶有一家有一家公司,最近他想招m个人.因为他的公司是如此的出名,所以有n个人来参加面试.然而,瑶瑶是如此忙,以至于没有时间来亲自面试他们.所以他准备选择m场面试来测试他们. 瑶瑶决定这样来安 ...
- Unity WebGL 窗口自适应
unity 打包好WebGL后,用文本编辑器编辑打包生成的 index.html 文件 在生成的html里面修改代码 <script type="text/javascript ...
- 【习题 8-19 UVA-1312】Cricket Field
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 添加两个y坐标0和h 然后从这n+2个y坐标中任选两个坐标,作为矩形的上下界. 然后看看哪些点在这个上下界中. 定义为坐标集合S S ...
- 解析XML文件之使用DOM解析器
在前面的文章中.介绍了使用SAX解析器对XML文件进行解析.SAX解析器的长处就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析. DOM解析器的长处可能是理解起来比較的直观,当然, ...
- POJ 1088: 滑雪(经典 DP+记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74996 Accepted: 27818 Description ...
- Java 实现状态(State)模式
/** * @author stone */ public class WindowState { private String stateValue; public WindowState(Stri ...
- HTML——上中下布局
上中下布局是最主要的布局方式,本比如果用户屏幕分辨率为800*600像素. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2h1bWlu/font ...
- 《Spring技术内幕》笔记-第四章 Spring MVC与web环境
上下文在web容器中的启动 1,IoC容器的启动过程 IoC的启动过程就是建立上下文的过程.该上下文是与ServletContext相伴.在Spring中存在一个核心控制分发器,Dispatcher ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...