Persistence.RepositoryFactory.GetRepositoryInstance C# (CSharp) Method

GetRepositoryInstance() public static method

Metodo statico per ottenere un'istanza di un repository di un tipo specificato. Se tutto va a buon fine il metodo ritornerà un oggetto delle classe Persistence.RepositoryImpl.TipoRepository derivata dalla classe astratta Persistence.Repository. Se viene richiesto un tipo di repository non disponibile verrà sollevata un'eccezione; è quindi preferibile, nel caso in cui non si sia sicuri della disponibilità di un tipo di repository, usare il metodo IsValidRepositoryType della Factory.
Eccezione sollevata nel caso ci siano errori nel caricamento della classe /// del repository o nell'invocazione del suo costruttore di default. Eccezione sollevata nel caso in cui la stringa contenente il tipo di repository /// richiesto risulti vuota Eccezione sollevata nel caso in cui la stringa contenente il tipo di /// repository richiesto risulti essere nulla
public static GetRepositoryInstance ( String repType, RepositoryConfiguration config ) : Persistence.Repository
repType String Stringa rappresentante il tipo di repository richiesto.
config RepositoryConfiguration
return Persistence.Repository
        public static Repository GetRepositoryInstance(String repType,RepositoryConfiguration config)
        {
            if (repType != null)
            {
                if (repType.Length != 0)
                {
                    String className = _generateRepositoryClassName(repType);
                    try
                    {
                        Type reflectedRepository = Type.GetType(className, true, true);
                        Type[] args = new Type[1] {config.GetType()};
                        Object[] param = new Object[1] { config };
                        object rep = reflectedRepository.GetConstructor(args).Invoke(param);
                        Repository inst=rep as Repository;
                        if (inst.RepositoryType.Equals(repType)) {
                            return inst;
                        } else {
                            throw new TypeLoadException("Type "+inst.RepositoryType+" is different from the expected "+repType+"!");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new TypeLoadException("Unable to load repository class " + className, ex);
                    }
                }
                else
                {
                    throw new ArgumentException("Repository Type Name must not be Empty!");
                }
            }
            else
            {
                throw new ArgumentNullException("Repository Type Name must not be NULL!");
            }
        }

Usage Example

Esempio n. 1
0
 /// <summary>
 /// Constructor for the Kademlia Repository. This begins with compiling regular expressions for whitespace and semanticless
 /// words and setting timespan sing the given values (if they are not passed, it uses the default). Then instantiates the
 /// repository of the fiven type and creates two indexes over the instantiatied repository. The first index is used to
 /// find keywords with an empty tag list; the second one is used to query keyword using tag identifier. Both indexes are
 /// necessary in order to cleanly delete resources and keywords from the repository
 /// </summary>
 /// <param name="repType">Name of the repository type. The default repository type is RavenDB ("Raven")</param>
 /// <param name="conf">Repository Configureation</param>
 /// <param name="elementValidity">Validity period of a Dht Element. Default value is 24 hours (1 day). The validity must
 /// be expressed in timespan format as described in MSDN reference for this type.</param>
 /// <param name="semanticFilter">Regular expression that will be used to remove semanticless word.</param>
 public KademliaRepository(string repType = "Raven",
                           RepositoryConfiguration conf = null,
                           string elementValidity       = "1",
                           string semanticFilter        = KademliaRepository.DefaultSemanticFilterRegexString)
 {
     log.Debug("Semantic Filter Regex used is " + DefaultSemanticFilterRegexString);
     if (!(TimeSpan.TryParse(elementValidity, out this._elementValidity)))
     {
         this._elementValidity = new TimeSpan(24, 0, 0);
     }
     this._semanticRegex   = new Regex(DefaultSemanticFilterRegexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
     this._whiteSpaceRegex = new Regex(@"[ ]{2,}", RegexOptions.Compiled);
     this._repository      = RepositoryFactory.GetRepositoryInstance(repType, conf);
     this._repository.CreateIndex("KademliaKeywords/KeysByTag",
                                  "from key in docs.KademliaKeywords\nfrom tag in key.Tags\nselect new { Kid = key.Id , Tid = tag}");
     this._repository.CreateIndex("KademliaKeywords/EmptyKeys",
                                  "from key in docs.KademliaKeywords\nwhere key.Tags.Count() == 0\nselect new { key.Id }");
 }
All Usage Examples Of Persistence.RepositoryFactory::GetRepositoryInstance