How to handle services in Cinch Model ?

Oct 12, 2010 at 10:11 AM

I have a problem with my application developed with Cinch V2. I can't handle situation when my Model can't use services like logger or dataLayerservice.

For example I have DataLayerService : IDataService which exposes methods to data provider.

Firstly I imported Model into ViewModel and imported logger and other services into Model. But it didn't work because  I created objects like

foreach (myDto in dataLayerService.GetAll(typeof(mydto)))

myModel = new MyModel(myDto);

and services were not injected into Model;

 

WPF Demo unfortunately doesn't contain any Model or similar implementations.

Coordinator
Oct 13, 2010 at 7:53 AM
Edited Oct 13, 2010 at 7:56 AM

There are a few options here

 

1. Just have the ViewModel import the services and pass them to constructor of model so something like this

[ExportViewModel("SomeViewModel")]    
[PartCreationPolicy(CreationPolicy.NonShared)]    
public class SomeViewModel : ViewModelBase
{

        private ILogger logger;

        [ImportingConstructor]
        public SomeViewModel(
            ILogger logger)
        {
            //setup services
            this.logger = logger;
	}



	...
	...
	...
	//Where you create your Models
	foreach (myDto in dataLayerService.GetAll(typeof(mydto)))
		myModel = new MyModel(myDto, logger);



}

You could also just get the services from the container directly like this:

[ExportViewModel("SomeViewModel")]    
[PartCreationPolicy(CreationPolicy.NonShared)]    
public class SomeViewModel : ViewModelBase
{



	//Where you create your Models

	ILogger logger = 
                    ViewModelRepository.Instance.Resolver.Container.GetExport<ILogger>().Value;

	foreach (myDto in dataLayerService.GetAll(typeof(mydto)))
		myModel = new MyModel(myDto, logger);



}

Obviously for both of these to work you will need to make sure your logger service is exported to MEF so something like that shown below (Note the MEF attributes above class definition), where I am using CinchV1 logger implementation. NOTE : that the ILogger interface and implementation do not exist in CinchV2, so you will need to grab the interface and the implementation class, and modify the service implementation as shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Slf;

namespace Cinch
{
    /// 
    /// A SimpleLoggerFacade based error logger.
    /// This is what Cinch uses by default, you can override
    /// this by supplying a new Cinch.ILogger based service
    /// 



    [PartCreationPolicy(CreationPolicy.NonShared)]
    [ExportService(ServiceType.Both, typeof(ILogger))]
    public class WPFSLFLogger : ILogger
    {
        #region Data
        private static Slf.ILogger logger = null;
        #endregion

        #region Ctor
        static WPFSLFLogger()
        {
            logger = LoggerService.GetLogger();
        }
        #endregion

        #region ILogger Members


        public void Error(Exception exception)
        {
            logger.Error(exception);
        }

        public void Error(object obj)
        {
            logger.Error(obj);
        }

        public void Error(string message)
        {
            logger.Error(message);
        }

        public void Error(Exception exception, string message)
        {
            logger.Error(exception, message);
        }

        public void Error(string format, params object[] args)
        {
            logger.Error(format, args);
        }

        public void Error(Exception exception, string format, params object[] args)
        {
            logger.Error(exception, format, args);
        }

        public void Error(IFormatProvider provider, string format, params object[] args)
        {
            logger.Error(provider, format, args);
        }

        public void Error(Exception exception, string format, IFormatProvider provider, params object[] args)
        {
            logger.Error(exception, format, provider, args);
        }


        

        #endregion
    }
}

ALSO NOTE

 

In Cinch I generally moved away from the Model idea, as most people preferred all the properties from their model to be duplicated on their ViewModel