Project Description

Macds framework is a library of common modules used in our daily basis as logging, mapping, serialization, validation, inversion of control, xml helper, etc.

Don't forget to provide me your feedback about why you want to download the library or why don't, I would like to know what is the current needs that you have so I can improve my libraries

*notes: don't forget to add a rate feedback, that small thing is our motivation

The core library offers many features of common tasks to help you build great foundation. I will provide more information about the namespaces Remoting and Service Locator in another page because the examples must be clear to help you understand how to use it. Also you can download the unit test project to check the examples in http://macds.codeplex.com/releases/view/99468

Configuration

Is usual to find us dealing with a bunch of configuration section to develop in enterprise modules and one of the challenges is the collections. The core includes the namespace Configuration where you can find the class GenericElementCollection, this class help me to save time when I have to create collection properties.

[ConfigurationProperty("ConnectionStrings", IsRequired = true),
ConfigurationCollection(typeof(GenericElementCollection<CustomSettingElement>), 
AddItemName = "database", ClearItemsName = "clear", RemoveItemName = "remove")]
public GenericElementCollection<CustomSettingElement> Databases
{
    get 
    { 
      return (GenericElementCollection<CustomSettingElement>)this["ConnectionStrings"]; 
    }
    set { this["ConnectionStrings"] = value; }
 }

*download the test project http://macds.codeplex.com/releases/view/99468

Logging

If you hate to deal with log4net XML driven in development state, you are just like me, now the core includes the namespace Log where you can find the class LogFactory. This class create a log4net instance without XML configuration, giving you the option to integrate logging behavior to any class in debug mode easily because it configure ILog instance for you.
log4net.ILog logger = LogFactory.GetLog4netLogger();

*note: you can create a custom log4net logger configured in the web.config using the factory as follow:
log4net.ILog logger = LogFactory.GetLog4netLogger("LoggerName", false);

*download the test project http://macds.codeplex.com/releases/view/99468

Mapping

It is a common task to keep the domain model different of what Entity Framework creates. Using this approach we can keep the business logic depending in our own design for handling the object distribution, but, we must deal with mapping process converting the entity model to our domain model for every single entity. Well, the core offers the namespace Mapping that offer a easy way for it.

You must decorate the property that you want to match from your class to the entity model using the attribute mapping, and passing the name of the equivalent property in the entity. Just adding the attribute you can let the core handle the mapping tasks for you:

Customer Model decorated using Mapping and Customer Entity
public class CustomerModel
{
    [MappingAttribute(Name = "FirstName")]
    public string Name { get; set; }

    [MappingAttribute(Name = "LastName")]
    public string LastName { get; set; }
}

public class CustomerEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}


Object to Entity
var  customerEntity = MappingFactory.ConvertFromMappedToObject(customerModel);


Entity to Object
var  customerModel = MappingFactory.ConvertFromObjectToMapped(customerEntity);

*download the test project http://macds.codeplex.com/releases/view/99468

Also the mapping factory class create query string of the properties decorated with the attribute QueryStringAttribute.

Order Model decorated using QueryString attribute
public class OrderModel
{
    [QueryString(Name = "id", Position = 1, Optional = false)]
    public int OrderID { get; set; }

    [QueryString(Name = "amount", Position = 2, Optional = false)]
    public float Amount { get; set; }

    [QueryString(Name = "customerID", Position = 3, Optional = false)]
    public int CustomerID { get; set; }
}


Object to QueryString
string queryString = MappingFactory.GetQueryStringFromObject(order);

// queryString value is: id=1&amount=10&customerID=1

*download the test project http://macds.codeplex.com/releases/view/99468

Serialization

Nowadays serializes object to XML or bytes is a common tasks and the core provides the namespace Serialization where you can find the Converter class to help you with these process.

Object and Bytes (the class must be decorated as Serializable)
// Serialize object to byte array
byte[] data = Converter.GetBytesFromObject(customerModel);

// Deserialize byte array to object
Customer customerModel = (CustomerModel)Converter.GetObjectFromBytes(data);


Object and XML
// Serialize object to XML
string xml = Converter.GetXmlFromObject<Customer>(customerModel);

// Deserialize XML to object
Customer customerModel = Converter.GetObjectFromXml<Customer>(xml);

*download the test project http://macds.codeplex.com/releases/view/99468

Validation

Dealing with enterprise solutions, we must create validation object to handle business rules, those validation if we don't take the correct approach, we will find them duplicated across the classes. The core offers a base class that help me build validation classes using the decorator pattern. Using this approach we will have a lot of validation units that we can group to accomplish a concrete rule.

The ValidatorBase is an abstract class that help you group the validation units that implement the IValidation interface. You need to override one method named GetValidations where you apply the validator rules to be used. Lets validate the object JoinModel.

We create our validation units
// Object to validate
public class JoinModel
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Password { get; set; }
}

// Validation Unit
public class StringEmptyValidation : IValidation
{
    private string data;

    public StringEmptyValidation(string data)
    {
        this.data = data;
    }

    #region IValidation Members

    public bool IsValid
    {
        get 
        { 
            return String.IsNullOrEmpty(this.data) ? false : true; 
        }
    }

    public Exception Exception
    {
        get { throw new ArgumentException("The data is empty"); }
    }

    #endregion
}

// Validation Group
public class JoinValidator : ValidatorBase<JoinModel>
{
    public JoinValidator(JoinModel model) : base(model) { }

    protected override IEnumerable<IValidation> GetValidations(JoinModel obj)
    {
        var list = new List<IValidation>();
        list.Add(new StringEmptyValidation(obj.Firstname));
        list.Add(new StringEmptyValidation(obj.Lastname));
        list.Add(new StringEmptyValidation(obj.Password));

        return list;
    }
}


Once you have the validation unit and the validation group created, the validation rule can be invoked in any service or repository class.
public void JoinProcess(JoinModel join)
{
    join = new JoinValidator(join).GetValidData();
    this.repository.Add(join);
}

*download the test project http://macds.codeplex.com/releases/view/99468

For complex scenarios when you are dealing with to many validation rules, the approach of adding and removing validation unit become a great feature.

Web Request

Integrating technologies like ASP 3.0 or PHP that offers web api, we use HttpWebRequest to process GET and POST call to other websites. The core offers the namespace Web when you can find the abstract class WebRequestBase. This base class provide the method SendRequest that you can use and define what type of call you want to process GET or POST, also you can define the variables that you want to send.

-Create a class that inherent from WebRequestBase-
public class HttpRequestManager : WebRequestBase
{
    public string GetRemoteDataByPost(string url, string data)
    {
        return this.SendRequest(url, data);
    }

    public string GetRemoteDataByGet(string url, string data)
    {
        return this.SendRequest(url, data, RequestMethod.GET);
    }
}


-Send Post or Get Request-
// Create instance of the http request manager class
var manager = new Mocks.HttpRequestManager();

// Retreive data from google.com
string postData = manager.GetRemoteDataByPost("http://www.domain.com/", "");

// Retreive data from google.com
string getData = manager.GetRemoteDataByGet("http://www.domain.com", "");

Last edited Dec 30, 2012 at 5:46 PM by dwaks, version 31