C# 接口名称空间前缀

发布于 2024-10-21 07:56:52 字数 1189 浏览 5 评论 0原文

假设我有这个接口 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 对象添加到我的界面中,因为我需要的只是一种方法通过这种前缀来提高阅读能力。

有什么想法吗?

谢谢

Let's suppose I have this interface 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
}

This is very basic until now and I can do things like

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

Ideally, I want something similare to this

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
}

Unfortunately, this is not accepted in C#. I cannot add these prefixes.

I want to use this kind of namespace to write something similar to this

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

I don't want to add User and Product objects to my Interface as all I need is a way to improve the reading by this kind of prefixes.

Any idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

明天过后 2024-10-28 07:56:52

将其分成两个独立的接口。

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; }
}

Split it into two separate interfaces.

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; }
}
各自安好 2024-10-28 07:56:52

正如您已经意识到的那样,您想要的内容在 C# 语法中是不合法的。
我建议您坚持使用第一个版本。它清晰、可读并且符合默认的命名约定。

What you want, is not legal in the C# syntax as you already realized.
I suggest you stick with your first version. It is clear, readable and conforms to the default naming conventions.

泼猴你往哪里跑 2024-10-28 07:56:52

IUser 中定义 Add 方法,在 IProduct 中定义 Remove 方法,然后在 IRepository 中定义怎么样? 只需定义 IUser User {get; set;}IProduct 产品 {get;设置;}

我相信你的语法 MyRepository.User.Add(...);
MyRepository.Product.Remove(...); 就可以了。

What about defining the Add method in IUser and the Remove method in IProduct and then in your IRepository just define IUser User {get; set;} and IProduct Product {get; set;}.

I believe then your syntax MyRepository.User.Add(...); and
MyRepository.Product.Remove(...); will be fine.

漫雪独思 2024-10-28 07:56:52

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

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

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

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

我不知道您是否在其他编程语言中具有此功能,但是,即使您有或没有,我发现一个糟糕的设计使得存储库负责的不仅仅是域对象类型。


如果您正在谈论其他任何事情,请回答

您还需要区分关注点。

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

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

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

Answer if you're talking about repository pattern:

In my humild opinion, and based on my experience, why are you mixing products and users?

Why you want to prefix repository methods?

Just create a repository for any domain object type "IUserRepository" and "IProductRepository" and you won't have such strange requirement.

I don't know if you have this feature in another programming language, but, even if you've it or not, I find a bad design making a repository reponsible of more than a domain object type.


Answer if you're talking about anything else

You would need to separate concerns too.

For example, you would have a data service and you want to manage users and products. I'd suggest you create two managers: UsersManager and ProductsManager.

Both would be associated to a DataServiceManager so you can have something like this:

DataServiceManager dataServiceMan = new DataServiceManager();
dataServiceMan.Users.RegisterUser(...);
dataServiceMan.Products.CreateProduct(...);
佞臣 2024-10-28 07:56:52

我认为在不同的存储库类中执行不同的实体更好的单独逻辑
并在基础 IRepository 中仅保留常用方法,例如添加删除保存更新等。
例如

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

I think is better separate logic for performing different Entities in different Repositories classes
and in base IRepository keep only common methods like Add Remove Save Update etc.
for example

interface IRepository
{
   void Save();
   //other common methods
}
interface IProductRepository:IRepository
 {
   //Methods specified for products
 }
彻夜缠绵 2024-10-28 07:56:52

我认为您需要显式接口实现,如下例所示:

    ....................
    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();

    }

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

I suppose that you are need explicit interface implementation, like sample below:

    ....................
    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();

    }

Pay attention, in such approach you can call interface methods only by explicitly convert object to specified interface

听风念你 2024-10-28 07:56:52

通常使用泛型来解决这个问题,因此您可以

  interface IRepository<T>

{
   T FindAll()
   T FindSingle(string id)

}

基于接口实现派生类,几乎总是让您的接口膨胀并不是正确的方法,并且会破坏 ISP

The is generally solved using generics so you would have

  interface IRepository<T>

{
   T FindAll()
   T FindSingle(string id)

}

then implement derived classes based on the interface, almost always bloating your interfaces isnt the way to go and breaks ISP

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