这更多是一个设计问题。
我正在构建一个应用程序,并且我创建了我的存储库模式结构,如下所示:
我的核心名称空间是 DAL/Repository/BusinessLogic 层程序集。
顺便说一句,我使用 Dapper.NET micro ORM 作为我的数据连接,这就是为什么你会在我的 SqlConnection 对象上看到一个扩展。
对于我的数据访问,我创建了一个基础存储库类:
namespace Core
{
public class BaseRepository<T>: IDisposable where T : BaseEntity
{
protected SqlConnection conn = null;
#region Constructors
public BaseRepository() : this("LOCAL")
{
}
public BaseRepository(string configurationKey = "LOCAL")
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings[configurationKey].ConnectionString);
}
#endregion
#region IDisposable
public void Dispose()
{
conn.Dispose();
}
#endregion
/// <summary>
/// returns a list of entities
/// </summary>
/// <typeparam name="T">BaseEntity type</typeparam>
/// <param name="sproc">optional parameters, stored procedure name.</param>
/// <returns>BaseEntity</returns>
protected virtual IEnumerable<T> GetListEntity(string sproc = null)
{
string storedProcName = string.Empty;
if (sproc == null)
{
storedProcName = "[dbo].sp_GetList_" + typeof(T).ToString().Replace("Core.",string.Empty);
}
else
{
storedProcName = sproc;
}
IEnumerable<T> items = new List<T>();
try
{
conn.Open();
items = conn.Query<T>(storedProcName,
commandType: CommandType.StoredProcedure);
conn.Close();
}
finally
{
conn.Close();
}
return items;
}
}
}
对于我拥有的每个实体,可以说 ExtendedUser, Messages ,我正在像这样在 Interface-Class 对上创建它:
namespace Core
{
public class ExtendedUserRepository : BaseRepository<UsersExtended>,IExtendedUserRepository
{
public ExtendedUserRepository() : this("PROD")
{
}
public ExtendedUserRepository(string configurationKey) : base(configurationKey)
{
}
public UsersExtended GetExtendedUser(string username)
{
var list = GetListEntity().SingleOrDefault(u => u.Username == username);
return list;
}
public UsersExtended GetExtendedUser(Guid userid)
{
throw new NotImplementedException();
}
public List<UsersExtended> GetListExtendedUser()
{
throw new NotImplementedException();
}
}
}
等等
上面的代码只是实体之一:ExtendedUser。
问题是:我应该为我拥有的每个实体创建一个 Interface-ClassThatImplemenetsInterface 对吗?或者我应该只有一个 RepositoryClass 和一个 IRepository 接口,其中包含来自我所有实体的所有方法?