是否可以将此代码的第二部分用于存储库模式和泛型
使用版本 2 获得与版本 1 相同的结果是否存在任何问题? 或者这只是糟糕的编码。
任何想法
public class Customer
{
public int CustomerID { get; set; }
public string EmailAddress { get; set; }
int Age { get; set; }
}
public interface ICustomer
{
void AddNewCustomer(Customer Customer);
void AddNewCustomer(string EmailAddress, int Age);
void RemoveCustomer(Customer Customer);
}
public class BALCustomer
{
private readonly ICustomer dalCustomer;
public BALCustomer(ICustomer dalCustomer)
{
this.dalCustomer = dalCustomer;
}
public void Add_A_New_Customer(Customer Customer)
{
dalCustomer.AddNewCustomer(Customer);
}
public void Remove_A_Existing_Customer(Customer Customer)
{
dalCustomer.RemoveCustomer(Customer);
}
}
public class CustomerDataAccess : ICustomer
{
public void AddNewCustomer(Customer Customer)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
public void AddNewCustomer(string EmailAddress, int Age)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
public void RemoveCustomer(Customer Customer)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
}
// VERSION 2
public class Customer_New : DataRespository<CustomerDataAccess>
{
public int CustomerID { get; set; }
public string EmailAddress { get; set; }
public int Age { get; set; }
}
public class DataRespository<T>
where T:class,new()
{
private T item = new T();
public T Execute { get { return item; } set { item = value; } }
public void Update()
{
//TO BE CODED
}
public void Save()
{
//TO BE CODED
}
public void Remove()
{
//TO BE CODED
}
}
class Program
{
static void Main(string[] args)
{
Customer_New cus = new Customer_New()
{
Age = 10,
EmailAddress = "[email protected]"
};
cus.Save();
cus.Execute.RemoveCustomer(new Customer());
// Repository Version
Customer customer = new Customer()
{
EmailAddress = "[email protected]",
CustomerID = 10
};
BALCustomer bal = new BALCustomer(new CustomerDataAccess());
bal.Add_A_New_Customer(customer);
}
}
Is there any issues in using version 2,to get the same results as version 1.
Or is this just bad coding.
Any Ideas
public class Customer
{
public int CustomerID { get; set; }
public string EmailAddress { get; set; }
int Age { get; set; }
}
public interface ICustomer
{
void AddNewCustomer(Customer Customer);
void AddNewCustomer(string EmailAddress, int Age);
void RemoveCustomer(Customer Customer);
}
public class BALCustomer
{
private readonly ICustomer dalCustomer;
public BALCustomer(ICustomer dalCustomer)
{
this.dalCustomer = dalCustomer;
}
public void Add_A_New_Customer(Customer Customer)
{
dalCustomer.AddNewCustomer(Customer);
}
public void Remove_A_Existing_Customer(Customer Customer)
{
dalCustomer.RemoveCustomer(Customer);
}
}
public class CustomerDataAccess : ICustomer
{
public void AddNewCustomer(Customer Customer)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
public void AddNewCustomer(string EmailAddress, int Age)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
public void RemoveCustomer(Customer Customer)
{
// MAKE DB CONNECTION AND EXECUTE
throw new NotImplementedException();
}
}
// VERSION 2
public class Customer_New : DataRespository<CustomerDataAccess>
{
public int CustomerID { get; set; }
public string EmailAddress { get; set; }
public int Age { get; set; }
}
public class DataRespository<T>
where T:class,new()
{
private T item = new T();
public T Execute { get { return item; } set { item = value; } }
public void Update()
{
//TO BE CODED
}
public void Save()
{
//TO BE CODED
}
public void Remove()
{
//TO BE CODED
}
}
class Program
{
static void Main(string[] args)
{
Customer_New cus = new Customer_New()
{
Age = 10,
EmailAddress = "[email protected]"
};
cus.Save();
cus.Execute.RemoveCustomer(new Customer());
// Repository Version
Customer customer = new Customer()
{
EmailAddress = "[email protected]",
CustomerID = 10
};
BALCustomer bal = new BALCustomer(new CustomerDataAccess());
bal.Add_A_New_Customer(customer);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你发生了很多没有多大意义的事情。
首先,属性的名称应该始终是名词(单数或复数)或“存在”动词,例如 Is* 或 Has*。这些是对象的属性,应该类似于您在回答“请描述一下您的办公桌吗?”之类的问题时所说的内容。
Execute
是一个操作,因此应该是一个方法。同样,版本 1 中的命名约定应为 PascalCased,这意味着没有下划线,并且所有单词的第一个字母应大写。这些并不是铁杆事实,但它们被认为是OOP常见的 C# 编码标准。其次,主方法中的代码实际上并未实现泛型类中的任何内容。您的类实际上所做的唯一事情是创建
CustomerDataAccess
的实例。Save()
方法不会执行任何操作,除非您特别能够调用item.Save()
为了在您的设备上使用您的保存、更新、删除功能泛型类,您的CustomerDataAccess
类必须实现泛型类所需的接口。例如:现在,您可以创建一个处理基本 CRUD 功能的泛型类,并且所有其他功能都可以通过 BaseRepository 属性访问。
注意 我从头开始/记忆中编写了上述代码。通用类型约束可能无法完全按照我的方式工作。
Stephen Walther 的 ASP.NET 3.5 Unleashed 有几章介绍如何创建存储库模式的设置与您在版本 2 中尝试完成的类似。他还将业务逻辑层和数据访问层之间的处理分开。尽管这本书很大(近 2000 页)并且许多代码示例都是多余的或者最好作为 CD 的一部分保留,但他对初级到中级的范围进行了相当深入的介绍。它可以在亚马逊上使用,价格约为 25 美元。
You have a lot of things going on that aren't making a lot of sense.
First of all, the names of properties should always be a noun (singular or plural) or a "being" verb like Is* or Has*. These are properties of an object, and should be similar to what you would say in response to a question like "Would you please describe your desk?"
Execute
is an operation, and should therefore be a method. Likewise, your naming conventions in Version 1 should be PascalCased which means no underscores and the first letter of all words should be capitalized. These aren't die-hard truths, but they are consideredOOPcommon C# coding standards.Secondly, the code in your main method isn't actually implementing anything in your generic class. The only thing your class is actually doing is creating an instance of
CustomerDataAccess
. TheSave()
method won't do anything, unless you specifically are able to callitem.Save()
In order to use your Save, Update, Delete functionality on your generic class, yourCustomerDataAccess
class will have to implement an interface expected by your generic class. For instance:Now, you can create a generic class that handles basic CRUD functionality, and all other functionality is accessible through the BaseRepository property.
NOTE I did the above code from scratch/memory. The generic type constraints may not work exactly as I have them.
ASP.NET 3.5 Unleashed by Stephen Walther has a couple of chapters on creating a repository pattern which is setup similarly to what you're trying to accomplish in Version 2. He also splits processing up between a business logic layer and a data access layer. Although the book is huge (nearly 2000 pages) and many of the code examples are redundant or better left as part of the CD, he goes pretty in-depth for beginner-to-intermediate range. It's available used on Amazon for around $25.
我认为在实现应用程序的对象模型时,您只需要问自己一些问题,就好像您正在对同事代码进行对象设计审查一样。
我认为可以继续...我希望你明白我的意思?
I think while implementing object model of your application you just have to ask yourself a number of questions as though you are make object design review of your collegue code.
I think could be continued... I hope you've catch my point?