There are 4 basic difference between Implicit and Explicit interface. To understand all four of them see the below example first:
There is a class C1 which implements two interfaces I1 and I2. and Both the class have method with same name i.e. sum().
public class C1 : I1, I2
{
//Explicit Implementation of interface
void I2.sum()
{
}
//Implicit Implementation of interface
public void sum()
{
}
}
public interface I1
{
void sum();
}
public interface I2
{
void sum();
}
The Interface I1 is implemented implicitly and I2 is explicitly. Below are the difference based on above example.
1) In Implicit implementation, no need to specify Interface name with its method.
e.g. public void sum() { }
Where as in Explicit implementation, you need to specify Interface name with its method
e.g. void I2.sum() { }
2) In implicit, access specifier is required with method name and it must be public, even protected will not work.
Where as in Explicit, no access specifier will be required.
3) You can not make virtual method in explicit implemention and same will work in implicit implemention.
4) You can not make abstract method in explicit implemention and same will work in implicit implemention.
Practical Example
Now you wonder, by considering above scenario, what is the use of these two implementation because only Implicit implementation will also work for all type of method then why these two has been introduced by Microsoft. or what should be the practical scenario where we need to use either Implicit or Explicit.
Suppose you need to implement 2 interface and both interface will have method with same name. At that time you can not create one method in your class for both interfaces, rather you must implement one interface Implicit and one Explicit (or both Explicit) for showing two different method in your class. (Check above example).
Hiding Interface Details:
If you implement interface explicitly then it will not show in class Intellisense list. So, if you want to hide your method name to display when creating object of class then you need to go for Explicit interface implementation and in this case you need to create object of your interface to know the method name.
E.g.: In our above example if I want to invoke method of I2 interface:
I2 obj = new C1();
obj.sum();
NOTE: for more please click http://developerssolutions.wordpress.com/2010/07/22/difference-between-implicit-and-explicit-interface/
I am running this Blog to help other guys, who are looking some bits and pieces in terms of MS technology....
Monday, December 6, 2010
Is it possible to declare multiple Interfaces with same name and method?
Yes, You can have same name for all the interfaces if you declare with partial keyword.
But they can have same method but with different signature.
partial interface AAA
{
void TestAAA();
}
partial interface AAA
{
string TestAAA(parameter);
}
But they can have same method but with different signature.
partial interface AAA
{
void TestAAA();
}
partial interface AAA
{
string TestAAA(parameter);
}
Implementing Two interface having the same method signature in the same class
There can be scenario when we would have two interface with the same method name and same type of arguments, same number of arguments and even the return type can be same and we need to implement both the interface in the same class. How to implement the interface in our class? Most of you will be thinking whats so tough in this, just implement the interface and the method and move on. If the signature of the methods in the interface were different then there would have been no problem but here the signature of the methods in two different interface are same and both the interfaces are gonna be implemented in the same class. The two interface are as below.
public interface IA
{
string PrintName();
}
public interface IB
{
string PrintName();
}
From the above code we can infer that we have two interface with names IA and IB and both have a single method named “PrintName”. The signature of both the methods are same and we need to implement the interfaces in our class say “A”. One way of impelementing the interface is as shown below i.e. just having a “public” implementation of the interface method only once.
public class A : IA, IB
{
public A()
{
}
public string PrintName()
{
return this.GetType().Name;
}
}
The above implementation has got a limitation i.e the method “PrintName” is considered to be a common method for all i.e commong method for the class, and for the interfaces IA and IB. If you are writing a code shown below
A a = new A();
IA ia = new A();
IB ib = new A();
Console.WriteLine(a.PrintName());
Console.WriteLine(ia.PrintName());
Console.WriteLine(ib.PrintName());
all the calls to method “PrintName” will give you the same result, the name of the class i.e. “A”. This is because all call to the method goes to the same definition. Now if you want to give different implementation to the methods in interface IA and IB, what will you do? Its also simple, just have two impelementation of the same method and prefix the method names with the interface name as shown below.
public class A : IA, IB
{
public A()
{
}
string IA.PrintName()
{
return “IA PrintName Method”;
}
string IB.PrintName()
{
return “IB PrintName Method”;
}
}
Now the below code will give you different output.
IA ia = new A();
IB ib = new A();
Console.WriteLine(ia.PrintName());// Will print "IA PrintName Method"
Console.WriteLine(ib.PrintName());// Will print "IB PrintName Method"
So this is how two interfaces having the same method signature can be given different implementation in the same class.
For more information please visit : http://sandblogaspnet.blogspot.com/2008/05/implementing-two-interface-having-same.html
public interface IA
{
string PrintName();
}
public interface IB
{
string PrintName();
}
From the above code we can infer that we have two interface with names IA and IB and both have a single method named “PrintName”. The signature of both the methods are same and we need to implement the interfaces in our class say “A”. One way of impelementing the interface is as shown below i.e. just having a “public” implementation of the interface method only once.
public class A : IA, IB
{
public A()
{
}
public string PrintName()
{
return this.GetType().Name;
}
}
The above implementation has got a limitation i.e the method “PrintName” is considered to be a common method for all i.e commong method for the class, and for the interfaces IA and IB. If you are writing a code shown below
A a = new A();
IA ia = new A();
IB ib = new A();
Console.WriteLine(a.PrintName());
Console.WriteLine(ia.PrintName());
Console.WriteLine(ib.PrintName());
all the calls to method “PrintName” will give you the same result, the name of the class i.e. “A”. This is because all call to the method goes to the same definition. Now if you want to give different implementation to the methods in interface IA and IB, what will you do? Its also simple, just have two impelementation of the same method and prefix the method names with the interface name as shown below.
public class A : IA, IB
{
public A()
{
}
string IA.PrintName()
{
return “IA PrintName Method”;
}
string IB.PrintName()
{
return “IB PrintName Method”;
}
}
Now the below code will give you different output.
IA ia = new A();
IB ib = new A();
Console.WriteLine(ia.PrintName());// Will print "IA PrintName Method"
Console.WriteLine(ib.PrintName());// Will print "IB PrintName Method"
So this is how two interfaces having the same method signature can be given different implementation in the same class.
For more information please visit : http://sandblogaspnet.blogspot.com/2008/05/implementing-two-interface-having-same.html
Saturday, November 20, 2010
Convert a Dynamic Entity to a System Entity
This sample code shows how to convert a dynamic entity instance into a strongly typed account entity instance. The code first creates a dynamic entity, populates its properties with account attributes and values, and then converts the dynamic entity to an account entity.
using System;
using System.Reflection;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
namespace Microsoft.Crm.Sdk.HowTo
{
public class ConvertDynamicToCore
{
static void Main(string[] args)
{
// TODO: Change the server URL and organization to match your Microsoft
// Dynamics CRM Server and Microsoft Dynamics CRM organization.
ConvertDynamicToCore.Run("http://localhost:5555", "CRM_SDK");
}
public static bool Run(string crmServerUrl, string orgName)
{
// Create an account dynamic entity.
DynamicEntity accountDynamicEntity = new DynamicEntity();
//Set a few account properties.
StringProperty name = new StringProperty();
name.Name = "name";
name.Value = "Fabrikam Inc.";
StringProperty accountnumber = new StringProperty();
accountnumber.Name = "accountnumber";
accountnumber.Value = "AZ1200";
Picklist plist = new Picklist();
plist.name = "UPS";
plist.Value = 2;
PicklistProperty shippingmethodcode = new PicklistProperty();
shippingmethodcode.Name = "address1_shippingmethodcode";
shippingmethodcode.Value = plist;
accountDynamicEntity.Properties = new Property[] { name, accountnumber, shippingmethodcode };
accountDynamicEntity.Name = EntityName.account.ToString();
//Create a strongly typed account entity to copy the dynamic entity into.
account coreAccount = new account();
//Convert the dynamic entity to a strongly typed business entity.
coreAccount = (account)Convert(accountDynamicEntity);
Console.WriteLine("\n|Core Account Attributes\n|=======================");
Console.WriteLine("|name: {0}", coreAccount.name);
Console.WriteLine("|accountnumber: {0}", coreAccount.accountnumber);
Console.WriteLine("|address1_shipmethodcode: {0}", coreAccount.address1_shippingmethodcode.Value);
return true;
}
///
/// Convert a dynamic entity into a strongly typed business entity.
///
public static BusinessEntity Convert(DynamicEntity entity)
{
string coreEntityName = entity.Name;
Type entType = (new account()).GetType();
ConstructorInfo init = entType.GetConstructor(new Type[] { });
object ent = init.Invoke(new object[] { });
foreach (Property p in entity.Properties)
{
PropertyInfo entProp = entType.GetProperty(p.Name);
if (null == entProp)
{
Console.WriteLine("Could not find attribute {0} on entity {1}.", p.Name, coreEntityName);
}
else
{
entProp.SetValue(ent, GetAttribute(entity, p.Name), null);
}
}
return (BusinessEntity)ent;
}
///
/// This method returns the value of a dynamic entity attribute.
///
public static object GetAttribute(BusinessEntity entity, string attribute)
{
if (entity.GetType() == typeof(DynamicEntity))
{
DynamicEntity de = (DynamicEntity)entity;
foreach (Property prop in de.Properties)
{
if (prop.Name == attribute)
{
PropertyInfo propInfo = prop.GetType().GetProperty("Value");
return propInfo.GetValue(prop, null);
}
}
return null;
}
else
{
PropertyInfo propInfo = entity.GetType().GetProperty(attribute);
return propInfo.GetValue(entity, null);
}
}
}
}
using System;
using System.Reflection;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
namespace Microsoft.Crm.Sdk.HowTo
{
public class ConvertDynamicToCore
{
static void Main(string[] args)
{
// TODO: Change the server URL and organization to match your Microsoft
// Dynamics CRM Server and Microsoft Dynamics CRM organization.
ConvertDynamicToCore.Run("http://localhost:5555", "CRM_SDK");
}
public static bool Run(string crmServerUrl, string orgName)
{
// Create an account dynamic entity.
DynamicEntity accountDynamicEntity = new DynamicEntity();
//Set a few account properties.
StringProperty name = new StringProperty();
name.Name = "name";
name.Value = "Fabrikam Inc.";
StringProperty accountnumber = new StringProperty();
accountnumber.Name = "accountnumber";
accountnumber.Value = "AZ1200";
Picklist plist = new Picklist();
plist.name = "UPS";
plist.Value = 2;
PicklistProperty shippingmethodcode = new PicklistProperty();
shippingmethodcode.Name = "address1_shippingmethodcode";
shippingmethodcode.Value = plist;
accountDynamicEntity.Properties = new Property[] { name, accountnumber, shippingmethodcode };
accountDynamicEntity.Name = EntityName.account.ToString();
//Create a strongly typed account entity to copy the dynamic entity into.
account coreAccount = new account();
//Convert the dynamic entity to a strongly typed business entity.
coreAccount = (account)Convert(accountDynamicEntity);
Console.WriteLine("\n|Core Account Attributes\n|=======================");
Console.WriteLine("|name: {0}", coreAccount.name);
Console.WriteLine("|accountnumber: {0}", coreAccount.accountnumber);
Console.WriteLine("|address1_shipmethodcode: {0}", coreAccount.address1_shippingmethodcode.Value);
return true;
}
///
/// Convert a dynamic entity into a strongly typed business entity.
///
public static BusinessEntity Convert(DynamicEntity entity)
{
string coreEntityName = entity.Name;
Type entType = (new account()).GetType();
ConstructorInfo init = entType.GetConstructor(new Type[] { });
object ent = init.Invoke(new object[] { });
foreach (Property p in entity.Properties)
{
PropertyInfo entProp = entType.GetProperty(p.Name);
if (null == entProp)
{
Console.WriteLine("Could not find attribute {0} on entity {1}.", p.Name, coreEntityName);
}
else
{
entProp.SetValue(ent, GetAttribute(entity, p.Name), null);
}
}
return (BusinessEntity)ent;
}
///
/// This method returns the value of a dynamic entity attribute.
///
public static object GetAttribute(BusinessEntity entity, string attribute)
{
if (entity.GetType() == typeof(DynamicEntity))
{
DynamicEntity de = (DynamicEntity)entity;
foreach (Property prop in de.Properties)
{
if (prop.Name == attribute)
{
PropertyInfo propInfo = prop.GetType().GetProperty("Value");
return propInfo.GetValue(prop, null);
}
}
return null;
}
else
{
PropertyInfo propInfo = entity.GetType().GetProperty(attribute);
return propInfo.GetValue(entity, null);
}
}
}
}
PublishXml Message (CrmService)
Publish the customizations for the specified entities.
The relevant classes are: 1) PublishXmlRequest 2) PublishXmlResponse
Remarks
To use this message, pass an instance of the PublishXmlRequest class as the request parameter in the Execute method.
For a list of required privileges, see PublishXml Privileges.
Example
The following code example shows how to use the PublishXml message.
// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create the request.
PublishXmlRequest request = new PublishXmlRequest();
request.ParameterXml = @"<importexportxml>
<entities>
<entity>account</entity>
<entity>contact</entity>
</entities>
<nodes/>
<securityroles/>
<settings/>
<workflows/>
</importexportxml>";
// Execute the request.
PublishXmlResponse response = (PublishXmlResponse)service.Execute(request);
The relevant classes are: 1) PublishXmlRequest 2) PublishXmlResponse
Remarks
To use this message, pass an instance of the PublishXmlRequest class as the request parameter in the Execute method.
For a list of required privileges, see PublishXml Privileges.
Example
The following code example shows how to use the PublishXml message.
// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create the request.
PublishXmlRequest request = new PublishXmlRequest();
request.ParameterXml = @"<importexportxml>
<entities>
<entity>account</entity>
<entity>contact</entity>
</entities>
<nodes/>
<securityroles/>
<settings/>
<workflows/>
</importexportxml>";
// Execute the request.
PublishXmlResponse response = (PublishXmlResponse)service.Execute(request);
Create Custom Entity Message (MetadataService)
The relevant classes: 1) CreateEntityRequest 2) CreateEntityResponse
Remarks
To perform this action, the caller must be a user in the organization for which metadata is requested and must haveCreate Entity and Read Entity privileges.
The entity created will have a property value of true for the EntityMetadata.IsCustomEntity property.
You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.
Example with Source Code:
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;
// Create the entity.
EntityMetadata timesheetEntity = new EntityMetadata();
timesheetEntity.SchemaName = "new_timesheet";
timesheetEntity.OwnershipType = new CrmOwnershipTypes();
timesheetEntity.OwnershipType.Value = OwnershipTypes.UserOwned;
timesheetEntity.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheet", 1033);
timesheetEntity.DisplayCollectionName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheets", 1033);
timesheetEntity.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Timesheet", 1033);
// Create the primary attribute.
StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();
primaryAttribute.SchemaName = "new_name";
primaryAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
primaryAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
primaryAttribute.MaxLength = new MetadataServiceSdk.CrmNumber();
primaryAttribute.MaxLength.Value = 100;
primaryAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Name", 1033);
primaryAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Name", 1033);
// Create the entity request.
CreateEntityRequest createEntity = new CreateEntityRequest();
createEntity.Entity = timesheetEntity;
createEntity.HasActivities = false;
createEntity.HasNotes = true;
createEntity.PrimaryAttribute = primaryAttribute;
// Execute the request.
CreateEntityResponse entityResponse = (CreateEntityResponse )metadataService.Execute(createEntity);
// Publish the new custom entity.
// Note that the custom entities cannot be used until they have been published.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);
Remarks
To perform this action, the caller must be a user in the organization for which metadata is requested and must haveCreate Entity and Read Entity privileges.
The entity created will have a property value of true for the EntityMetadata.IsCustomEntity property.
You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.
Example with Source Code:
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;
// Create the entity.
EntityMetadata timesheetEntity = new EntityMetadata();
timesheetEntity.SchemaName = "new_timesheet";
timesheetEntity.OwnershipType = new CrmOwnershipTypes();
timesheetEntity.OwnershipType.Value = OwnershipTypes.UserOwned;
timesheetEntity.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheet", 1033);
timesheetEntity.DisplayCollectionName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheets", 1033);
timesheetEntity.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Timesheet", 1033);
// Create the primary attribute.
StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();
primaryAttribute.SchemaName = "new_name";
primaryAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
primaryAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
primaryAttribute.MaxLength = new MetadataServiceSdk.CrmNumber();
primaryAttribute.MaxLength.Value = 100;
primaryAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Name", 1033);
primaryAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Name", 1033);
// Create the entity request.
CreateEntityRequest createEntity = new CreateEntityRequest();
createEntity.Entity = timesheetEntity;
createEntity.HasActivities = false;
createEntity.HasNotes = true;
createEntity.PrimaryAttribute = primaryAttribute;
// Execute the request.
CreateEntityResponse entityResponse = (CreateEntityResponse )metadataService.Execute(createEntity);
// Publish the new custom entity.
// Note that the custom entities cannot be used until they have been published.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);
Wednesday, October 27, 2010
Maximum Number of Index per Table in SQL SERVER
SQL Server 2005:
1 Clustered Index + 249 Nonclustered Index = 250 Index
SQL Server 2008:
1 Clustered Index + 999 Nonclustered Index = 1000 Index
1 Clustered Index + 249 Nonclustered Index = 250 Index
SQL Server 2008:
1 Clustered Index + 999 Nonclustered Index = 1000 Index
Subscribe to:
Posts (Atom)
Split the String values with a special character in MS Flow to convert this into Array
Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...
-
Sometimes you experienced when you have subgrid in your CRM Form, but when you click the ‘expand’ button to expand the view then it will re...
-
I have a web service that takes a serialize string and saves it. This works fine for "small" files, but once I hit a large siz...
-
Use the following line of code to create complete workflow activity with two output parameters. 1) Open Visual Studio 2010 ID. 2) Open ...