进行某种非平凡、个人编码项目,在其中可以自由地尝试设计模式。
但我不知道我是否做得正确。
以下是我的项目结构:
正如您所看到的,有许多文件夹,我将在下面详细描述它们。
Domain : Project Domain Entities go here I've a simple Personnel class which is inherited from EntityBase class, EntityBase class has a single property named Id.
public int Id { get; set; }
Infrustructure : Here is a simple Data Access Layer with two classes. SqlDataLayer is a simple class which is inherited from an abstract class named DataLayer. Here I provide some functionality like following code :
public SQLDataLayer() { const string connString = "ConnectionString goes here"; _connection = new SqlConnection(connString); _command = _connection.CreateCommand(); }
public override void AddParameter(string key, string value) {
var parameter = _command.CreateParameter();
parameter.Value = value;
parameter.ParameterName = key;
_command.Parameters.Add(parameter);
}
执行DataReader:
public override IDataReader ExecuteReader() {
if (_connection.State == ConnectionState.Closed)
_connection.Open();
return _command.ExecuteReader();
}
- 存储库:我在这里尝试实现存储库模式。IRepository是一个通用接口。
IRepository.cs:
等等。
public interface IRepository<TEntity> where TEntity : EntityBase
{
DataLayer Context { get; }
TEntity FindOne(int id);
ICollection<TEntity> FindAll();
void Delete(TEntity entity);
void Insert(TEntity entity);
void Update(TEntity entity);
}
Repository.cs :
public class Repository<TEntity> : IRepository<TEntity> where TEntity : EntityBase, new() {
private readonly DataLayer _domainContext;
private readonly DataMapper<TEntity> _dataMapper;
public Repository(DataLayer domainContext, DataMapper<TEntity> dataMapper) {
_domainContext = domainContext;
_dataMapper = dataMapper;
}
public DataLayer Context {
get { return _domainContext; }
}
public TEntity FindOne(int id)
{
var commandText = AutoCommand.CommandTextBuilder<TEntity>(CommandType.StoredProcedure, MethodType.FindOne);
// Initialize parameter and their types
Context.AddParameter("Id", id.ToString(CultureInfo.InvariantCulture));
Context.SetCommandType(CommandType.StoredProcedure);
Context.SetCommandText(commandText);
var dbReader = Context.ExecuteReader();
return dbReader.Read() ? _dataMapper.Map(dbReader) : null;
}
我没有暴露未实现的IRepository方法。 在这里,泛型仓储类中,我期望构造函数有两个参数,第一个是对我的SqlDataLayer类的引用,第二个是对Entity DataMapper的引用。这些参数由每个继承自Repository类的实体仓库类发送。例如:
public class PersonnelRepository : Repository<Personnel>, IPersonnelRepository {
public PersonnelRepository(DataLayer domainContext, PersonnelDataMapper dataMapper)
: base(domainContext, dataMapper) {
}
}
如您在FindOne方法中所见,我试图自动化一些操作,例如创建CommandText,然后利用我的DataLayer类配置命令,最终执行命令以获取IDataReader。我将IDataReader传递给我的DataMapper类以映射到实体。
DomainMapper : Finally here I map result of IDataReader to Entities, bellow is a sample of how I map Personnel entity :
public class PersonnelDataMapper : DataMapper<Personnel> { public override Personnel Map(IDataRecord record) { return new Personnel { FirstName = record["FirstName"].ToString(), LastName = record["LastName"].ToString(), Address = record["Address"].ToString(), Id = Convert.ToInt32(record["Id"]) }; }}
用法:
using (var context = new SQLDataLayer()) {
_personnelRepository = new PersonnelRepository(context, new PersonnelDataMapper());
var personnel = _personnelRepository.FindOne(1);
}
我知道我在这里犯了许多错误,这就是我来这里的原因。我需要您的建议,以了解我做错了什么或这个简单测试项目的优点是什么。
提前致谢。
DataLayer
类的内容(可能附带一些代码)? - smartcaveman