我已經在我的asp.net mvc web應用程序中實現了一個存儲庫模式...但是我想知道這是一個好的存儲庫模式,還是我可以改進它更多...好的存儲庫模式爲asp.net mvc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaxiMVC.BusinessObjects;
namespace TaxiMVC.Models
{
public class ClientRepository
{
private TaxiDataContext taxidb = new TaxiDataContext();
Client cli = new Client();
//Get all Clients
public IQueryable<ClientBO> FindAllClients(int userId)
{
var client = from c in taxidb.Clients
where c.CreatedBy == userId && c.IsDeleted == 0
select new ClientBO()
{
ClientId = c.ClientId,
ClientName= c.ClientName,
ClientMobNo= Convert.ToString(c.ClientMobNo),
ClientAddress= c.ClientAddress
};
return client;
}
//Get Client By Id
public ClientBO FindClientById(int userId,int clientId)
{
return (from c in taxidb.Clients
where c.CreatedBy == userId && c.ClientId == clientId && c.IsDeleted == 0
select new ClientBO()
{
ClientId = c.ClientId,
ClientName= c.ClientName,
ClientMobNo= Convert.ToString(c.ClientMobNo),
ClientAddress= c.ClientAddress
}).FirstOrDefault();
}
//Insert a new client
public bool ClientInsert(ClientBO clientBO)
{
cli.ClientName = clientBO.ClientName;
cli.ClientMobNo = Convert.ToInt64(clientBO.ClientMobNo);
cli.ClientAddress = clientBO.ClientAddress;
cli.CreatedDate = clientBO.CreatedDate;
cli.IsDeleted = clientBO.IsDeleted;
cli.CreatedBy = clientBO.CreatedBy;
if (!taxidb.Clients.Where(c => c.ClientMobNo == cli.ClientMobNo).Any())
{
taxidb.Clients.InsertOnSubmit(cli);
taxidb.SubmitChanges();
return true;
}
else
return false;
}
//Client Update
public ClientBO updateClient(ClientBO clientBO)
{
var table = taxidb.GetTable<Client>();
var cli = table.SingleOrDefault(c => c.ClientId == clientBO.ClientId && c.CreatedBy==clientBO.CreatedBy);
cli.ClientName = clientBO.ClientName;
cli.ClientMobNo = Convert.ToInt64(clientBO.ClientMobNo);
cli.ClientAddress = clientBO.ClientAddress;
taxidb.SubmitChanges();
return clientBO;
}
//Delete Clients
public bool deleteClients(string Ids, int userId)
{
var idsToDelete = Ids.Split(',').Select(c => Convert.ToInt32(c));
var clientsToDelete = taxidb.Clients.Where(c => idsToDelete.Contains(c.ClientId));
foreach (var client in clientsToDelete)
{
client.IsDeleted = Convert.ToByte(1);
}
taxidb.SubmitChanges();
return true;
}
}
}
和我ClientBo.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TaxiMVC.BusinessObjects
{
public class ClientBO
{
public int ClientId { get; set; }
public string ClientName { get; set; }
public string ClientMobNo { get; set; }
public string ClientAddress { get; set; }
public DateTime CreatedDate { get; set; }
public byte IsDeleted { get; set; }
public int CreatedBy { get; set; }
}
}
我沒有在這裏實現一個IRepository ...我應該已經實現,或應我的倉庫能夠進一步提高......任何建議。 ...
嚴格來說,它不是「存儲庫」模式。 你可以在這裏閱讀更多: http://fabiomaulo.blogspot.com/2009/09/repository-or-dao-repository.html – griZZZly8 2010-07-19 05:01:23