[英]asp.net mvc4 EF5 Singleton?
I have a project going on and I'd like to have one unique instance of a class.我有一个项目正在进行中,我想要一个独特的类实例。
I have a 'JobOffer' class, which has a property of type 'OfferStatus' (which is abstract and implements a state pattern).我有一个“JobOffer”类,它有一个“OfferStatus”类型的属性(它是抽象的并实现了一个状态模式)。 I have 'StateAvailable' and 'StateUnavailable' (or 'open' and 'closed' if you wish).
我有 'StateAvailable' 和 'StateUnavailable'(或者如果你愿意,也可以是 'open' 和 'closed')。 The 'JobOffer' objects have to be stored in the db.
'JobOffer' 对象必须存储在数据库中。
I'd like to have just one 'StateAvailable' and one 'StateUnavailable', so when I create a new JobOffer I reference to 'StateAvailable' or 'StateUnavailable', and then I could list all the jobOffers which are Open (available) and all that are Closed (unavailable).我只想有一个“StateAvailable”和一个“StateUnavailable”,所以当我创建一个新的 JobOffer 时,我会引用“StateAvailable”或“StateUnavailable”,然后我可以列出所有开放(可用)和所有已关闭(不可用)。
I know that I could do this by adding the states in the db in the seed method, and never instantiate a new state.我知道我可以通过在种子方法的数据库中添加状态来做到这一点,并且永远不要实例化新状态。 But I was wondering if it is possible to do a singleton or something to avoid that somebody (I mean controller, model or anything) can create new instances of that class.
但我想知道是否有可能做一个单例或其他事情来避免某人(我的意思是控制器、模型或任何东西)可以创建该类的新实例。
public class JobOffer {
public int JobOfferId {get;set;}
public OfferState State {get;set;
public virtual ICollection<Person> People {get;set;}
//And some methods here, which depends on the state
//ie, this.State.myMethod();
My first thought was to use a boolean.我的第一个想法是使用布尔值。 Then you said you have to be able to expand to have more states, so I thought of an enum.
然后你说你必须能够扩展到有更多的状态,所以我想到了一个枚举。 Then you said you have this requirement to use a class, so... here's a little something I use when I want an enum with more smarts.
然后你说你有这个要求来使用一个类,所以......当我想要一个更聪明的枚举时,我会使用一些东西。 You could call it a sort of "enumerating class", I suppose.
我想你可以称它为一种“枚举类”。 So, your
OfferState
class looks like this:因此,您的
OfferState
类如下所示:
public sealed class OfferState
{
public bool CanChangeState { get; set; }
//whatever properties you need
public static OfferState Available = new OfferState(true);
public static OfferState Unavailable = new OfferState(true);
public static OfferState Closed = new OfferState(false);
//whatever states you need
public OfferState(bool canChange)
{
CanChangeState = canChange;
}
}
This acts kind of like an enum, but it has properties like a class.这有点像枚举,但它具有类似类的属性。 So in your logic, you can check state:
所以在你的逻辑中,你可以检查状态:
if (jobOffer.State == OfferState.Available)
{
//stuff
}
You can also get properties off it, so you can use it to get information about the state:您还可以从中获取属性,以便您可以使用它来获取有关状态的信息:
jobOffer.ExpiryDate = jobOffer.CreationDate.Add(OfferState.Available.MaxDuration);
And of course, the static
nature of the various states will ensure that there's only ever one instance of each.当然,各种状态的
static
特性将确保每个状态只有一个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.