C# 接口名称空间前缀
假设我有这个接口 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将其分成两个独立的接口。
Split it into two separate interfaces.
正如您已经意识到的那样,您想要的内容在 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.
在
IUser
中定义Add
方法,在IProduct
中定义Remove
方法,然后在IRepository 中定义怎么样?
只需定义IUser User {get; set;}
和IProduct 产品 {get;设置;}
。我相信你的语法
MyRepository.User.Add(...);
和MyRepository.Product.Remove(...);
就可以了。What about defining the
Add
method inIUser
and theRemove
method inIProduct
and then in yourIRepository
just defineIUser User {get; set;}
andIProduct Product {get; set;}
.I believe then your syntax
MyRepository.User.Add(...);
andMyRepository.Product.Remove(...);
will be fine.如果您正在谈论存储库模式,请回答:
以我的拙见,并根据我的经验,您为什么要混合产品和用户?
为什么要为存储库方法添加前缀?
只需为任何域对象类型“IUserRepository”和“IProductRepository”创建一个存储库,您就不会有如此奇怪的要求。
我不知道您是否在其他编程语言中具有此功能,但是,即使您有或没有,我发现一个糟糕的设计使得存储库负责的不仅仅是域对象类型。
如果您正在谈论其他任何事情,请回答
您还需要区分关注点。
例如,您有一个数据服务,并且想要管理用户和产品。我建议您创建两个管理器:UsersManager 和 ProductsManager。
两者都将与 DataServiceManager 关联,因此您可以拥有如下内容:
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:
我认为在不同的存储库类中执行不同的实体更好的单独逻辑
并在基础 IRepository 中仅保留常用方法,例如添加删除保存更新等。
例如
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
我认为您需要显式接口实现,如下例所示:
注意,在这种方法中,您只能通过显式将对象转换为指定接口来调用接口方法
I suppose that you are need explicit interface implementation, like sample below:
Pay attention, in such approach you can call interface methods only by explicitly convert object to specified interface
通常使用泛型来解决这个问题,因此您可以
基于接口实现派生类,几乎总是让您的接口膨胀并不是正确的方法,并且会破坏 ISP
The is generally solved using generics so you would have
then implement derived classes based on the interface, almost always bloating your interfaces isnt the way to go and breaks ISP