简体   繁体   中英

Concepts of OOP

I saw a project, i need explaination of this code

public class Grades : RepositoryBase<GEN_Grades>, IGrades
{
    public Grades() : this(new sCMSRepositoryContext())
    {
     // some code here
    }

 }

Where as the RepositoryBase Class is below

  public class sCMSRepositoryContext : IRepositoryContext
    {
        private const string OBJECT_CONTEXT_KEY = "sCMS.Dal.EntityModels";
        public IObjectSet<T> GetObjectSet<T>() 
            where T : class
        {
            return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY).CreateObjectSet<T>();
        }

        /// <summary>
        /// Returns the active object context
        /// </summary>
        public ObjectContext ObjectContext
        {
            get
            {
                return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY);
            }
        }

        public int SaveChanges()
        {
            return this.ObjectContext.SaveChanges();
        }

        public void Terminate()
        {
            ContextManager.SetRepositoryContext(null, OBJECT_CONTEXT_KEY);
        }

    }

// and here is the irepository class

public interface IRepositoryContext
{

    IObjectSet<T> GetObjectSet<T>() where T : class;

    ObjectContext ObjectContext { get; }

    /// <summary>
    /// Save all changes to all repositories
    /// </summary>
    /// <returns>Integer with number of objects affected</returns>
    int SaveChanges();

    /// <summary>
    /// Terminates the current repository context
    /// </summary>
    void Terminate();
}

i need explaination of constructor on the Grades Class that what is a meaning of this statement

public Grades() : this(new sCMSRepositoryContext())

why and when we need this???

As we know that we use ":" operator for inheritance But not getting why it has inherited the object of class also both the sCMSRepositoryContext and Grades Class don't have such a relation (ie inheritance)

thanks in advance

It is simple. You just explicitly call second constructor when invoked a first one.

Imagine something like:

public class Repository {
     //FIRST CONSTRUCTOR
     public Repository() : this(null) { //NULL OR SOME OTHER DEFAULT VALUE
     }


     //SECOND CONSTRUCTOR
     public Repository(DBObject ob) {
         //DRAMATICALLY IMPORTANT INTIALIZATION CODE
     }
}

In some cases when consumer of your class use a simple ctor, it's guaranteed that the second constructor will be invoked too , so important intialization will be executed too.

//THIS CODE WILL EXECUTE BOTH CONSTRUCTORS
Repository re = new Repository(); 

Why all this? You can invoke important initialization code in single functions (ctor) and avoid code duplication, which makes easier testing and maintaining of your code in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM