hadi khodabandeh
hadi khodabandeh

Reputation: 615

use internal class in public method

hi I'm creating a library. I have a model class that is internal accessible and a method that is public.

I know that because of the level of access, internal access cannot be used in public

If I make model class access public, When the user uses the library, he has access to both the method and the model class I want the user to have access to the method only

 public class GithubReleaseModel
        {
            public bool IsExistNewVersion { get; set; }
            public string Url { get; set; }
            public string Changelog { get; set; }
            public string Version { get; set; }
            public string DownloadUrl { get; set; }
            public string Size { get; set; }
            public bool IsPreRelease { get; set; }
            public DateTime CreatedAt { get; set; }
            public DateTime PublishedAt { get; set; }
        }

 public static GithubReleaseModel IsNewVersionExistGithub(string Username, string Repository)
        {
            var model = new GithubReleaseModel();
            ...
            return model;
        }

Upvotes: 0

Views: 2533

Answers (3)

Progman
Progman

Reputation: 19546

Define an interface with the methods the consumer method can call. Your implementation class can stay internal that way without exposing anything.

public interface IGithubReleaseModel {
    int DoSomethingYouWant();
}

internal class GithubReleaseModel : IGithubReleaseModel {
    public int DoSomethingYouWant() {
        return 42;
    }
}

public static IGithubReleaseModel IsNewVersionExistGithub(string Username, string Repository)
{
    var model = new GithubReleaseModel();
    ...
    return model;
}

Upvotes: 0

Stefan
Stefan

Reputation: 672

If you just want outside user not to be able to change properties, make the setters internal:

public bool IsExistNewVersion { get; internal set; }

This way everyone can read, but you can only write from inside the assembly (your library).

EDIT

If you don't want users of your library to create an instance of your class, limit access to the constructor:

    public class GithubReleaseModel
    {

        internal GithubReleaseModel()
        {
            // internal constructor 
        }

        public bool IsExistNewVersion { get; set; }
        public string Url { get; set; }
        public string Changelog { get; set; }
        public string Version { get; set; }
        public string DownloadUrl { get; set; }
        public string Size { get; set; }
        public bool IsPreRelease { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime PublishedAt { get; set; }
    }

    public static GithubReleaseModel IsNewVersionExistGithub(string Username, string Repository)
    {
        var model = new GithubReleaseModel();
        ...
        return model;
    }

This way you can create and return your model just as you did already, but if someone from outside wants to create a new GithubReleaseModel() he gets an access violation.

Upvotes: 4

Soc
Soc

Reputation: 7780

If what you mean is that you don't want the consumer of the library to instantiate your model, you can do make the constructor private or internal.

Example:

public class GithubReleaseModel
{
  private GithubReleaseModel()
  {
  }
  // ...
}

Upvotes: 0

Related Questions