C# 类公共属性:getter 和 setter 在这里做正确的事情吗?
我有一堂课,如下所示;
public class AccommPropertyRepository : IAccommPropertyRepository {
public AccommPropertyRepository() {
}
public AccommPropertyRepository(int _accommPropertyID) {
accommPropertyID = _accommPropertyID;
}
private readonly int accommPropertyID;
private Guid _accommpropertyguid;
public AccommProperty GetSingle(int id) {
//Logic sits here and returns single AccommProperty class
}
public Guid AccommPropertyGUID {
get {
var model = GetSingle(accommPropertyID);
_accommpropertyguid = model.AccommPropertyGUID;
return _accommpropertyguid;
}
set {
}
}
}
如您所见,有一个名为 AccommPropertyGUID
的公共属性。我已经测试了代码,它返回了它应该返回的内容,但我不确定我的代码是否以正确的方式编写。
我在这里实现了该结构吗?
更新:
我的目标是能够按如下方式调用此类;
var poo = new AccommPropertyRepository(1000);
var pooGUID = poo.AccommPropertyGUID;
I have a class which looks like as follows;
public class AccommPropertyRepository : IAccommPropertyRepository {
public AccommPropertyRepository() {
}
public AccommPropertyRepository(int _accommPropertyID) {
accommPropertyID = _accommPropertyID;
}
private readonly int accommPropertyID;
private Guid _accommpropertyguid;
public AccommProperty GetSingle(int id) {
//Logic sits here and returns single AccommProperty class
}
public Guid AccommPropertyGUID {
get {
var model = GetSingle(accommPropertyID);
_accommpropertyguid = model.AccommPropertyGUID;
return _accommpropertyguid;
}
set {
}
}
}
As you can see, there is a public property called AccommPropertyGUID
. I have tested the code, it returns what it should but I am not sure my code is written in a right way.
Did I implement the structure right here?
UPDATE:
My aim here is to be able to call this class as follows;
var poo = new AccommPropertyRepository(1000);
var pooGUID = poo.AccommPropertyGUID;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然问题是关于这里的属性,但是如果你看一个更抽象的级别,即在类级别,你会发现这里有些奇怪的东西。该类用于表示 AccomProperty 的存储库。现在是一个存储库的简单定义,它存储(由底层持久存储抽象)特定类型的对象,并允许您对这些对象执行一些操作(如访问、删除等)。因此,在我的观点中,使用这样的存储库
并不是一个好主意。由于此代码表示您正在创建存储库对象以仅访问一个 id 为 1000 的特定 accomm 属性对象,其中存储库应允许访问底层存储的对象,而不仅仅是单个对象。使用存储库的更好方法如下:
这种访问 GUID 属性的方式是对象的一部分,而不是存储库的一部分,因为该属性是实际对象的责任,而不是存储库的责任。存储库只是为了允许您访问底层对象集。
我希望这是有道理的。
Although the question is about the property here but if you look at a much abstract level i.e at the class level you will find something strange here. The class is used to represent the Repository for AccomProperty. Now a simple definition of a repository that it stores (abstracted by the underlying persistent store) a specific type of objects and allows you to perform some operations (like access, delete etc) on those objects. So using a repository like
isn't a good idea in my POV. As this code denotes that you are creating the repository object to access only one specific accomm property object which has id 1000 where as repository should allow access to the underling stored objects and not just to a single object. A better way to use repository would be something like:
This way accessing the GUID property is part of the object and not of the repository as the property is responsibility of the actual object and not the repository. Repository is there just to allow you to access the underlying sets of objects.
I hope this makes sense.
您的二传手看起来不正确。您需要在其中设置
_accommpropertyguid
或将其完全删除。您可以对传入的值添加验证。您需要决定是否需要在该房产上安置二传手。这是最简单的实现:在我看来,getter 看起来不错,但如果不了解更多有关
GetSingle
实现的信息,很难判断。一般来说,使用 getter 应该没有副作用,而且应该快。
Your setter looks incorrect. You need to set
_accommpropertyguid
in it or remove it altogether. You can add validation on the passed in value. You need to decide if you want a setter on the property or not. Here is the simplest implementation:The getter looks OK to me, though without knowing more about the implementation of
GetSingle
it is difficult to tell.In general, there should be no side effects to using the getter and it should be fast.
我认为你应该这样做这样
你就不必每次都重新计算 _accommpropertyguid 值
I think you should do this instead
In this way you won't have to recalculate _accommpropertyguid value each time
看起来不错,如果你想要一个清晰的代码,你可以删除空集块。
但我不确定如果未分配“accommPropertyID”会发生什么?
您有 2 个构造函数,第一个构造函数没有对“accommPropertyID”进行任何赋值
这样你的一些函数可能会出错。您可以使用 try-catch 博客或者您
必须为“accommPropertyID”分配默认值,否则您必须删除第一个
构造函数。
it looks fine, if you want to have a clear code you may remove the empty set block.
But i'm not sure about what will happen, if "accommPropertyID" is not assigned ?
You have 2 constructors and the first one has not any assignment to "accommPropertyID"
so that some of your functions may give error. You may use try-catch blogs or you
have to assign a default value for "accommPropertyID" or you have to remove the first
constructor.