Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching of RepositoryStringProvider results #45

Open
djoledjole opened this issue Nov 29, 2013 · 0 comments
Open

Caching of RepositoryStringProvider results #45

djoledjole opened this issue Nov 29, 2013 · 0 comments

Comments

@djoledjole
Copy link

Hello I tried to cache ViewLocalizer and succeeded, but I am struggling with RepositoryStringProvider

I did subclass of ViewLocalizer and RepositoryStringProvider in following way:

public class CachedViewLocalizer : ViewLocalizer
{
    public CacheWrapper CacheRepository { get; set; }

    public CachedViewLocalizer(IViewLocalizationRepository repository): base(repository)
    {
        this.CacheRepository = new CacheWrapper();
    }

    public override string Translate(string viewPath, RouteData routeData, string text)
    {
        string prompt;

        if (CacheRepository.IsSet(text)) return text;

        prompt = base.Translate(viewPath, routeData, text);

        CacheRepository.Set(text, prompt, 600);

        return prompt;
    }
}

// caching type translations
public class CachedTypeLocalizer : RepositoryStringProvider
{
    /// <summary>
    /// Gets or sets the instance of the class which wraps Cache Object
    /// </summary>
    public CacheWrapper CacheRepository { get; set; }

    public CachedTypeLocalizer(ILocalizedTypesRepository repository) : base(repository)
    {
        this.CacheRepository = new CacheWrapper();
    }

    protected override string Translate(Type type, string name)
    {
        var promptName = type.FullName + "." + name;

        string prompt;

        if (CacheRepository.IsSet(promptName)) return promptName;

        prompt = base.Translate(type, name);

        CacheRepository.Set(promptName, prompt, 600);

        return prompt;
    }
}

In Global.asax

private static void RegisterLocalizationFeaturesInTheContainer(ContainerBuilder builder)
{

builder.RegisterControllers(typeof(Griffin.MvcContrib.GriffinAdminRoles).Assembly);
builder.RegisterControllers(Assembly.GetExecutingAssembly());

 //builder.RegisterType<RepositoryStringProvider>().AsImplementedInterfaces().InstancePerLifetimeScope();
 builder.RegisterType<CachedTypeLocalizer>().As<RepositoryStringProvider>().InstancePerLifetimeScope();

 //builder.RegisterType<ViewLocalizer>().AsImplementedInterfaces().InstancePerLifetimeScope();
 builder.RegisterType<CachedViewLocalizer>().As<ViewLocalizer>().InstancePerLifetimeScope();                                        

 builder.RegisterType<SqlLocalizedTypesRepository>).AsImplementedInterfaces().InstancePerLifetimeScope();

 builder.RegisterType<SqlLocalizedViewsRepository>).AsImplementedInterfaces().InstancePerLifetimeScope();

 builder.RegisterInstance(new AdoNetConnectionFactory("CustomerConnection")).AsSelf();
 builder.RegisterType<LocalizationDbContext>().AsImplementedInterfaces().InstancePerLifetimeScope();

}

but with error
System.InvalidOperationException: Failed to find an 'ILocalizedStringProvider' implementation. Either include one in the LocalizedModelMetadataProvider constructor, or register an implementation in your Inversion Of Control container.

Do you have any suggestion

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant