繁体   English   中英

c#接口名称空间前缀

[英]c# Interface namespace prefix

假设我有这个接口IRepository

 public interface IRepository
 {
    #region User
        IUser AddUser(String userName, String password, String email);
        IUser FindUser(String identifier);
    #endregion User

    #region Product
        IProduct AddProduct(...);
        void RemoveProduct(...);
    #endregion Product
}

到目前为止,这是非常基本的,我可以做类似的事情

IRepository MyRepository = new Repository(...);
MyRepository.AddUser(...);
MyRepository.RemoveProduct(...);

理想情况下,我想要与此相似的东西

public interface IRepository
{
    #region User
        IUser User.User(String userName, String password, String email);
        IUser User.Find(String identifier);
    #endregion User

    #region Product
        IProduct Product.Add(...);
        void Product.Remove(...);
    #endregion Product
}

不幸的是,这在C#中不被接受。 我无法添加这些前缀。

我想使用这种命名空间来编写与此类似的内容

IRepository MyRepository = new Repository(...);
MyRepository.User.Add(...);
MyRepository.Product.Remove(...);

我不想将User和Product对象添加到我的界面中,因为我所需要的只是通过这种前缀来改善阅读效果的一种方法。

任何想法?

谢谢

将其拆分为两个单独的接口。

public interface IRepositoryUser
{
    IUser Add(String userName, String password, String email);
    IUser Find(String identifier);
}

public interface IRepositoryProduct
{
    IProduct Add(...);
    void Remove(...);
}

public interface IRepository
{
    IRepositoryUser User { get; }
    IRepositoryProduct Product { get; }
}

正如您已经意识到的那样,所需的C#语法不合法。
我建议您坚持使用第一个版本。 它清晰易读,并符合默认的命名约定。

如果您正在谈论存储库模式,请回答:

根据我的谦卑意见,并根据我的经验,您为什么要混合产品和用户?

为什么要为存储库方法添加前缀?

只需为任何域对象类型“ IUserRepository”和“ IProductRepository”创建一个存储库,就不会有如此奇怪的要求。

我不知道您是否用另一种编程语言拥有此功能,但是即使您拥有或没有,我仍然发现一个错误的设计,使存储库不仅负责域对象类型,而且负责任。


回答您是否还有其他疑问

您也需要分开关注。

例如,您将有一个数据服务,并且您想要管理用户和产品。 我建议您创建两个管理器:UsersManager和ProductsManager。

两者都将与DataServiceManager关联,因此您可以具有以下内容:

DataServiceManager dataServiceMan = new DataServiceManager();
dataServiceMan.Users.RegisterUser(...);
dataServiceMan.Products.CreateProduct(...);

IUser定义Add方法,然后在IProduct中定义Remove方法,然后在您的IRepository只需定义IUser User {get; set;} IUser User {get; set;}IProduct Product {get; set;} IProduct Product {get; set;}

我相信您的语法MyRepository.User.Add(...); MyRepository.Product.Remove(...); 会没事的。

我认为最好是在不同的存储库类中执行不同的实体的单独逻辑更好,并且在基本的IRepository中仅保留诸如Add Remove Save Update等常见方法。

interface IRepository
{
   void Save();
   //other common methods
}
interface IProductRepository:IRepository
 {
   //Methods specified for products
 }

通常使用泛型来解决,因此您将拥有

  interface IRepository<T>

{
   T FindAll()
   T FindSingle(string id)

}

然后基于该接口实现派生类,几乎总是使您的接口肿,这并不是解决ISP的方法

我想您需要显式的接口实现,例如下面的示例:

    ....................
    interface IOne
    {
       void MethodOne();
    }

    interface ITwo
    {
        void MethodTwo();
    }

    interface IThree
    {
        void MethodThree();
    }

    class TestIf : IOne, ITwo, IThree
    {
        void IThree.MethodThree()
        {
            MessageBox.Show("Method three");                
        }

        void IOne.MethodOne()
        {
            MessageBox.Show("Method one");
        }

        void ITwo.MethodTwo()
        {
            MessageBox.Show("Method two");
        }
    }
    ...................
    private void button1_Click(object sender, EventArgs e)
    {
        TestIf t = new TestIf();

        IOne one = t as IOne;
        ITwo two = t as ITwo;
        IThree three = t as IThree;

        one.MethodOne();
        two.MethodTwo();
        three.MethodThree();

    }

请注意,在这种方法中,您只能通过将对象显式转换为指定的接口来调用接口方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM