具有工作单元的存储库模式最适合 Entity Framework 应用程序吗?
我正在创建一个新的 asp.net mvc 应用程序,我想使用存储过程(微软企业库)而不是 Entity Framework (或任何其他 ORM)。
那么我如何将存储库模式与 mvc 应用程序一起使用?
我已经探索了很多教程,但没有得到预期的结果。请建议我更好的 n 层应用程序方法。
最佳答案
您必须了解“存储库模式”只是组织代码的一种想法。我认为您仍然可以毫无问题地使用它。
我只是想为您提供一个使用sql存储过程实现的示例:假设您必须管理经典表“Contacts”。
您可以创建您的 UnitOfWork 合约:
public interface IContactsRepository {
void AddContact(Contact c);
void RemoveContact(Contact c);
void UpdateContact(Contact c);
void Search(string keyword);
}
之后,您可以在不使用 EF 的情况下创建自己的实现:
public sealed class SPContactsRepository : IContactsRepository {
// Is just an exampl.e
public void AddContact(Contact c) {
var sqlCommnad = new SqlCommand(this._connectionString);
sqlCommand.CommandText = "dbo.AddContact";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.AddParameter("Name", c.Name);
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQuery();
}
}
希望这能有所帮助!
关于asp.net - 没有 Entity Framework 的带有 mvc 的存储库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37053889/