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 = "操作成功" });
}
小结:也很清晰,很简单。