是否可以将此代码的第二部分用于存储库模式和泛型

发布于 2024-09-01 19:01:33 字数 2665 浏览 5 评论 0原文

使用版本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-09-08 19:01:33

你发生了很多没有多大意义的事情。

首先,属性的名称应该始终是名词(单数或复数)或“存在”动词,例如 Is* 或 Has*。这些是对象的属性,应该类似于您在回答“请描述一下您的办公桌吗?”之类的问题时所说的内容。 Execute 是一个操作,因此应该是一个方法。同样,版本 1 中的命名约定应为 PascalCased,这意味着没有下划线,并且所有单词的第一个字母应大写。这些并不是铁杆事实,但它们被认为是OOP常见的 C# 编码标准。

其次,主方法中的代码实际上并未实现泛型类中的任何内容。您的类实际上所做的唯一事情是创建 CustomerDataAccess 的实例。 Save() 方法不会执行任何操作,除非您特别能够调用 item.Save() 为了在您的设备上使用您的保存、更新、删除功能泛型类,您的 CustomerDataAccess 类必须实现泛型类所需的接口。例如:

public interface IDataAccess<T> : where T : YourBaseObject {
   public void Update(T item);
   public void Save(T item);
   public void Remove(T item);
}

public class Customer : YourBaseObject {
    public int CustomerID { get; set; }
    public string EmailAddress { get; set; }
    public int Age { get; set; }
}

public class CustomerDataAccess : 
    DataRespository<IDataAccess<Customer>> {
    public void PerformCustomerOnlyAction(Customer customer) { 
     /* do stuff */
    }
}

现在,您可以创建一个处理基本 CRUD 功能的泛型类,并且所有其他功能都可以通过 BaseRepository 属性访问。

/* e.g. T = IDataAccess<Customer>, K = Customer */
public class DataRespository<T>
   where T : IDataAccess<K>, new()
   where K : YourBaseObject, new()
{

    private T _base;
    public T BaseRepository { 
          get { 
             if(_base == null)
                 _base = Activator.CreateInstance<T>(); 
             return _base;
          } 
    }
    public void Update(K item) { /* functionality for YourBaseObject */ }
    public void Save(K item) { /* functionality for YourBaseObject */ }
    public void Remove(K item) {  /* functionality for YourBaseObject */ }
}   

class Program
{
    static void Main(string[] args)
    {
        var repository = new CustomerDataAccess();
        Customer c = new Customer {
            Age = 10,
            EmailAddress = "[email protected]"
        };
        repository.Save(c);
        // This pass-through is no longer needed, but shown as example
        // repository.BaseRepository.PerformCustomerOnlyAction(c);
        repository.PerformCustomerOnlyAction(c);
    }
}

注意 我从头开始/记忆中编写了上述代码。通用类型约束可能无法完全按照我的方式工作。

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 considered OOP common 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. The Save() method won't do anything, unless you specifically are able to call item.Save() In order to use your Save, Update, Delete functionality on your generic class, your CustomerDataAccess class will have to implement an interface expected by your generic class. For instance:

public interface IDataAccess<T> : where T : YourBaseObject {
   public void Update(T item);
   public void Save(T item);
   public void Remove(T item);
}

public class Customer : YourBaseObject {
    public int CustomerID { get; set; }
    public string EmailAddress { get; set; }
    public int Age { get; set; }
}

public class CustomerDataAccess : 
    DataRespository<IDataAccess<Customer>> {
    public void PerformCustomerOnlyAction(Customer customer) { 
     /* do stuff */
    }
}

Now, you can create a generic class that handles basic CRUD functionality, and all other functionality is accessible through the BaseRepository property.

/* e.g. T = IDataAccess<Customer>, K = Customer */
public class DataRespository<T>
   where T : IDataAccess<K>, new()
   where K : YourBaseObject, new()
{

    private T _base;
    public T BaseRepository { 
          get { 
             if(_base == null)
                 _base = Activator.CreateInstance<T>(); 
             return _base;
          } 
    }
    public void Update(K item) { /* functionality for YourBaseObject */ }
    public void Save(K item) { /* functionality for YourBaseObject */ }
    public void Remove(K item) {  /* functionality for YourBaseObject */ }
}   

class Program
{
    static void Main(string[] args)
    {
        var repository = new CustomerDataAccess();
        Customer c = new Customer {
            Age = 10,
            EmailAddress = "[email protected]"
        };
        repository.Save(c);
        // This pass-through is no longer needed, but shown as example
        // repository.BaseRepository.PerformCustomerOnlyAction(c);
        repository.PerformCustomerOnlyAction(c);
    }
}

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.

压抑⊿情绪 2024-09-08 19:01:33

我认为在实现应用程序的对象模型时,您只需要问自己一些问题,就好像您正在对同事代码进行对象设计审查一样。

  • 为什么CustomerAccessLayer要实现接口?是否会有多个层实现此接口。或者也许您期望实现此接口的类有任何多态行为?或者,您可能会将接口与独立模块分开,并通过任何类型的服务提供其功能?
  • 为什么需要 BALCustomer 类?为什么不能直接调用 CustomerAccesLayer?而且,我已经谈到过 codesyle 了吗? :)
  • 如果 DataRepository 具有通用行为并且会提供许多 AccessLayers 抛出 Execute 属性,为什么它有自己的方法?

我认为可以继续...我希望你明白我的意思?

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.

  • Why CustomerAccessLayer implements interface? Is there will be a number of layers implementing this Interface. Or maybe you are expecting any polymorph behaviour from classes implements this interface? Or maybe you will separate interface to standalone module and will provide its functionality though any kind of service?
  • Why do you need BALCustomer class? Why you could not make calls directly to CustomerAccesLayer? And, have i already spoke about codesyle? :)
  • If DataRepository have a generic behaviour and will provide a number of AccessLayers throw Execute property why it is have its own methods?

I think could be continued... I hope you've catch my point?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文